Skip to main content

Tag examples

Tags help you classify resources by project, environment or owning team, which makes it easier to track costs and apply policies. With Terraform, you create a tag once and then attach it to every resource in the configuration, so you never forget to tag a new resource. For tag concepts, see FPT Cloud Tagging.

Prerequisites​

Tagging takes two steps​

  1. Create the tag with the fptcloud_tagging resource.
  2. Put the ID of the tag in the tag_ids argument of each resource.

The resources in this guide that accept tag_ids are fptcloud_instance, fptcloud_storage, fptcloud_subnet, fptcloud_security_group, fptcloud_floating_ip and fptcloud_snapshot.

Create a tag​

# tags.tf
resource "fptcloud_tagging" "env_production" {
key = "environment"
value = "production"
color = "#057a55"
scope_type = "ORG"
}

resource "fptcloud_tagging" "team_payments" {
key = "team"
value = "payments"
scope_type = "ORG"
}
  • key is required, and value and color are optional.
  • scope_type = "ORG" makes the tag available across the whole organization, like a tag created on the Portal.
  • color takes a color code from the Tag colors table.

Attach tags to resources​

Add tag_ids to the resources that you want to tag:

# tagged.tf
data "fptcloud_vpc" "example" {
name = "example-vpc"
}

resource "fptcloud_subnet" "payments" {
vpc_id = data.fptcloud_vpc.example.id
name = "payments-subnet"
type = "NAT_ROUTED"
cidr = "10.30.1.0/24"
gateway_ip = "10.30.1.1"
tag_ids = [fptcloud_tagging.env_production.id, fptcloud_tagging.team_payments.id]
}
# tagged.tf
resource "fptcloud_floating_ip" "payments" {
vpc_id = data.fptcloud_vpc.example.id
tag_ids = [fptcloud_tagging.env_production.id, fptcloud_tagging.team_payments.id]
}

For Instances and disks, add the same tag_ids line to the fptcloud_instance or fptcloud_storage block. Tags are updated in place, so adding or removing tags does not recreate the resource.

Attach a common set of tags to every resource​

Declare the common set of tags in locals, then reuse it in every resource. When you need to add a tag, you only edit one place:

# common-tags.tf
locals {
common_tag_ids = [
fptcloud_tagging.env_production.id,
fptcloud_tagging.team_payments.id,
]
}

Then set tag_ids = local.common_tag_ids on each resource.

Create several tags from a map​

# tags-map.tf
locals {
tags = {
environment = "staging"
project = "online-shop"
cost-center = "cc-1024"
}
}

resource "fptcloud_tagging" "this" {
for_each = local.tags

key = each.key
value = each.value
scope_type = "ORG"
}

Get the list of IDs with [for t in fptcloud_tagging.this : t.id].

Reuse an existing tag​

If the tag was already created on the Portal, read it with a data source instead of creating it again:

# existing-tag.tf
data "fptcloud_tagging" "owner" {
key = "owner"
value = "platform-team"
}

Use data.fptcloud_tagging.owner.tags[0].id in tag_ids.

Tag colors​

Colorcolor value
Gray#4b5563
Red#c81e1e
Orange#d03801
Yellow#dc6803
Green#057a55
Teal#047481
Indigo#5850ec
Purple#6c2bd9
Sky blue#00bfff

The value is not case-sensitive. Leave color empty to create a tag without a color.

Next steps​