Skip to main content

Instance examples

An Instance is a virtual server that runs on FPT Cloud. This page gives Terraform examples for common scenarios, from a single Instance to a group of Instances spread across several physical servers. For Instance concepts, see Instance overview.

Prerequisites​

Shared configuration​

The examples on this page share the lookup blocks and the Subnet below. Save them in a file named shared.tf:

# shared.tf
data "fptcloud_vpc" "example" {
name = "example-vpc"
}
# shared.tf
data "fptcloud_image" "ubuntu" {
vpc_id = data.fptcloud_vpc.example.id

filter {
key = "name"
values = ["UBUNTU-22.04"]
match_by = "substring"
}
}
# shared.tf
data "fptcloud_flavor" "small" {
vpc_id = data.fptcloud_vpc.example.id

filter {
key = "cpu"
values = ["2"]
}
filter {
key = "memory_mb"
values = ["4096"]
}
}
# shared.tf
data "fptcloud_storage_policy" "ssd" {
vpc_id = data.fptcloud_vpc.example.id

filter {
key = "name"
values = ["Premium-SSD"]
}
}
# shared.tf
resource "fptcloud_subnet" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "app-subnet"
type = "NAT_ROUTED"
cidr = "10.10.1.0/24"
gateway_ip = "10.10.1.1"
}

Linux Instance with an SSH key​

This is the most common configuration: a Linux Instance that you log in to with an SSH key. The ssh_key argument takes the content of the public key itself.

# instance.tf
resource "fptcloud_instance" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "app-server-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}

Register an SSH key on FPT Cloud​

Register the public key with fptcloud_ssh_key so that the key appears in Compute Engine → SSH Keys on the Portal, then reuse it for several Instances:

# ssh.tf
resource "fptcloud_ssh_key" "deployer" {
name = "deployer-key"
public_key = file(pathexpand("~/.ssh/id_rsa.pub"))
}

resource "fptcloud_instance" "worker" {
vpc_id = data.fptcloud_vpc.example.id
name = "worker-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = fptcloud_ssh_key.deployer.public_key
status = "POWERED_ON"
}

If the key is already registered on the Portal, read the key by name and use its public_key:

# ssh.tf
data "fptcloud_ssh_key" "team" {
name = "team-key"
}

Then set ssh_key = data.fptcloud_ssh_key.team.public_key on the Instance.

warning

Changing the ssh_key of a running Instance deletes and recreates the Instance. If you need to replace the key, update it directly in the operating system of the Instance instead of editing the Terraform configuration.

Instance with a password​

Use password instead of ssh_key when you log in with a password. Pass the password through a variable with sensitive = true so that Terraform does not print it on the screen:

# windows.tf
variable "admin_password" {
type = string
sensitive = true
}

data "fptcloud_image" "windows" {
vpc_id = data.fptcloud_vpc.example.id

filter {
key = "name"
values = ["WINSRV-STD2022"]
match_by = "substring"
}
}
# windows.tf
resource "fptcloud_instance" "windows" {
vpc_id = data.fptcloud_vpc.example.id
name = "win-server-01"
image_name = data.fptcloud_image.windows.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 80
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
password = var.admin_password
status = "POWERED_ON"
}

Provide the password through an environment variable when you run the command:

export TF_VAR_admin_password="YOUR_STRONG_PASSWORD"
terraform apply
warning

Each Instance uses exactly one of the two arguments ssh_key or password. The password is still stored in the state, so protect the state file carefully.

Instance with a fixed private IP​

Set private_ip when other systems need to reach the Instance at a known address. The address must be inside the CIDR range of the Subnet and must not be in use:

# fixed-ip.tf
resource "fptcloud_instance" "db" {
vpc_id = data.fptcloud_vpc.example.id
name = "db-server-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
private_ip = "10.10.1.50"
storage_size_gb = 100
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}

private_ip is used only when the Instance is created. Changing this value after creation does not change the IP of the Instance.

Several identical Instances​

Use count to create several Instances with the same configuration. The names are numbered automatically:

