How to Provision a CloudRaya Virtual Machine with Terraform

How-tointermediateTime to complete:15 to 45 minutes
  • By Tiyan Wisesa
  • Updated November 26, 2026
  • Requires VM
  • Tested on Terraform 1.x, CloudRaya provider 0.2
On this page

Infrastructure as Code (IaC) means describing your servers, disks and networks in text files instead of clicking through a control panel. Terraform reads those files and makes CloudRaya match them. This gives you three things:

  • Speed: one command builds everything.
  • Repeatability: the same files produce the same result every time.
  • Safety: you see exactly what will change before anything is touched.

In this guide you use the CloudRaya Terraform provider to create an SSH key pair, a virtual machine (VM) and a data disk attached to that VM, and then log in to the VM over SSH. Along the way you learn how a Terraform project is laid out and how its files connect.

Terraform works in three steps, and you repeat them whenever you change something:

  1. Write: describe the infrastructure in .tf files.
  2. Plan: Terraform shows what it would create, change or destroy. Nothing happens yet.
  3. Apply: Terraform makes the changes in CloudRaya.

Prerequisites

Before you begin, you need:

  • A CloudRaya account.
  • Terraform 1.0 or later. Check with terraform version.
  • An SSH key pair on your computer. If you do not have one, create it:
bash
ssh-keygen -t ed25519

This creates ~/.ssh/id_ed25519.pub, the public key that Terraform uploads to CloudRaya.

  • A project ID and a subnet name. The next two sections show where to find them. They are the only values you have to look up yourself. Everything else is found by name from the code, or created by it.
Warning: The resources in this guide are real and are billed like any other CloudRaya resource. Follow Clean up when you are done.

Find your project ID

A CloudRaya project is the container that owns your resources. Terraform needs its ID, not its name. The panel shows only the name, and an account can have several projects with the same name (for example more than one "Default Project"), so you ask the API for the list.

Log in and list your projects. In PowerShell:

powershell
$base  = "https://api-v2.cloudraya.com"
$pass  = Read-Host "Password" -AsSecureString
$plain = [System.Net.NetworkCredential]::new("", $pass).Password

$login = Invoke-RestMethod -Method Post -Uri "$base/core/v1/auth/login" `
  -ContentType "application/json" `
  -Body (@{ email = "you@example.com"; password = $plain } | ConvertTo-Json)
$token = if ($login.data.token) { $login.data.token } else { $login.token }

$res = Invoke-RestMethod -Uri "$base/core/v1/projects" -Headers @{ Authorization = "Bearer $token" }
$projects = if ($res.data.projects) { $res.data.projects } else { $res.projects }
$projects | Select-Object id, name, is_default, is_owner | Format-Table

Or with curl and jq on Linux and macOS:

bash
TOKEN=$(curl -s -X POST https://api-v2.cloudraya.com/core/v1/auth/login \
    -H "Content-Type: application/json" \
    -d '{"email":"you@example.com","password":"your-password"}' | jq -r '.data.token // .token')
curl -s https://api-v2.cloudraya.com/core/v1/projects \
    -H "Authorization: Bearer $TOKEN" | jq '.data.projects // .projects | map({id, name, is_default, is_owner})'

The result looks like this:

text
id                         name                  is_default is_owner
--                         ----                  ---------- --------
01abc...                   Default Project                1        1
01def...                   my-other-project               0        0

The is_default project is the one CloudRaya created with your account, and is_owner shows whether you own the project. Copy the id of the project you want to build in.

Find your subnet name

A VM must be attached to a subnet, which lives inside a VPC (virtual private cloud). This guide uses a subnet that already exists. In the panel, open VPC, open your VPC, and copy the name from the Name column of the Subnet table. It looks like default-vpcnet-123456.

Please Note: You need the name of the subnet, not the name of the VPC. The page title shows the VPC (for example default-vpc-123456). The name you need is in the table below it (for example default-vpcnet-123456). Mixing them up is the most common mistake. See Troubleshooting.

If you do not have a VPC yet (the VPC page says "There is no vpc yet"), create one first. CloudRaya also creates a default VPC the first time you successfully create a VM from the panel, so a new account may have none. You can either use Add VPC in the panel, or build one with Terraform by following Build a VPC with Terraform and coming back here.

