Complete These Tutorials First
A VM on CloudRaya lives inside a VPC (Virtual Private Cloud). In Provision a CloudRaya Virtual Machine with Terraform you attached a VM to a VPC that already existed.
In this guide, you create the network yourself with Terraform. You will create a VPC, two subnets, and the firewall rules (ACL) that protect them. Because you choose all the names, you do not need to look up a subnet name in the panel.
You need the same prerequisites as the first guide: a CloudRaya account, Terraform, and a project ID (see Find your Project ID). Set your credentials as environment variables in the same way:
$env:CLOUDRAYA_EMAIL = "you@example.com"
$env:CLOUDRAYA_PASSWORD = "your-password"Warning: Resources are billed like any other CloudRaya resource. Follow Clean up when you are done.
The three pieces
- VPC: A private network with a large address range, for example
10.10.32.0/19. - Subnet: A smaller part of the VPC, for example
10.10.32.0/25. VMs are attached to a subnet. - ACL: A list of firewall rules. It says which traffic can come in (ingress) and which traffic can go out (egress). Every subnet must have an ACL.
Creating a VPC in CloudRaya always creates its first subnet and first ACL with it. The provider calls them initial_subnet and initial_acl.
Choose the address range
You cannot choose any address for the VPC. CloudRaya accepts only certain private ranges for each region. To see them, open the panel and go to VPC → Add VPC. Pick your region, choose Add IP manually, and open the IP Address list. For Jakarta and size /19 it offers blocks such as:
10.10.0.0 - 10.10.31.255
10.10.32.0 - 10.10.63.255
10.10.64.0 - 10.10.95.255
...
10.10.224.0 - 10.10.255.255This guide uses 10.10.32.0/19. If you choose an address outside the offered blocks, the API answers Your IP address is outside the allowed network range for this region (HTTP 400).
Please Note: If one of your VPCs already uses a block, pick another one from the list.
Set up the project
Create a folder with these files.
providers.tf
terraform {
required_providers {
cloudraya = {
source = "cloudraya/cloudraya"
version = "~> 0.2"
}
}
}
provider "cloudraya" {
project_id = var.project_id
}variables.tf
variable "project_id" {
description = "ID of the CloudRaya project to build in."
type = string
}
variable "region_name" {
description = "Region name."
type = string
default = "Jakarta"
}
variable "name_prefix" {
description = "Prefix used to name the VPC, subnets and ACL."
type = string
default = "tutorial"
}
variable "vpc_ip_address" {
description = "Network address of the VPC. Must be one of the blocks the panel offers."
type = string
default = "10.10.32.0"
}
variable "source_cidr" {
description = "Who may connect to the VM. 0.0.0.0/0 means the whole internet."
type = string
default = "0.0.0.0/0"
}terraform.tfvars
project_id = "your-project-id"Define the network
Create main.tf:
data "cloudraya_region" "main" {
name = var.region_name
}
resource "cloudraya_vpc" "main" {
name = "${var.name_prefix}-vpc"
region_id = data.cloudraya_region.main.id
ip_address = var.vpc_ip_address
network_size = "19"
# The first subnet, created together with the VPC.
initial_subnet = {
name = "${var.name_prefix}-subnet"
ip_address = var.vpc_ip_address
network_size = "25"
}
# The first ACL, created together with the VPC. These rules mirror the
# default ACL that the CloudRaya panel creates.
initial_acl = {
name = "${var.name_prefix}-acl"
rules = [
# --- Ingress: traffic coming into the subnet ---
{
protocol = "TCP"
action = "Allow"
source_cidr = var.source_cidr
traffic_type = "Ingress"
start_port = "22" # SSH
end_port = "22"
},
{
protocol = "TCP"
action = "Allow"
source_cidr = var.source_cidr
traffic_type = "Ingress"
start_port = "3389" # RDP (Windows remote desktop)
end_port = "3389"
},
{
protocol = "TCP"
action = "Allow"
source_cidr = var.source_cidr
traffic_type = "Ingress"
start_port = "80" # HTTP
end_port = "80"
},
{
protocol = "TCP"
action = "Allow"
source_cidr = var.source_cidr
traffic_type = "Ingress"
start_port = "443" # HTTPS
end_port = "443"
},
# --- Egress: traffic leaving the subnet ---
{
protocol = "TCP"
action = "Allow"
source_cidr = var.source_cidr # for egress rules this is the destination
traffic_type = "Egress"
start_port = "1"
end_port = "65535"
},
{
protocol = "UDP"
action = "Allow"
source_cidr = var.source_cidr
traffic_type = "Egress"
start_port = "1"
end_port = "65535"
},
]
}
}
# A second subnet in the same VPC. Every subnet needs an ACL, so reuse the one
# created with the VPC.
resource "cloudraya_vpc_network" "app" {
vpc_id = cloudraya_vpc.main.id
network_acl_id = cloudraya_vpc.main.initial_acl_id
name = "${var.name_prefix}-subnet-2"
description = "second subnet"
ip_address = "10.10.33.0"
network_size = "24"
}Some points about this file:
- Sizes are strings.
network_size = "19"has quotes. The provider expects text, not a number. - Subnets must stay inside the VPC range. The VPC
10.10.32.0/19covers10.10.32.0to10.10.63.255. So the subnets10.10.32.0/25and10.10.33.0/24both fit. - For egress rules,
source_cidris the destination. The provider uses the same field name for the address in every rule.0.0.0.0/0means "anywhere". - Port 3389 is for Windows remote desktop. It is in the file because the default ACL in the panel has it. If your network is only for Linux, you can remove this rule.
- References set the order.
cloudraya_vpc_network.appusescloudraya_vpc.main.id, so Terraform creates the VPC first.
Warning:
0.0.0.0/0means anyone on the internet can try to connect. If this is more than a test, allow only your own network to connect to the VM. Setsource_cidrto your public IP address, followed by/32, for example203.0.113.10/32.Note:
/32means exactly one address. Many home internet connections change IP address from time to time. If you cannot connect with SSH one day, check your IP again and updatesource_cidr.
What "initial" means
The documentation says of initial_subnet and initial_acl: used at create time only: it is never read back or reconciled, and editing it forces a new VPC. In plain words:
- Terraform uses these two blocks one time, when it creates the VPC.
- Afterwards it does not look at them again. If you change an ACL rule in the panel, Terraform does not notice and does not undo it.
- If you edit the blocks in your
.tffile, Terraform plans to delete the VPC and create a new one, because it cannot change them in place.
So decide the first subnet and the ACL rules before your first apply. To add more subnets, use cloudraya_vpc_network, like the example above. There is no Terraform resource for an ACL on its own yet. So if you want to change ACL rules later, do it in the panel.
Show the results
Create a file named outputs.tf:
output "vpc_id" {
value = cloudraya_vpc.main.id
}
output "first_subnet_id" {
value = cloudraya_vpc.main.initial_subnet_id
}
output "acl_id" {
value = cloudraya_vpc.main.initial_acl_id
}
output "second_subnet_id" {
value = cloudraya_vpc_network.app.id
}
output "second_subnet_cidr" {
value = cloudraya_vpc_network.app.network_address
}
output "second_subnet_gateway" {
value = cloudraya_vpc_network.app.gateway
}Build the network
terraform init
terraform planThe plan ends with:
Plan: 2 to add, 0 to change, 0 to destroy.The two items are the VPC and the second subnet. Run terraform apply and type yes. CloudRaya builds the VPC in the background, and Terraform waits until it is ready.
In the panel, open VPC, then open your VPC. You should see:
- Network Address
10.10.32.0/19. - On the Subnet tab, two subnets:
tutorial-subnet(10.10.32.0/25) andtutorial-subnet-2(10.10.33.0/24). Both use the ACLtutorial-acl. - On the Access List tab, click Detail on
tutorial-aclto see the ingress and egress rules.
Use the network for a VM
Your first subnet is named tutorial-subnet. To put a VM in it, use the VM project from the first guide. Open its terraform.tfvars and change this value:
network_name = "tutorial-subnet"Then run terraform plan. The VM should show as changed in place (~), not destroyed and created again (-/+). If the plan looks right, run terraform apply.
The two projects do not know about each other. When you clean up, destroy the VM project first, then this one.
Clean up
terraform destroyType yes to confirm. The VPC cannot be removed while a VM is still attached to one of its subnets, so destroy any VM in it first.
Warning: Removing a VPC also removes its subnets and ACL.
Troubleshooting
Your IP address is outside the allowed network range for this region (HTTP 400)
The VPC address is not one of the blocks CloudRaya offers for the region. Open Add VPC in the panel and pick an address from the IP Address list (see Choose the address range).
Terraform wants to replace the VPC after you edited initial_subnet or initial_acl
This is expected, see What "initial" means. Revert the edit, or accept that the VPC is destroyed and created again.
