Maya Ray
Maya Ray

Reputation: 541

Terraform is adding and removing the same tags

I imported few EC2 instances and modified our code accordingly. When I run 'terraform plan' the plan shows it's adding and removing same tags. We checked below URL but that fixes resource parameter. https://github.com/hashicorp/terraform-provider-aws/issues/12065

Below is the example:

       "  # module.ec2_app_demo.aws_instance.instances[\"test1\"] will be updated in-place",
        "  ~ resource \"aws_instance\" \"instances\" {",
        "      ~ iam_instance_profile                 = \"test1\" -> \"test1-Role\"",
        "        id                                   = \"i-xxxx\"",
        "      ~ tags                                 = {",
        "          + \"cost_center\"    = \"xxxx\"",
        "          + \"creation_date\"  = \"2022-06-22\"",
        "          - \"creation_date \" = \"2022-06-22\" -> null",
        "          + \"team\"           = \"xxx_yyy\"",
        "          - \"team \"          = \"xxx_yyy\" -> null",
        "            # (15 unchanged elements hidden)",
        "        }",
        "      ~ tags_all                             = {",
        "          + \"cost_center\"    = \"xxxx\"",
        "          + \"creation_date\"  = \"2022-06-22\"",
        "          - \"creation_date \" = \"2022-06-22\" -> null",
        "          + \"team\"           = \"xxx_yyy\"",
        "          - \"team \"          = \"xxx_yyy\" -> null",
        "            # (15 unchanged elements hidden)",
        "        }",
        "      ~ user_data                            = \"c5ba7e37a1f54c411f4aa0e0fc67c0275c6ea375\" -> \"2ace6f48283db2adc579dae9ac5bf618fdafd450\"",
        "      + user_data_replace_on_change          = false",
        "        # (29 unchanged attributes hidden)",

Below is the code for ec2 and ebs:

resource "aws_instance" "instances" {
  for_each = { for instance in var.ec2_instances : instance.name => instance }

  ami                    = each.value.ami
  instance_type          = each.value.type
  cpu_core_count         = each.value.cpu_core_count
  cpu_threads_per_core   = each.value.cpu_threads_per_core
  key_name               = each.value.key_name
  subnet_id              = join("\", \"", data.aws_subnet_ids.subnet_id["${each.value.subnet_name}"].ids)
  vpc_security_group_ids = data.aws_security_groups.sg_id[each.value.name].ids
  secondary_private_ips  = each.value.secondary_private_ips
  iam_instance_profile   = each.value.instance_profile
  
  root_block_device  {
    volume_type = each.value.root_block_device_volume_type
    volume_size = each.value.root_block_device_volume_size
  }

tags = merge({Name = each.key},each.value.tags)


}

vars.tf

variable "ec2_instances" {
  type = list(object({
    name                  = string
    tags                  = map(string)
    type                  = string
    ami                   = string
    cpu_core_count        = number
    cpu_threads_per_core  = number    
    vpc_name              = string
    subnet_name           = string
    key_name              = string
    security_groups       = list(string)
    secondary_private_ips = list(string)
    instance_profile      = string
    additional_eni        = number
    root_block_device_volume_type  = string
    root_block_device_volume_size  = number
    // Patch_Group_Tag       = string
  }))
  description = "EC2 instances to create, with their properties"
  default     = []
}

terraform.tfvars

  {
    "additional_eni": 0,
    "ami": "ami-0f692f9feb5113ce5",
    "cpu_core_count": 4,
    "cpu_threads_per_core": 2,
    "instance_profile": "Test1",
    "key_name": "key_pair.pem",
    "name": "Test1",
    "root_block_device_volume_size": 200,
    "root_block_device_volume_type": "gp3",
    "secondary_private_ips": [],
    "security_groups": [
      "test1-sg"
    ],
    "subnet_name": "test1-snet",
    "tags": {
      "Name": "Test1",
      "cost_center": "Test1",
      "creation_date": "2022-06-22",
      "team": "Test1"
    },
    "type": "m5.2xlarge",
    "vpc_name": "test2-vpc"
  },
  {
    "additional_eni": 0,
    "ami": "ami-0f692f9feb5113ce5",
    "cpu_core_count": 4,
    "cpu_threads_per_core": 2,
    "instance_profile": "test2",
    "key_name": "key_pair.pem",
    "name": "Test2",
    "root_block_device_volume_size": 200,
    "root_block_device_volume_type": "gp3",
    "secondary_private_ips": [],
    "security_groups": [
      "test-sg"
    ],
    "subnet_name": "test2-snet",
    "tags": {
      "Name": "Test2",
      "cost_center": "Test2",
      "creation_date": "2022-06-22",
      "team": "Test2"
    },
    "type": "m5.2xlarge",
    "vpc_name": "test1-vpc"
  }

Upvotes: 0

Views: 1119

Answers (1)

Mark B
Mark B

Reputation: 200446

Looking at the Terraform plan, it is not saying it is adding and removing the same tags. What it is saying is that there were tags named "creation_date " and "team " (notice the space character at the end) that are being removed (that's what the -> null means). And there are new tags named "creation_date" and "team" (without the extra space) being created.

Upvotes: 1

Related Questions