14 min read

Learning Objectives

By the end of this module, you will be able to:

  • Provision Dedicated Core and vCPU servers with `ionoscloud_server` and `ionoscloud_vcpu_server`, sized within the platform limits
  • Bootstrap servers at first boot with cloud-init `user_data` passed from Terraform
  • Reserve static public IPv4 addresses with `ionoscloud_ipblock` and assign them to a server NIC
  • Build reproducible environments from `ionoscloud_snapshot` and define horizontal scaling with `ionoscloud_autoscaling_group`
  • Choose between Cubes and Compute Engine servers based on the storage model each one provisions

Unit 2.1: Compute and Server Automation

Introduction

In Module 1 you built TaskBoard's base VDC with one generic server. Now TaskBoard needs a real compute tier: an API server with dedicated cores that handles request traffic, and a cheaper worker that processes background jobs. Both must come up configured, without anyone logging in to install packages.

In this unit you write that compute tier as code: server types and CPU options, cloud-init through Terraform, a reserved public IP, snapshots, VM Auto Scaling Groups and Cubes. In the Code Lab you deploy the compute tier and verify SSH access and the cloud-init run.

1. Server Types in Terraform

resource "ionoscloud_server" "api" {
  name              = "taskboard-api-01"
  datacenter_id     = ionoscloud_datacenter.taskboard.id
  type              = "ENTERPRISE"
  cores             = 4
  ram               = 8192
  cpu_family        = ionoscloud_datacenter.taskboard.cpu_architecture[0].cpu_family
  availability_zone = "AUTO"
  image_name        = data.ionoscloud_image.ubuntu.id
  ssh_keys          = [var.ssh_public_key]

  volume {
    name      = "system"
    size      = 20
    disk_type = "BALANCED"
  }

  nic {
    lan  = ionoscloud_lan.public.id
    name = "public"
    dhcp = true
  }
}

The type argument selects the server usage: ENTERPRISE (now named Dedicated Core), CUBE or VCPU. It is immutable, like availability_zone, which accepts AUTO, ZONE_1 or ZONE_2 (Compute has no Zone 3; see Unit 2.4). ram is given in MB, so 8 GB is 8192.

1.1 Dedicated Core vs vCPU

The two Compute Engine server types differ in how CPU capacity is assigned and what you can control in code:

Property Dedicated Core Server vCPU Server
CPU use Exclusive Shared
Maximum compute 62 cores 60 vCPUs
Maximum RAM 230 GB 230 GB
cpu_family selectable Yes No
Terraform resource ionoscloud_server with type = "ENTERPRISE" ionoscloud_vcpu_server (or type = "VCPU")

As the table shows, choose Dedicated Core for predictable CPU capacity and control over the CPU architecture. A vCPU Server's performance depends on the underlying CPU, the VM configuration and the current load on the shared host. RAM is set in increments of 0.25 GB. For TaskBoard's background worker, a vCPU Server is enough:

resource "ionoscloud_vcpu_server" "worker" {
  name          = "taskboard-worker-01"
  datacenter_id = ionoscloud_datacenter.taskboard.id
  cores         = 2
  ram           = 4096
  image_name    = data.ionoscloud_image.ubuntu.id
  ssh_keys      = [var.ssh_public_key]

  volume {
    name      = "system"
    size      = 20
    disk_type = "BALANCED"
  }

  nic {
    lan  = ionoscloud_lan.public.id
    name = "public"
    dhcp = true
  }
}

volume is required on ionoscloud_vcpu_server, and there is no cpu_family argument: the CPU family of a vCPU Server cannot be chosen at creation or changed later.

1.2 Selecting the CPU Family

On a Dedicated Core Server, cpu_family sets the CPU architecture. Not all CPU architectures are available in all data center regions, so the API server above reads the family from the computed cpu_architecture attribute of ionoscloud_datacenter instead of hard-coding one. Unlike the vCPU Server, the CPU family of a Dedicated Core Server can be changed after creation.

2. Cloud-Init Bootstrap from Terraform

Cloud-init automates the initialization of servers during system boot. User data must be written in shell scripts or cloud-config directives using YAML syntax. It is available on all public Linux images supplied by IONOS CLOUD; Windows images and Linux LVM are currently out of scope.

2.1 Writing the cloud-config

Store the configuration next to your Terraform code, for example cloud-init/api.yaml:

#cloud-config
package_update: true
packages:
  - nginx
runcmd:
  - systemctl enable --now nginx

The first line #cloud-config marks the content as cloud-config data. A file that starts with #! is run as a user-data script instead.

2.2 Passing user_data