# web.tf
variable "web_count" {
type = number
default = 3
}

resource "fptcloud_instance" "web" {
count = var.web_count

vpc_id = data.fptcloud_vpc.example.id
name = format("web-server-%02d", count.index + 1)
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}

This configuration creates web-server-01, web-server-02 and web-server-03. Increase web_count to add Instances without affecting the running Instances.

Several Instances with different configurations​

Use for_each with a map when each Instance has its own role and size:

# servers.tf
locals {
servers = {
api = { disk_gb = 40 }
worker = { disk_gb = 80 }
report = { disk_gb = 120 }
}
}
# servers.tf
resource "fptcloud_instance" "server" {
for_each = local.servers

vpc_id = data.fptcloud_vpc.example.id
name = "${each.key}-server"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = each.value.disk_gb
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}
tip

With for_each, each Instance has a stable address such as fptcloud_instance.server["api"]. Removing an element from the map deletes only that Instance, unlike count, which can shift the indexes of the Instances that follow.

Instance Group spread across several physical servers​

An Instance Group with the Soft Anti Affinity policy prefers to place the Instances on different physical servers, so that one hardware failure does not stop the whole group. According to Instance Group overview, a group needs 2 to 10 Instances that do not belong to any other group.

Look up the policy and create a group for the Instances in the web example above:

# group.tf
data "fptcloud_instance_group_policy" "anti_affinity" {
vpc_id = data.fptcloud_vpc.example.id

filter {
key = "name"
values = ["Soft Anti Affinity"]
}
}

resource "fptcloud_instance_group" "web" {
vpc_id = data.fptcloud_vpc.example.id
name = "web-group"
policy_id = data.fptcloud_instance_group_policy.anti_affinity.instance_group_policies[0].id
vm_ids = fptcloud_instance.web[*].id
}
warning

Changing any argument of fptcloud_instance_group recreates the group, including vm_ids. Adding Instances to or removing Instances from vm_ids deletes the old group and creates a new one.

Instance powered off until needed​

The status argument sets the power state of the Instance. Set POWERED_OFF for an Instance that only needs to run when there is work to do, for example a test machine:

# test-box.tf
variable "test_box_running" {
type = bool
default = false
}

resource "fptcloud_instance" "test_box" {
vpc_id = data.fptcloud_vpc.example.id
name = "test-box-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = var.test_box_running ? "POWERED_ON" : "POWERED_OFF"
}

Power on the Instance with terraform apply -var="test_box_running=true".

Read the private IP of an Instance​

A newly created Instance may not report its private IP for a short time. Read the Instance again with a data source to get the IP, for example when you attach a Security Group:

# ip.tf
data "fptcloud_instance" "app" {
vpc_id = data.fptcloud_vpc.example.id
id = fptcloud_instance.app.id
}

output "app_private_ip" {
value = data.fptcloud_instance.app.private_ip
}

If app_private_ip is empty after the first apply, run terraform apply again.

Common arguments​

ArgumentRequiredWhen changed
vpc_idYesRecreates the Instance.
nameYesRenames in place. The name must not contain spaces.
image_nameYesRecreates the Instance.
subnet_idYesRecreates the Instance.
flavor_nameRecommendedResizes in place. See Change and scale your infrastructure.
storage_size_gbYesIncreases the root disk size in place. It cannot be decreased.
storage_policy_idYesMoves the root disk to the new storage policy in place.
statusYesPowers the Instance on or off in place. Values: POWERED_ON, POWERED_OFF.
ssh_key or passwordYes, choose oneRecreates the Instance.
private_ipNoUsed only at creation.
tag_idsNoUpdates tags in place. See Tag examples.

For the full list of arguments, see fptcloud_instance in the provider reference documentation.

note

The root disk belongs to the Instance and is managed with storage_size_gb and storage_policy_id. Do not declare fptcloud_storage for the root disk. To attach a Security Group, use the apply_to argument of the Security Group as shown in Network examples.

Next steps​