How a Terraform project is organized

Terraform reads every .tf file in a folder and combines them into one configuration. You can name your files anything you like. Terraform loads every .tf file in the same folder and treats them as one configuration. People split the code into several files only to make it easier to read. This guide uses a common layout:

  • providers.tf: Which provider to use
  • variables.tf: The inputs of the project
  • main.tf: The resources and lookups that make up the infrastructure
  • outputs.tf: What Terraform prints when it finishes

For a small project you could put everything in one file and it would behave the same.

The building blocks

A Terraform file is made of blocks. Here is a simple example:

hcl
data "cloudraya_region" "main" {
  name = var.region_name
}

Every part of this block is decided by someone different: Terraform, the provider, or you. Here is who decides what:

  • data is decided by Terraform. It is a fixed word that tells Terraform what kind of block this is. The other block types are resource, variable, output, provider and terraform.
  • "cloudraya_region" is decided by the provider. It is the type of thing you want to read or create. It exists because the CloudRaya provider defines it.
  • "main" is decided by you. It is a name you choose, so you can refer to this block later, for example as data.cloudraya_region.main.id.
  • name is decided by the provider. Each type has its own list of allowed settings (called arguments). You can find the list in the provider documentation.
  • var.region_name is decided by you. It is the value of one of your own variables.

You will use two types of blocks most often:

  • A data block reads information that already exists in CloudRaya. For example, it can find the ID of the region called Jakarta. It does not create or change anything.
  • A resource block creates and manages a service in CloudRaya. For example, it can create a virtual machine (VM). Later, it can also update or delete that VM.

Set up the project

Create a folder and go into it:

bash
mkdir cloudraya-vm
cd cloudraya-vm

Set your credentials

The provider needs your CloudRaya e-mail and password to log in. You may set them as environment variables, so you do not need to write them in any file.

On Linux or macOS:

bash
export CLOUDRAYA_EMAIL="you@example.com"
export CLOUDRAYA_PASSWORD="your-password"

On Windows PowerShell:

powershell
$env:CLOUDRAYA_EMAIL = "you@example.com"
$env:CLOUDRAYA_PASSWORD = "your-password"

These values only last for the current terminal window. If you open a new window, set them again.

Configure the provider

Create a file named providers.tf:

hcl
terraform {
  required_providers {
    cloudraya = {
      source  = "cloudraya/cloudraya"
      version = "~> 0.2"
    }
  }
}

provider "cloudraya" {
  project_id = var.project_id
}

The terraform block tells Terraform which provider to download and which versions it can use. Here, ~> 0.2 means version 0.2 or higher, but lower than 1.0. For example, 0.2 and 0.9 are allowed, but 1.0 is not.

The provider block sets which CloudRaya project Terraform uses. Everything you create goes into this project.

Declare the variables

Create a file named variables.tf. It holds the inputs of the project. If a variable has a default value, you do not have to set it. Terraform uses the default. If a variable has no default value, you must give it a value yourself, for example in the terraform.tfvars file. You will create that file in a later step.

hcl
variable "project_id" {
  description = "ID of the CloudRaya project to build in."
  type        = string
}

variable "network_name" {
  description = "Name of the existing subnet (not the VPC) to attach the VM to."
  type        = string
}

variable "region_name" {
  description = "Region name."
  type        = string
  default     = "Jakarta"
}

variable "package_name" {
  description = "VM package (size) name. Must be offered in the region."
  type        = string
  default     = "Small-R2"
}

variable "template_name" {
  description = "OS template name. Must be offered in the region."
  type        = string
  default     = "Ubuntu 22.04 v05.22"
}

variable "disk_package_name" {
  description = "Data disk package name."
  type        = string
  default     = "Disk-50"
}

variable "hostname" {
  description = "Name of the VM. Also used to name the key pair and the disk."
  type        = string
  default     = "web-01"
}

variable "ssh_public_key_path" {
  description = "Path to the public key to authorize on the VM."
  type        = string
  default     = "~/.ssh/id_ed25519.pub"
}

project_id and network_name have no default value, because every account has different values. The other variables have defaults. Change them only if you want something different.

Provide your values

Create a file named terraform.tfvars. Terraform loads it automatically. Add the two values you collected:

hcl
project_id   = "your-project-id"
network_name = "your-subnet-name"