In the Cloud API v6, cloud-init is configured on the volume resource. In Terraform, user_data is an argument of the volume block and expects the cloud-init configuration as a base64 encoded string:

data "ionoscloud_image" "ubuntu" {
  image_alias = "ubuntu:latest"
  type        = "HDD"
  cloud_init  = "V1"
  location    = var.location
}

resource "ionoscloud_server" "api" {
  # ... arguments from Section 1 ...
  volume {
    name      = "system"
    size      = 20
    disk_type = "BALANCED"
    user_data = base64encode(file("${path.module}/cloud-init/api.yaml"))
  }
}

The user_data property is immutable, is only allowed on new volume creation, and works only with cloud-init compatible images, which is why the image data source filters on cloud_init = "V1".

2.3 SSH Key Injection

ssh_keys accepts key text or file paths, and SSH keys are only supported if a public Linux image is used for the volume creation. The field is immutable, may only be set in creation requests, and always returns null when read.

variable "ssh_public_key" {
  type        = string
  description = "Public key text, for example the content of ~/.ssh/id_ed25519.pub"
}

The path form does not support ~ expansion, so pass key text from a variable, as shown, or use an absolute path.

3. Public IP Addresses with ionoscloud_ipblock

A NIC connected to the internet gets a public IPv4 address by DHCP, and that address may change when you deallocate the VM using Power Stop or remove the network interface. For an API endpoint behind a DNS record, reserve a static address.

3.1 Reserving the Block

resource "ionoscloud_ipblock" "api" {
  name     = "taskboard-api-ip"
  location = ionoscloud_datacenter.taskboard.location
  size     = 1
}

An IP address can only be used in the data center region where it was reserved, so the block takes its location from the data center. You cannot reserve a specific IPv4 address. Only contract owners, administrators or users with the Reserve IP Blocks privilege can reserve IP addresses, so a CI token needs that privilege.

3.2 Assigning the Address to the NIC

resource "ionoscloud_server" "api" {
  # ...
  nic {
    lan  = ionoscloud_lan.public.id
    name = "public"
    dhcp = true
    ips  = [ionoscloud_ipblock.api.ips[0]]
  }
}

The reference to the computed ips list makes Terraform reserve the block before it creates the server. The computed ip_consumers attribute shows which NIC and server use each address. Reserved addresses are charged, so destroy unused blocks.

4. Snapshots and Reproducible Environments

4.1 Capturing a Configured Volume

resource "ionoscloud_snapshot" "api_golden" {
  datacenter_id = ionoscloud_datacenter.taskboard.id
  volume_id     = ionoscloud_server.api.boot_volume
  name          = "taskboard-api-golden"
  description   = "API server after cloud-init bootstrap"
}

The server's computed boot_volume attribute gives you the volume ID. If data still in the VM's RAM must be in the snapshot, run sync or shut down the guest OS first.

4.2 Building Servers from the Snapshot

image_name on a server accepts an image name, ID or alias, and may also be a snapshot ID:

resource "ionoscloud_server" "api_clone" {
  # same arguments as ionoscloud_server.api, except:
  name       = "taskboard-api-02"
  image_name = ionoscloud_snapshot.api_golden.id
}

Snapshots are limited to the data center location where they were originally created, although several VDCs in that location can use them. Snapshots are not incremental: each one is a separate copy that uses storage space in your account, so delete old ones in your pipeline. Use cloud-init for fast bootstraps from clean images, and a snapshot when a slow bootstrap must be frozen at a known state.

5. VM Auto Scaling and Cubes

5.1 An Auto Scaling Group as Code

resource "ionoscloud_autoscaling_group" "workers" {
  datacenter_id     = ionoscloud_datacenter.taskboard.id
  name              = "taskboard-workers"
  min_replica_count = 1
  max_replica_count = 4

  policy {
    metric              = "INSTANCE_CPU_UTILIZATION_AVERAGE"
    scale_in_threshold  = 25
    scale_out_threshold = 70
    unit                = "PER_MINUTE"
    scale_in_action {
      amount                  = 1
      amount_type             = "ABSOLUTE"
      termination_policy_type = "OLDEST_SERVER_FIRST"
      cooldown_period         = "PT5M"
      delete_volumes          = true
    }
    scale_out_action {
      amount          = 1
      amount_type     = "ABSOLUTE"
      cooldown_period = "PT5M"
    }
  }

  replica_configuration {
    availability_zone = "AUTO"
    cores             = 2
    ram               = 4096
    nic {
      lan  = ionoscloud_lan.app.id # private app LAN, built in Unit 2.2
      name = "app"
      dhcp = true
    }
    volume {
      image_alias = "ubuntu:latest"
      name        = "system"
      size        = 20
      type        = "BALANCED"
      user_data   = base64encode(file("${path.module}/cloud-init/worker.yaml"))
      ssh_keys    = [var.ssh_public_key]
      boot_order  = "AUTO"
    }
  }
}

