Skip to content

Terraform examples

Each example is a complete main.tf; add the provider setup next to it.

Check quota and price first

# Check headroom and price before creating anything.
data "rso_quota" "current" {}

data "rso_cost_estimate" "small_vm" {
  instance_vcpu    = 1
  instance_ram_gb  = 2
  instance_disk_gb = 60
}

output "vcpu_remaining" {
  value = data.rso_quota.current.remaining["vcpu"]
}

output "small_vm_monthly" {
  value = "${data.rso_cost_estimate.small_vm.monthly} ${data.rso_cost_estimate.small_vm.currency}"
}

A serverless app

# A serverless app from a public image, always keeping one instance warm.
resource "rso_app" "hello" {
  name  = "tf-example-hello"
  image = "docker.io/nginxinc/nginx-unprivileged:stable"
  port  = 8080

  env = {
    TARGET = "Terraform"
  }

  cpu_request    = "50m"
  memory_request = "64Mi"
  cpu_limit      = "250m"
  memory_limit   = "128Mi"

  min_scale = 1
  max_scale = 3
}

output "app_url" {
  value = rso_app.hello.url
}

A virtual machine

# A virtual machine from an OS template, reachable with your SSH key.
data "rso_instance_templates" "all" {}

variable "template_name" {
  description = "Name of the OS template to clone."
  type        = string
  default     = "ubuntu-26.04"
}

variable "ssh_public_key" {
  description = "SSH public key installed for the default user."
  type        = string
  default     = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyReplaceMeWithYourOwnPublicKey000 example"
}

resource "rso_instance" "vm" {
  name           = "tf-example-vm"
  template       = data.rso_instance_templates.all.by_name[var.template_name]
  cpus           = 1
  memory_mb      = 1024
  root_disk_gb   = 20
  ssh_public_key = var.ssh_public_key
}

output "instance_vmid" {
  value = rso_instance.vm.vmid
}

A PostgreSQL cluster

# A managed PostgreSQL cluster with one primary and one replica.
resource "rso_postgres_cluster" "db" {
  name         = "tf-example-db"
  instances    = 2
  storage_size = "5Gi"

  cpu_request    = "100m"
  memory_request = "256Mi"
  cpu_limit      = "500m"
  memory_limit   = "512Mi"
}

output "postgres_host" {
  value = rso_postgres_cluster.db.host
}

A container registry

# A private container registry. Push with:
#   docker login <domain> -u <service account> -p <token>
resource "rso_docker_registry" "images" {
  name = "tf-example-images"
}

output "registry_domain" {
  value = rso_docker_registry.images.domain
}

Queues and topics

# A queue fed by a topic, a webhook for the same topic, a message published on
# a schedule and an AMQP login for a worker inside the cluster.
resource "rso_queue" "orders" {
  name = "tf-example-orders"
}

resource "rso_topic" "events" {
  name = "tf-example-events"
}

# Only messages whose routing key starts with "order." reach the queue.
resource "rso_topic_subscription" "orders" {
  topic  = rso_topic.events.name
  queue  = rso_queue.orders.name
  filter = "order.*"
}

variable "webhook_secret" {
  type      = string
  sensitive = true
  default   = "change-me"
}

# Every message of the topic is also POSTed to a URL, signed with the secret.
resource "rso_queue_webhook" "notify" {
  name   = "tf-example-notify"
  topic  = rso_topic.events.name
  url    = "https://hooks.example.com/rso"
  secret = var.webhook_secret
}

# A heartbeat published to the topic every hour.
resource "rso_scheduled_message" "heartbeat" {
  name         = "tf-example-heartbeat"
  schedule     = "0 * * * *"
  topic        = rso_topic.events.name
  routing_key  = "system.heartbeat"
  body         = jsonencode({ type = "heartbeat" })
  content_type = "application/json"
}

resource "rso_queue_credential" "worker" {
  name = "tf-example-worker"
}

data "rso_queues" "this" {}

output "http_endpoint" {
  value = data.rso_queues.this.http_endpoint
}

output "amqp_uri" {
  value     = rso_queue_credential.worker.uri
  sensitive = true
}

A scheduled job

# A container that runs every night at 02:30 UTC.
resource "rso_scheduled_job" "report" {
  name      = "tf-example-report"
  image     = "busybox:1.36"
  command   = ["sh", "-c"]
  args      = ["echo building the nightly report; date -u"]
  schedule  = "30 2 * * *"
  cpu       = 0.25
  memory_mb = 128

  env = [
    { name = "REPORT_FORMAT", value = "csv" },
  ]
}

A bucket that announces uploads

# Every image uploaded under uploads/ announces itself on a queue.
resource "rso_bucket" "media" {
  name = "tf-example-media"
}

resource "rso_queue" "uploads" {
  name = "tf-example-uploads"
}

resource "rso_bucket_notification" "images" {
  bucket     = rso_bucket.media.name
  events     = ["CREATED"]
  key_prefix = "uploads/"
  key_suffix = ".jpg"
  queue      = rso_queue.uploads.name
}

A service account for CI

# A machine identity for CI. The token is only available at creation.
resource "rso_service_account" "ci" {
  name       = "tf-example-ci"
  expires_at = "2027-06-30T00:00:00Z"
}

output "ci_token" {
  value     = rso_service_account.ci.token
  sensitive = true
}