Search code examples
terraformtagsoracle-cloud-infrastructure

How to make defined OCI tags in Terraform?


I don't know why I can't make tags like this. I don't have any issues on AWS.

resource "oci_core_public_ip" "public-ip-address-1" {
  compartment_id    = oci_identity_compartment.tf-compartment.id
  display_name      = "reserved public ip"
  lifetime          = "RESERVED"
  private_ip_id     = data.oci_core_private_ips.attach-public-ip-address-1.private_ips[0]["id"]
  defined_tags = {
    Lifecycle = "persistent"
  }
  lifecycle {
    prevent_destroy = true
  }
}

I get this error:

╷
│ Error: invalid key structure found Lifecycle
│
│   with oci_core_public_ip.public-ip-address-1,
│   on compute.tf line 32, in resource "oci_core_public_ip" "public-ip-address-1":
│   32: resource "oci_core_public_ip" "public-ip-address-1" {
│

Do I have to declare tags elsewhere?


Solution

  • In your case you need to create a tag_namespace of Lifecycle

    I wrote a example for you -

    resource "oci_identity_tag_namespace" "generic" {
      # Required
      compartment_id = "CompartmentOCID"
      description    = "Generic namespace for adding generic labels"
      name           = "generic"
    }
    
    resource "oci_identity_tag" "generic_label_project" {
        # Required
        description = "Label tag used for adding a generic label"
        name = "label"
        tag_namespace_id = oci_identity_tag_namespace.generic.id
    
        # Optional
        is_cost_tracking = false
        is_retired = false
    }
    
    resource "oci_core_vcn" "service_vcn" {
      cidr_block     =     local.cidr_block
      compartment_id =     "CompartmentOCID"
      display_name =       "ServiceVCN"
    
      defined_tags = {
        "generic.label" = "persistent"
      }
    }