Skip to main content

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 init runs 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_rsa and ~/.ssh/id_rsa.pub. If you do not have one, create it with the ssh-keygen -t rsa -b 4096 command.
note

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.

  1. Create a catalog.tf file in the project folder. Replace example-vpc with 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
    }
  2. Run terraform apply and enter yes.

  3. Note the name of the Linux image and the name of the storage policy you want to use.

  4. Delete the catalog.tf file.

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_ROUTED lets Instances in the Subnet reach the Internet outbound through a NAT gateway. Use ISOLATED for a Subnet that does not need Internet access.
  • ssh_key takes the content of the public key itself, not the key name.
  • storage_size_gb is the size of the root disk, in GB.

Create the resources​

  1. Preview the changes:

    terraform plan

    The plan shows Plan: 2 to add, 0 to change, 0 to destroy.

  2. Apply the changes:

    terraform apply
  3. Enter yes to 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.

tip

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.

danger

terraform destroy permanently deletes the Instance and the data on its root disk. This action cannot be undone.

Next steps​