These are identifiers, not secrets, but they are specific to your account. If your folder is a Git repository, keep this file and Terraform's working files out of version control. Create a .gitignore:

text
.terraform/
*.tfstate
*.tfstate.*
crash.log
*.tfvars
Warning: The state files (terraform.tfstate) store details of everything Terraform manages, as plain text. Treat them as sensitive. Never commit or share them.

Terraform can get the value of a variable from several places. If you set the same variable in more than one place, Terraform uses the last one in this list:

  1. The default in variables.tf
  2. Environment variables named TF_VAR_<variable>
  3. terraform.tfvars and *.auto.tfvars
  4. -var and -var-file on the command line

For example, a value you pass with -var replaces the same value in terraform.tfvars.

Define the infrastructure

Create a file named main.tf. It has two parts: lookups first, then resources.

Look up IDs by name

The CloudRaya API identifies everything by ID, but you only know names. A data block finds the ID for a name:

hcl
data "cloudraya_region" "main" {
  name = var.region_name
}

data "cloudraya_package" "vm" {
  name      = var.package_name
  region_id = data.cloudraya_region.main.id
}

data "cloudraya_template" "os" {
  name      = var.template_name
  region_id = data.cloudraya_region.main.id
}

data "cloudraya_vpc_network" "net" {
  name = var.network_name
}

data "cloudraya_vm_storage_package" "disk" {
  name = var.disk_package_name
}

The package and the template need a region_id, because each region offers its own packages and templates. So Terraform finds the region first, then looks for the package and the template in that region. If a name does not exist in that region, terraform plan stops and tells you. It does not fail in the middle of the deployment.

Note: Names must match exactly. This includes capital letters, spaces and dashes. Ubuntu 22.04 v05.22 and ubuntu 22.04 are two different names to Terraform. The panel shows the region as ID - Jakarta, but the name to use is Jakarta.

Create the key pair, the VM and the disk

Add these resources to the same file:

hcl
resource "cloudraya_ssh_keypair" "deploy" {
  name       = "${var.hostname}-key"
  public_key = file(pathexpand(var.ssh_public_key_path))
}

resource "cloudraya_vm" "web" {
  hostname        = var.hostname
  region_id       = data.cloudraya_region.main.id
  package_id      = data.cloudraya_package.vm.id
  template_id     = data.cloudraya_template.os.id
  network_id      = data.cloudraya_vpc_network.net.id
  ssh_keypair_ids = [cloudraya_ssh_keypair.deploy.id]
  note            = "managed by terraform"
}

resource "cloudraya_vm_storage" "data" {
  name               = "${var.hostname}-data"
  region_id          = data.cloudraya_region.main.id
  product_id         = data.cloudraya_vm_storage_package.disk.id
  virtual_machine_id = cloudraya_vm.web.id
}

Why does region_id use data.cloudraya_region.main.id and not var.region_name? Because region_id needs an ID, and var.region_name is a name. Here is a simple way to remember it. Arguments that end in _id need an ID, and the ID comes from a data block or from another resource. Other text arguments, like hostname, can use a variable directly.

You do not need to put these blocks in order. Terraform reads the references between them and works out the order by itself.

The VM needs the IDs from the lookups and the key pair, so Terraform creates those first. The disk needs the VM, so it is created last. Items that do not depend on each other are created at the same time. When you destroy, Terraform goes in the opposite order.

Show the results

Create a file named outputs.tf:

hcl
output "vm_id" {
  value = cloudraya_vm.web.id
}

output "vm_state" {
  value = cloudraya_vm.web.state
}

output "vm_cpu" {
  value = cloudraya_vm.web.cpu_number
}

output "vm_ram_gb" {
  value = cloudraya_vm.web.ram_gb
}

output "data_disk_gb" {
  value = cloudraya_vm_storage.data.disk_size_gb
}

Outputs print some attributes of what Terraform built. The attributes of each resource are listed in the provider documentation.

Build the infrastructure

Initialize

bash
terraform init

Terraform downloads the CloudRaya provider from the registry and writes .terraform.lock.hcl, which pins the exact provider version. Commit that file so everyone uses the same version. The output ends with:

text
Terraform has been successfully initialized!

Check the plan

bash
terraform plan

The plan lists everything Terraform would do, without doing it. Read it before you continue. For this project the summary line is:

