Dedicated Valheim Server, Built with Terraform-GCP

Jacob Luna
6 min readApr 19, 2021

--

I have been playing Valheim for almost two months and thought it would be a fun Terraform project to create a module for others to use.

Overview:

This walk through will show you how to deploy a dedicated Valheim server utilizing the IaC tool Terraform on Google Cloud Platform.

(This walkthrough is geared to those who have a basic understanding of GCP and Terraform already. Find my article on “Setting Up Terraform Cloud with GCP for more beginner info.)

What is Valheim?

Valheim is an early access survival game based around norse mythology that launched on Steam on February 2nd 2021; it has sold 5.7million copies so far!

Repository:

I created a public repository and nested the Terraform configuration files here.

cd into your Desktop directory and git clone the repo

Open Valheim-TF with your preferred text editor or IDE and navigate to the “provider.tf”; update the credential attribute to the path of your JSON key.

terraform {
required_version = ">=0.14"

required_providers {
google = {
source = "hashicorp/google"
version = "~>3.0"
}
}
}


provider "google" {
region = var.region[0]
zone = var.zone[0]
credentials = file("PATH_HERE") #Feel free to use other options like ENV VAR #
project = var.project
}

Look at the vars.tf to understand what is needed to be specified if a .tfvars file is not created.

variable "service_name" {}

variable "project" {}

variable "server_name" {}

variable "machine_type" {
type = string
default = "e2-medium"
}
variable "VPC" {
type = string
default = "default"
}

variable "region" {
type = list(any)
default = ["us-central1", "us-west1", "us-east1"]
}

variable "zone" {
type = list(any)
default = ["us-central1-a", "us-west1-a", "us-east1-a"]
}

The main.tf is where the deployed infrastructure lives. This includes, service account, server, and firewall needed by Valheim to run properly. Ensure you update the path for the “metadate_startup_script” file path, or the start up script will fail.

#Service account for Server will use
resource "google_service_account" "Valheim_svc" {
account_id = var.service_name
display_name = var.service_name
project = var.project
}


#Server
resource "google_compute_instance" "valheim-server" {
name = var.server_name
machine_type = var.machine_type


tags = ["valheim-fw"]

boot_disk {
initialize_params {
image = "projects/confidential-vm-images/global/images/ubuntu-2004-focal-v20200720"
}
}

metadata_startup_script = file("/Users/test/Desktop/Valheim-TF/valheim-startup.sh")


network_interface {
network = var.VPC
access_config {}
}

service_account {
email = google_service_account.Valheim_svc.email
scopes = ["cloud-platform"]
}
}



#firewall
resource "google_compute_firewall" "valheim-fw" {
name = "valheim-fw"
network = var.VPC

allow {
protocol = "icmp"
}

allow {
protocol = "tcp"
ports = ["2456", "2457", "2458"]
}

target_tags = ["valheim-fw"]
}

Deploy Infrastructure:

Run terraform plan; you will immediate notice a prompt to fill in the value of the variables in the vars.tf file. Once all values are entered, the plan will output.

Valheim-TF % terraform plan
var.project
Enter a value: topo
var.server_name
Enter a value: Valheim-server-1
var.service_name
Enter a value: svc-valheim

If you are happy with the results, run terraform apply with the same values.

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.Outputs:pub_ip = “35.223.132.72”
server_name = “valheim-server-1”
svc_email = “svc-valheim@topo.iam.gserviceaccount.com

Google Cloud Platform(GCP):

Head to the GCP console as you need to SSH into the VM to do some manual adjustments.

The valheim-postboot.txt will have all the commands needed to copy and paste into the VM terminal, but first verify the user “steam” is on the VM.

Next, install steamcmd with sudo apt install steamcmd and accept the license agreement.

Navigate to the steam user with the command, sudo -i -u steam bash, create the symbolic link (ln -s /usr/games/steamcmd steamcmd). You can confirm it worked with ls -la command.

Create an environment variable.

Once the environment variable is created you have to open up a new terminal window for the changes to go into effect. You can do this by selecting “New Connection to <server name>” from the settings cog.

Once the new terminal is up, sudo back into the steam user and echo $path.

Valheim configuration:

Install Valheim. The first installation might take up to 5 minutes. To verify it is installed use the ls command. There will be a new directory called valheimserver.

Create a new startup script under the valheimserver directory called “start_valheim.sh”; copy & paste the Valheim server configs and update the -name -world -password entry.

Make start_valheim.sh executable with chmod command. You can now execute the script and it will boot your server!

Optional systemd unit:

Navigate back to a sudo user and create a service in systemd/system directory (sudo vi /etc/systemd/system/valheimserver.service), copy & paste the .service file.

Run sudo systemctl daemon-reload then run sudo systemctl start valheimserver. This will boot the Valheim server. Confirm the valheim server is done booting with the sudo systemctl status valheimserver command.

Connecting to Valheim Server:

Open up Valheim on your PC and select “Join Game” > then choose “Join IP”. Enter the IP address seen in Terraform outputs or in GCP console. Enter the password when prompted. You should spawn in the world like normal.

FIN:

Thank you for reading my guide on setting up a dedicated linux Valheim server. There is an amazing video from Geekhead on youtube that does a walk through similar to the steps above.

Bio: Jake has been in IT for 4 years and is currently an IT Engineer for Aunt Bertha. He currently holds a CompTIA S+ and Google’s ACE , Google’s PCA and Terraform associate certifications!

LinkedIn: https://www.linkedin.com/in/jacob-c-luna/

--

--

No responses yet