Create your first Instance
This tutorial helps you create a Subnet and a Linux Instance that you sign in to with an SSH key, using a single configuration file. After this tutorial, you understand the full lifecycle of a resource in Terraform: look up, create, check, and delete.
Prerequisites
- ✅ You have completed Set up and configure:
terraform initruns successfully and the environment variables are declared. - ✅ Your VPC has enough quota for one Subnet and one Instance.
- ✅ You have an SSH key pair on your computer, for example
~/.ssh/id_rsaand~/.ssh/id_rsa.pub. If you do not have one, create it with thessh-keygen -t rsa -b 4096command.
FPT Cloud accepts public keys in OpenSSH format: RSA with 2048 bits or more, Ed25519, and ECDSA. See SSH Key reference.
View the options in your VPC
Images, flavors, and storage policies differ between Regions and VPCs. Review the lists for your VPC before you choose.
-
Create a
catalog.tffile in the project folder. Replaceexample-vpcwith the name of your VPC:data "fptcloud_vpc" "example" {name = "example-vpc"}data "fptcloud_image" "all" {vpc_id = data.fptcloud_vpc.example.id}data "fptcloud_storage_policy" "all" {vpc_id = data.fptcloud_vpc.example.id}output "images" {value = data.fptcloud_image.all.images[*].name}output "storage_policies" {value = data.fptcloud_storage_policy.all.storage_policies[*].name} -
Run
terraform applyand enteryes. -
Note the name of the Linux image and the name of the storage policy you want to use.
-
Delete the
catalog.tffile.
Write the configuration
Create a main.tf file with the three parts below.
Look up the VPC, image, flavor, and storage policy. The flavor is selected by number of vCPUs and amount of RAM, so the configuration works in any VPC that has a flavor with 2 vCPUs and 4 GB of RAM:
# main.tf
data "fptcloud_vpc" "example" {
name = "example-vpc"
}
# main.tf
data "fptcloud_image" "ubuntu" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["UBUNTU-22.04"] # Replace with the image name you noted in the previous step
match_by = "substring"
}
}
# main.tf
data "fptcloud_flavor" "small" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "cpu"
values = ["2"]
}
filter {
key = "memory_mb"
values = ["4096"]
}
}
# main.tf
data "fptcloud_storage_policy" "ssd" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["Premium-SSD"] # Replace with the storage policy name you noted
}
}
Create the Subnet and the Instance. Choose a CIDR range that does not overlap with the existing Subnets in the VPC:
# main.tf
resource "fptcloud_subnet" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "quickstart-subnet"
type = "NAT_ROUTED"
cidr = "10.20.1.0/24"
gateway_ip = "10.20.1.1"
}
resource "fptcloud_instance" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "quickstart-vm-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"
}
Output the results:
# main.tf
output "instance_id" {
value = fptcloud_instance.app.id
}
output "private_ip" {
value = fptcloud_instance.app.private_ip
}
A few things to know about the configuration above:
NAT_ROUTEDlets Instances in the Subnet reach the Internet outbound through a NAT gateway. UseISOLATEDfor a Subnet that does not need Internet access.ssh_keytakes the content of the public key itself, not the key name.storage_size_gbis the size of the root disk, in GB.
Create the resources
-
Preview the changes:
terraform planThe plan shows
Plan: 2 to add, 0 to change, 0 to destroy. -
Apply the changes:
terraform apply -
Enter
yesto confirm.
Terraform waits until the Instance is running, which usually takes a few minutes. When it finishes, Terraform prints instance_id and private_ip. The quickstart-vm-01 Instance appears in the Instance list on Portal with the Running status.
The Instance currently has only a private address. To access it from the Internet, attach a Security Group and a Floating IP as shown in Network examples.
Clean up
When you no longer need them, delete all the resources you just created:
terraform destroy
Enter yes to confirm. Terraform deletes the Instance first, then the Subnet.
terraform destroy permanently deletes the Instance and the data on its root disk. This action cannot be undone.