VM Auto Scaling supports horizontal scaling only: it adds or removes VMs of the same size, driven by one scaling policy per group. The metric is one of INSTANCE_CPU_UTILIZATION_AVERAGE, INSTANCE_NETWORK_IN_BYTES, INSTANCE_NETWORK_IN_PACKETS, INSTANCE_NETWORK_OUT_BYTES or INSTANCE_NETWORK_OUT_PACKETS. The scale-in and scale-out thresholds must differ by at least 40%, which is why the example uses 25 and 70. The cooldown period is between 2 minutes and 24 hours, with a default of 5 minutes. Replica volumes use the Essential, Balanced or Performance storage types.

5.2 Rules That Affect Your Code

  • Replica configuration applies to new replicas only. Updating replica_configuration does not affect the existing replicas.
  • Set delete_volumes deliberately. With false, the volumes of removed replicas remain in the data center (see Common Pitfalls).
  • Keep the group size moderate. The minimum replica count is 1. IONOS CLOUD recommends at most 100 VMs per group and at most 5 VMs added or removed per scaling action.
  • Plan for spikes. Cooldown timers scale gradually, so add VMs ahead of a known traffic peak.

The group itself lives in the separate VM Auto Scaling API (see the Quick Card).

5.3 Cubes in Code

data "ionoscloud_template" "xs" {
  name = "Basic Cube XS"
}

resource "ionoscloud_cube_server" "helper" {
  name           = "taskboard-cube-01"
  datacenter_id  = ionoscloud_datacenter.taskboard.id
  template_uuid  = data.ionoscloud_template.xs.id
  image_name     = "ubuntu:latest"
  image_password = var.image_password
  volume {
    name         = "nvme"
    licence_type = "LINUX"
    disk_type    = "DAS"
  }
}

A Cube is sized by its template, not by cores and ram. Its inline volume must use disk_type = "DAS", and you cannot set volume.size, because the template defines the direct-attached NVMe storage. The Basic templates range from Basic Cube XS (1 vCPU, 2 GB RAM, 60 GB NVMe) to Basic Cube XL (16 vCPUs, 32 GB RAM, 960 GB NVMe). Template properties cannot be changed after provisioning, and deleting the Cube also deletes its NVMe volume, so take a snapshot first if you need the data. Each Cube supports up to 23 Block Storage volumes in addition to the NVMe volume.

API Reference Quick Card

Key API endpoints behind this unit's resources:

Method Endpoint Description
POST /datacenters/{datacenterId}/servers Created by ionoscloud_server and ionoscloud_vcpu_server
POST /ipblocks Created by ionoscloud_ipblock
POST /datacenters/{datacenterId}/volumes/{volumeId}/create-snapshot Creates a snapshot of a volume
GET /templates Lists the Cube templates
GET /groups/{groupId}/servers Lists the servers of an Auto Scaling Group (VM Auto Scaling API)

Base URL: https://api.ionos.com/cloudapi/v6 (VM Auto Scaling: https://api.ionos.com/autoscaling) Authentication: Authorization: Bearer <token>

Code Lab

Objective: Deploy TaskBoard's compute tier with Terraform: a Dedicated Core API server (4 cores, 8 GB RAM) with a reserved public IP and cloud-init bootstrap, and a vCPU worker (2 cores). Verify SSH access and the cloud-init run.

Prerequisites:

  • IONOS CLOUD account with an API token in IONOS_TOKEN and the Reserve IP Blocks privilege
  • Terraform installed locally, with the provider pinned as in Unit 1.2
  • An SSH key pair, for example ~/.ssh/id_ed25519

Step 1: Create the project and the cloud-config

mkdir -p taskboard-compute/cloud-init && cd taskboard-compute
# save the cloud-config from Section 2.1 as cloud-init/api.yaml
head -n 1 cloud-init/api.yaml

Expected output:

#cloud-config

Step 2: Write main.tf

Combine the pinned provider, data center and public LAN from Unit 1.2, the image data source (Section 2.2), the IP block (Section 3.1), the API server (Section 1, plus user_data from 2.2 and ips from 3.2) and the worker (Section 1.1). Add:

output "datacenter_id" { value = ionoscloud_datacenter.taskboard.id }
output "api_public_ip" { value = ionoscloud_ipblock.api.ips[0] }
output "worker_ip"     { value = ionoscloud_vcpu_server.worker.primary_ip }

