Skip to content

Your first instance

This builds one instance, imports your SSH key, opens port 22 from one network and gives the instance a public IP, so you can log in. It uses the compute:write and network:write scopes.

An instance needs a region, a size and a storage pool. This guide builds in region gro. No instance type can be ordered yet, so you give the size yourself: the vCPUs, the memory and the disk.

  1. The region: a region id from List compute regions, or from the virtscale_regions data source.

  2. The size. The API accepts these values:

    Attribute Allowed values
    vcpus 1, 2, 4, 8, 16 or 32
    memory_mb Whole GB from 1 to 128, written in MB: the GB times 1024, so 1024 to 131072
    disk_gb 10 to 2000, in steps of 10
  3. The storage pool and the network profile. In gro the storage pool is gro-ceph and the network profile is gro-tenant-default. You can leave network_profile out: the API picks the region’s default.

A size off the grid is refused with 422 invalid_spec. To read the values from the API:

export REGION=gro
API="https://api.virtscale.nl/v1/orgs/$VIRTSCALE_ORGANIZATION/compute"
curl -s "$API/limits" -H "Authorization: Bearer $VIRTSCALE_API_KEY"
curl -s "$API/storage-pools?region=$REGION" -H "Authorization: Bearer $VIRTSCALE_API_KEY"
curl -s "$API/network-profiles?region=$REGION" -H "Authorization: Bearer $VIRTSCALE_API_KEY"

Get limits returns the ranges for vcpus_steps, memory_gb and disk_gb. List storage pools and List network profiles return the id to use.

The virtscale_compute_limits data source returns the same grid to a configuration. memory_mb is memory_gb times 1024:

data "virtscale_compute_limits" "grid" {}

# The largest size the grid allows, as an output.
output "largest_size" {
  value = {
    vcpus     = max(data.virtscale_compute_limits.grid.vcpus_steps...)
    memory_mb = data.virtscale_compute_limits.grid.memory_gb.max * 1024
    disk_gb   = data.virtscale_compute_limits.grid.disk_gb.max
  }
}

The grid says what is valid, not what your quota has room for.

An instance_type, and the virtscale_instance_types data source, exist for when types can be ordered. Until then a type is refused with 422 instance_type_unpriced.

Put the network you will log in from in terraform.tfvars:

ssh_source_cidr = "<your network as an IPv4 CIDR, for example 203.0.113.0/24>"

The CIDR must have no host bits set: 203.0.113.0/24, not 203.0.113.5/24.

Save this as main.tf:

terraform {
  required_providers {
    virtscale = {
      source  = "registry.terraform.io/virtscale/virtscale"
      version = "~> 0.2"
    }
  }
}

# No credentials here: VIRTSCALE_API_KEY and VIRTSCALE_ORGANIZATION come from the environment.
provider "virtscale" {}

variable "region" {
  type        = string
  description = "The region to build in. The storage pool and network profile below are gro's."
  default     = "gro"
}

variable "ssh_source_cidr" {
  type        = string
  description = "The network SSH is allowed from, as an IPv4 CIDR."
}

variable "public_key_path" {
  type        = string
  description = "The OpenSSH public key you log in with."
  default     = "~/.ssh/id_ed25519.pub"
}

data "virtscale_images" "region" {
  region = var.region
}

locals {
  # The Debian 12 image, picked out of the catalogue by the distribution and the version it reports.
  debian_12_images = [
    for img in data.virtscale_images.region.images : img
    if img.os_distro == "debian" && img.os_version == "12"
  ]
}

# Imported once, taken by name below. cloud-init installs it for the image's default user (debian)
# when the instance first boots.
resource "virtscale_ssh_key" "laptop" {
  name       = "laptop"
  public_key = file(var.public_key_path)
}

resource "virtscale_instance" "web" {
  name            = "web-01"
  region          = var.region
  image           = local.debian_12_images[0].id
  vcpus           = 1
  memory_mb       = 1024 # 1 GB
  disk_gb         = 10
  storage_pool    = "gro-ceph"
  network_profile = "gro-tenant-default" # optional: the API picks the region's default
  ssh_keys        = [virtscale_ssh_key.laptop.name]

  lifecycle {
    # Stops the plan with the region's image names when there is not exactly one Debian 12 image.
    precondition {
      condition     = length(local.debian_12_images) == 1
      error_message = "Expected exactly one Debian 12 image in ${var.region}, found ${length(local.debian_12_images)}. The region offers: ${join(", ", [for img in data.virtscale_images.region.images : img.name])}. Change the filter in locals to pick one of them."
    }
  }
}

