Look up information with data sources
This page shows you how to look up information with data sources, the foundation of every example in the Resource examples section. Looking up values with data sources instead of copying IDs by hand lets your configuration work in another VPC without changes.
Prerequisites
- ✅ You have completed Set up and configure.
Look up a VPC
Most resources need vpc_id. Look up the VPC by name:
data "fptcloud_vpc" "example" {
name = "example-vpc"
}
Use data.fptcloud_vpc.example.id wherever vpc_id is required. This data source searches for the VPC in the tenant declared in the provider. If the VPC is not found, terraform plan stops with an error.
Look up a flavor
The flavor determines the number of vCPUs and the amount of RAM of an Instance.
Select by hardware specification. This method works in any VPC that has a matching flavor:
data "fptcloud_flavor" "medium" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "cpu"
values = ["4"]
}
filter {
key = "memory_mb"
values = ["8192"]
}
filter {
key = "type"
values = ["VM_SIZE"]
}
}
Select by name when you already know the exact flavor name in the VPC:
data "fptcloud_flavor" "by_name" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["Medium-4"] # Replace with a flavor name that exists in your VPC
}
}
Use data.fptcloud_flavor.medium.flavors[0].name for the flavor_name argument of the Instance.
Look up an image
Search by part of the name, excluding GPU images:
data "fptcloud_image" "ubuntu" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["UBUNTU-22.04"]
match_by = "substring"
}
filter {
key = "is_gpu"
values = ["false"]
}
}
Search with a regular expression and sort the results so that the same image is always selected:
data "fptcloud_image" "rocky" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["^ROCKYLINUX-9"]
match_by = "re"
}
sort {
key = "name"
direction = "desc"
}
}
Use data.fptcloud_image.ubuntu.images[0].name for the image_name argument of the Instance.
Changing the image_name of a running Instance deletes and recreates the Instance. If your image filter can return a newer image over time, pin the image name for important Instances.
Look up a storage policy
The storage policy is the performance tier of a disk:
data "fptcloud_storage_policy" "ssd" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["Premium-SSD"] # Replace with a storage policy name that exists in your VPC
}
}
Use data.fptcloud_storage_policy.ssd.storage_policies[0].id for the storage_policy_id argument.
Look up existing resources
Use data sources to reference resources created on Portal without bringing them under Terraform management.
data "fptcloud_subnet" "shared" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["shared-subnet"]
}
}
data "fptcloud_instance" "jump_host" {
vpc_id = data.fptcloud_vpc.example.id
name = "jump-host-01"
}
data.fptcloud_subnet.shared.subnets[0].idreturns the ID of the Subnet.data.fptcloud_instance.jump_host.private_ipreturns the private IP of the Instance.
List all available values
Remove the filter block to get the full list, then print it with output:
data "fptcloud_flavor" "all" {
vpc_id = data.fptcloud_vpc.example.id
}
output "flavors" {
value = [for f in data.fptcloud_flavor.all.flavors : "${f.name}: ${f.cpu} vCPU, ${f.memory_mb} MB"]
}
Run terraform apply to see the list. A data source only reads data and does not create any resource.
Filter rules
The fptcloud_flavor, fptcloud_image, fptcloud_storage_policy, fptcloud_subnet, and fptcloud_instance_group_policy data sources share the same filter and sort blocks:
| Element | How it works |
|---|---|
Several filter blocks | A result must match all blocks. |
Several values in values | A result needs to match only one value. |
match_by = "exact" | Default. Matches the whole string, case-insensitive. |
match_by = "substring" | The string contains the value you search for. Case-sensitive. |
match_by = "re" | Matches with a regular expression. Case-sensitive. |
sort | Sorts the results by key, with direction set to asc (default) or desc. |
When no result matches, the returned list is empty and an expression such as flavors[0] fails with an Invalid index error. Check the filter values again by listing the full list as shown in the previous section.