Expected output:

(file saved)

Step 3: Plan and apply

export TF_VAR_ssh_public_key="$(cat ~/.ssh/id_ed25519.pub)"
terraform init
terraform plan -out=tfplan
terraform apply tfplan

Expected output:

Plan: 5 to add, 0 to change, 0 to destroy.
...
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

Outputs:
api_public_ip = "<reserved IP>"
datacenter_id = "<datacenter UUID>"
worker_ip = "<DHCP-assigned IP>"

Step 4: Verify the servers through the API

DC=$(terraform output -raw datacenter_id)
curl -s -H "Authorization: Bearer $IONOS_TOKEN" \
  "https://api.ionos.com/cloudapi/v6/datacenters/$DC/servers?depth=1" \
  | jq -r '.items[].properties | "\(.name) \(.type) \(.cores) \(.ram) \(.vmState)"'

Expected output:

taskboard-api-01 ENTERPRISE 4 8192 RUNNING
taskboard-worker-01 VCPU 2 4096 RUNNING

Step 5: Connect over SSH and wait for cloud-init

ssh -i ~/.ssh/id_ed25519 root@"$(terraform output -raw api_public_ip)" \
  'cloud-init status --wait && tail -n 3 /var/log/cloud-init-output.log'

Expected output:

status: done
Cloud-init v. ... finished at ...

Step 6: Check the bootstrapped service

curl -sI "http://$(terraform output -raw api_public_ip)" | head -n 1

Expected output:

HTTP/1.1 200 OK

Validation Checklist:

  • [ ] terraform apply created the data center, LAN, IP block and both servers
  • [ ] The API lists taskboard-api-01 as ENTERPRISE and taskboard-worker-01 as VCPU
  • [ ] SSH works with your key, and cloud-init status reports done
  • [ ] The API server answers on its reserved public IP

Cleanup:

terraform destroy -auto-approve

Common Pitfalls

Developer mistakes to avoid with this unit's topic:

  1. Testing before cloud-init has finished

    • Problem: terraform apply completes, but the first health check fails or packages are missing.
    • Why it happens: Provisioning complete means the infrastructure is ready; bootstrapping, including the execution of cloud-init data, can take additional time.
    • Fix: Wait with cloud-init status --wait in your pipeline, and read /var/log/cloud-init-output.log and /var/log/cloud-init.log when something is missing.
  2. Expecting a user_data change to reconfigure a server

    • Problem: You edit the cloud-config and run apply, but the running server never picks up the change.
    • Why it happens: user_data is immutable and only allowed on new volume creation. Cloud-init can only be set at initial provisioning and cannot be applied to servers that are already provisioned.
    • Fix: Design servers as replaceable and rebuild them with terraform apply -replace=ionoscloud_server.api, or apply runtime changes with a configuration management tool.
  3. Volume leaks from an Auto Scaling Group

    • Problem: After days of scaling, scale-out fails and the data center is full of unattached volumes.
    • Why it happens: With delete_volumes = false, removed replicas leave their volumes behind, and every scale-out creates new ones until the contract's resource limits are reached.
    • Fix: Set delete_volumes = true for stateless replicas and keep state in managed services or shared storage (Units 2.4 and 2.5).

Summary

You can now build TaskBoard's compute tier as code: a Dedicated Core API server and a vCPU worker that bootstrap themselves from a base64 encoded cloud-config, a reserved public IP for the API, snapshots for reuse in the same location, Auto Scaling Groups for identical replicas, and Cubes sized by template.

Key Points:

  • type and availability_zone on a server are immutable; servers use AUTO, ZONE_1 or ZONE_2
  • Only Dedicated Core Servers let you choose cpu_family; read it from cpu_architecture of the data center
  • user_data is base64 encoded cloud-init, set only on new volume creation with a cloud-init compatible image
  • Reserved IPs come from ionoscloud_ipblock in the data center's region; DHCP addresses can change on Power Stop
  • Auto Scaling is horizontal only, and replica configuration changes reach new replicas only

Important Terminology:

  • Dedicated Core Server: A server with exclusive CPU cores, type = "ENTERPRISE", with a selectable CPU family
  • vCPU Server: A server with shared virtual CPUs, managed with ionoscloud_vcpu_server
  • user_data: The base64 encoded cloud-init configuration of a volume, applied once at first boot
  • IP block: A set of reserved, static public IPv4 addresses that is bound to one region
  • Replica configuration: The template an Auto Scaling Group uses to create each new VM

Next Steps

Continue Learning: Unit 2.2: Network and Connectivity as Code

Related Topics: