Skip to content

Create a VM via Terraform Provider

To be able to create a virtual machine on the STACKIT platform with Terraform, the STACKIT Terraform Provider has to be installed.

Follow the installation instruction for the provider on The official Terraform provider for STACKIT. You will also need your Project ID as well as the ID of the image you want to use.

In your main.tf file, create a volume resource.

resource "stackit_volume" "<VOLUME_NAME>" {
project_id = "<PROJECT_ID>"
name = "<VOLUME_NAME>"
availability_zone = "eu01-1"
description = "<DESCRIPTION>"
size = <VOLUME_SIZE>
performance_class = "<PERFORMANCE_CLASS_NAME>"
source = {
id = "<IMAGE_ID>"
type = "image"
}
labels = {
"my" = "label"
}
}

Create a security group resource and add the rules to allow SSH and ICMP.

resource "stackit_security_group" "sg1" {
project_id = "<PROJECT_ID>"
name = "sg1"
}
resource "stackit_security_group_rule" "SSH" {
project_id = "<PROJECT_ID>"
security_group_id = stackit_security_group.sg1.security_group_id
direction = "ingress"
protocol = {
name = "tcp"
}
port_range = {
min = 22
max = 22
}
}
resource "stackit_security_group_rule" "ICMP" {
project_id = "<PROJECT_ID>"
security_group_id = stackit_security_group.sg1.security_group_id
direction = "ingress"
protocol = {
name = "icmp"
}
}

Create a network, interface resource and a public IP for your server.

resource "stackit_network" "net1" {
project_id = "<PROJECT_ID>"
name = "net1"
nameservers = ["1.1.1.1"]
ipv4_prefix_length = 24
labels = {
"key" = "value"
}
}
resource "stackit_network_interface" "nic1" {
project_id = "<PROJECT_ID>"
network_id = stackit_network.net1.network_id
allowed_addresses = [<ARRAY_OF_ALLOWED_IP_ADDRESSES]
security_group_ids = [stackit_security_group.sg1.security_group_id]
}
resource "stackit_public_ip" "pub1" {
project_id = "<PROJECT_ID>"
network_interface_id = stackit_network_interface.nic1.network_interface_id
}

Create a key pair that will be used to authenticate your SSH access.

resource "stackit_key_pair" "mykey" {
name = "mykey"
public_key = chomp(file("~/.ssh/id_rsa.pub"))
labels = {
"foo" = "bar"
}
}

Create a server resource and attach your network interface to the server.

resource "stackit_server" "<SERVER_NAME>" {
project_id = "<PROJECT_ID"
name = "<SERVER_NAME>"
boot_volume = {
source_type = "volume"
source_id = stackit_volume.<VOLUME_NAME>.volume_id
}
availability_zone = "eu01-1"
labels = {
"key" = "value"
}
machine_type = "<FLAVOR_ID>"
keypair_name = "<KEY_NAME>"
}
resource "stackit_server_network_interface_attach" "<ATTACH_NAME>" {
project_id = "<PROJECT_ID>"
server_id = stackit_server.<SERVER_NAME>.server_id
network_interface_id = stackit_network_interface.nic1.network_interface_id
}

To apply these settings to your project, you have to plan the files in your Terraform directory.

Terminal window
terraform plan

Now you are able to apply the changes.

Terminal window
terraform apply

The creation of your server should start now.

After the server is created, try to connect to the server.

Terminal window
ssh <USERNAME>@<PUBLIC_IP>