text
Plan: 3 to add, 0 to change, 0 to destroy.

The three items are the key pair, the VM and the data disk.

Apply

bash
terraform apply

Terraform shows the plan again and asks for confirmation. Type yes. CloudRaya builds resources in the background and Terraform waits for each one to become active, so a VM takes about one to three minutes. When it finishes, the outputs are printed:

text
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

Outputs:

data_disk_gb = "50.000"
vm_cpu = 1
vm_id = "<your vm id>"
vm_ram_gb = 2
vm_state = "running"

The values depend on the package and disk you chose. Open the CloudRaya panel and you will see the new VM and its disk.

Log in to the VM

  1. In the panel, open Virtual Machine, find your VM, on the Overview tab, find its public IP address.
  2. Connect with SSH:
bash
ssh root@<your-VM-IP>

The first time, SSH asks you to confirm the server's fingerprint; type yes.

The Ubuntu 22.04 v05.22 template uses root as the user. Your key pair is already authorized.

Change an existing resource

Terraform compares your files with what it recorded in the state. Edit a value, run plan, and it shows only the difference. You can also override a variable for a single run without editing any file:

bash
terraform plan -var="hostname=web-02"

Terraform marks each line of a change:

  • + will be created
  • ~ will be changed in place
  • -/+ will be destroyed and created again
  • - will be destroyed

According to the provider documentation, changes to the VM behave like this:

  • hostname: Renamed in place
  • package_id: The VM is scaled in place
  • network_id: The VM is moved in place
  • ssh_keypair_ids, note: Updated in place
  • template_id, region_id, project_id: A new VM is created
Warning: A change to template_id destroys the VM and its root disk. Always check the plan for -/+ or - lines before you apply. The SSH key pair has no update operation, so changing any of its arguments also replaces it.

The data disk follows the same rules: a different product_id resizes it in place, while a different name or region_id creates a new disk. If you set, clear or change virtual_machine_id, the disk is attached or detached. It is not deleted.

Clean up

To remove everything you created in this guide:

bash
terraform destroy

Type yes to confirm. Terraform removes the resources in the reverse order of creation: the data disk first, then the VM, then the key pair. Detaching the disk takes a moment. The provider waits for it to finish before it deletes the disk.

Warning: terraform destroy permanently deletes the VM and the data on its disks.

By default, the public IP of the VM is released when the VM is destroyed. To keep it, set keep_ip_on_delete = true on the cloudraya_vm resource, and apply that change before you destroy.

Always delete these resources with terraform destroy, not in the panel. If you delete the VM or the subnet in the panel, the Terraform state no longer matches what really exists. A later destroy can fail. The data disk and the key pair are not deleted, and you keep paying for them until you delete them by hand.

Troubleshooting

... not found for a region, package, template or disk package

The name does not match exactly. Copy it from the panel, including spaces, dashes and capitalization. For a package or template, also check that it is offered in your region.

Missing project

Terraform could not find a project ID. Check that project_id is set in terraform.tfvars or passed with -var.

Network not found or VPC network not found

text
No VPC network named "default-vpc-123456" in project <your project id>.

You probably entered the VPC name instead of the subnet name. Open the VPC in the panel and copy the name from the Subnet table (for example default-vpcnet-123456). Also check that the project in the message is the one you expect, and that the subnet still exists.

Terraform asks Enter a value for a variable

The variable has no default and no value was found. Check that terraform.tfvars sits in the same folder as the .tf files.

Missing e-mail or password

Environment variables only exist in the terminal window where you set them. Set them again in the window where you run Terraform.

The deployment is rolled back

Sometimes CloudRaya cancels a VM deployment because there is not enough capacity or quota. The provider shows this in the error message. Try another package or region.

Conclusion

In this guide, you used Terraform to create an SSH key pair, a VM and a data disk on CloudRaya. You logged in to the VM and saw how the files work together. Variables give values to the provider and the resources. data blocks find IDs from names. Resources refer to each other, so Terraform knows the order. Outputs show the result. You also learned the everyday steps: plan, apply and destroy.

Next steps

About the author

Tiyan Wisesa

Product Technical Writer at CloudRaya. Writes and tests the CloudRaya Tutorials, with a focus on clear steps that anyone can follow.