# The whole rule set of the instance: SSH from one network only.
resource "virtscale_instance_firewall" "web" {
  instance_id = virtscale_instance.web.id

  rule {
    direction   = "ingress"
    protocol    = "tcp"
    port_min    = 22
    port_max    = 22
    remote_cidr = var.ssh_source_cidr
    description = "ssh from the office"
  }
}

resource "virtscale_public_ip" "web" {
  region      = var.region
  instance_id = virtscale_instance.web.id
}

output "instance_id" {
  value = virtscale_instance.web.id
}

output "private_ipv4" {
  value = virtscale_instance.web.private_ipv4
}

output "public_ip" {
  value = virtscale_public_ip.web.address
}

Things to know about this file:

  • name is 1 to 63 lowercase letters, digits or hyphens, starting and ending with a letter or digit. It is unique among your live instances in a region. A deleted instance’s name can be used again.
  • vcpus, memory_mb and disk_gb set the size, and must be on the grid above. storage_pool names the pool. The pool and the network profile are read back into the state. Give the three sizes or an instance_type, not both.
  • The image is picked by the distribution and the version the catalogue reports, not by a name. If the region has no Debian 12 image, or more than one, the plan stops with the image names the region offers.
  • ssh_keys names keys by their virtscale_ssh_key name. At most 10, each once. The instance keeps the keys it booted with, even if you delete or replace the key resource later.
  • The firewall resource owns the instance’s whole rule set. A rule added in the panel shows up as a difference and the next apply removes it. The first apply replaces whatever rules the instance already has.
  • An organization may hold one public IP per region by default. An address in state error does not count.
  • workload_type is left out here, so it is customer, the default. infra is only for VirtScale’s own internal organizations: any other organization gets 422 workload_not_allowed. A different value replaces the instance.
tofu init
tofu plan
tofu apply

plan shows four resources to add. In tests against production the instance took 31 to 59 seconds to reach running. The provider waits for that before it moves on, and gives up after 20 minutes. If the instance ends in error, the apply fails at once with the platform’s reason.

ssh debian@$(tofu output -raw public_ip)

debian is the default user of the Debian images. Other images have their own, for example ubuntu.

user_data is plain cloud-init text: a #cloud-config document, a script that starts with #!, or a MIME multipart document, at most 32 KiB. It is sent when the instance is created. Add it to the instance:

resource "virtscale_instance" "web" {
  # ... the attributes from the configuration above ...

  user_data = <<-EOT
    #cloud-config
    packages:
      - htop
  EOT
}
  • cloud-init applies it after the platform’s own part, which installs the keys of ssh_keys.
  • Every new instance has a config drive, so cloud-init reads the keys and user_data without the metadata service.
  • user_data is sensitive: the plan hides it, but the state holds it as text. Keep the state in an encrypted backend, as Object storage explains for the S3 secret.
  • cloud-init reads it at the first boot only, so a different user_data replaces the instance.
  • The API never returns it. After an import the instance has none in the state, so add lifecycle { ignore_changes = [user_data] } to the block.
  • Production accepts user_data for every organization. Text that cannot be delivered is refused with 422 user_data_invalid, see Troubleshooting.

Some attributes change in place: hostname, disable_api_termination, and vcpus and memory_mb. Change any other (name, region, image, disk_gb, instance_type, storage_pool, network_profile, ssh_keys, user_data, workload_type) and the plan says must be replaced: the old instance and its disk are destroyed and a new one is created. A storage_pool or network_profile you left out is the platform’s choice and is never a change. Read the plan before you apply it.

The provider does not manage power state. An instance that you stop outside Terraform stays stopped, and the plan shows no change. Start it again where you stopped it.

Changing vcpus or memory_mb resizes the running instance in place, keeps its disk and restarts the instance: apply waits until it reports the new size. disk_gb, instance_type, storage_pool, network_profile and image still replace it.

In production resizing is off for customer organizations: the apply fails with 503 not_ready. It is on for VirtScale’s internal organizations.

disable_api_termination = true protects the instance, like AWS’s setting of that name:

resource "virtscale_instance" "web" {
  # ... the attributes from the configuration above ...

  disable_api_termination = true
}

While it is true, a destroy, or a change that replaces the instance, is refused with 409 vm_protected. Set it to false and apply first, then destroy or replace. The provider never lifts the protection for you. disable_api_termination changes in place, and changing it needs the compute:write scope (the permission compute.instance.update).

tofu destroy

Destroy clears the firewall rules, releases the public IP, deletes the instance and then removes the SSH key. Deleting the instance waits until it is gone, up to 10 minutes. In tests against production it took 12 to 16 seconds.

An instance with disable_api_termination = true stops the destroy with 409 vm_protected. Set it to false and apply first, see Protect an instance from deletion.

Object storage, or import what you already made.