Skip to content

[Bug]: Ignore_tags not working as expected with ec2 module #36706

Description

@BharadwajAyinapurapu

Terraform Core Version

1.7.4

AWS Provider Version

5.43.0

Affected Resource(s)

I am using ec2-instance module in AWS to create an EC2 instance. Here's the main.tf


terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region = var.region
  ignore_tags {
    keys         = ["LastAttachInstance", "LastAttachTime", "Company", "Launched_by", "PrincipalId"]
    key_prefixes = ["c7n:"]
  }
}

module "sonar_rhel8_instance" {
  source                        = "terraform-aws-modules/ec2-instance/aws"
  count                         = var.instance_count
  name                         = "sonarqube-rhel8-instance-${count.index + 1}"
  ami                            = var.spg_rhel8_ami
  instance_type            = var.sonar_instance_type
  metadata_options     = { "http_tokens" : "required" }
  root_block_device     = [{
    encrypted  = true
    kms_key_id = var.sonar_kms_key_id
  }]
  ebs_block_device      = [{
    device_name = "/dev/sdb"
    volume_size = 100
    encrypted   = true
    kms_key_id  = var.sonar_kms_key_id
  }]
  ebs_optimized          = true
  tags = merge(
    var.sonar_aws_tags,
    {
      Name                 = "sonarqube-rhel8-instance-${count.index + 1}",
      Maintenance_Schedule = var.maintenance_schedule[count.index % length(var.maintenance_schedule)],
    },
  )
  volume_tags              = merge(
    var.sonar_aws_tags,
    {
        Name        = "sonarqube-rhel8-instance-${count.index + 1}",
        Environment = var.environment_name,
        AWSBackup   = "EBS-Daily",
    },
  )
}

I have created the instance using terraform. Tags specified are getting ignored, whereas volume_tags are not getting iignored.

Expected Behavior

Instance tags as well as Volume_tags getting ignore.

Actual Behavior

This is the terraform plan output. As we can see it is detecting the changes in volume tags. It is changing the volume tags' value to null, which it shouldn't as we mentioned terraform to ignore the tags.

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # module.sonar_rhel8_instance[0].aws_instance.this[0] will be updated in-place
  ~ resource "aws_instance" "this" {
        id                                   = "i-0d8f73187b4ca6634"
        tags                                 = {
            "AWSBackup"            = "EC2-Monthly"
            "AppID"                = "2D3A0CF2-37F6-41CE-B5C1-C6AA6DE0FC8B"
            "BU"                   = "Platts"
            "Backup"               = "Daily"
            "Environment"          = "DEV"
            "Maintenance_Schedule" = "2nd-Tue-plus1d-06:00UTC"
            "Name"                 = "plt-ia-sonarqube-default-rhel8-instance-1"
            "Owner"                = "irum.malik@spglobal.com"
            "Project"              = "Platts Techops"
            "Support_Group"        = "PL-PAS-Techops"
            "Technology"           = "Platts SonarQube"
            "Used_For"             = "Application Server"
        }
      ~ volume_tags                          = {
          - "Company"       = "spgi_volume" -> null
          - "Launched_by"   = "bharadwaj.a@spglobal.com" -> null
          - "PrincipalId"   = "AROAJXSACXP7NVTGZL5W4:bharadwaj.a@spglobal.com" -> null
            # (11 unchanged elements hidden)
        }
        # (31 unchanged attributes hidden)

        # (10 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

However it is working as expected if we use resource blocks to create EC2 instance and EBS volume respectively.
So is this an issue with module. Any suggestions/workarounds for this please?

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region = var.region
  ignore_tags {
    keys         = ["LastAttachInstance", "LastAttachTime", "Company", "Launched_by", "PrincipalId"]
    key_prefixes = ["c7n:"]
  }
}

module "sonar_rhel8_instance" {
  source               = "terraform-aws-modules/ec2-instance/aws"
  count                = var.instance_count
  name                 = "plt-ia-sonarqube-${terraform.workspace}-rhel8-instance-${count.index + 1}"
  ami                  = var.spg_rhel8_ami
  instance_type        = var.sonar_instance_type
  metadata_options     = { "http_tokens" : "required" }
  root_block_device = [{
    encrypted  = true
    kms_key_id = var.sonar_kms_key_id
  }]
  ebs_block_device = [{
    device_name = "/dev/sdb"
    volume_size = 100
    encrypted   = true
    kms_key_id  = var.sonar_kms_key_id
  }]
  ebs_optimized = true
  tags = merge(
    var.sonar_aws_tags,
    {
      Name                 = "plt-ia-sonarqube-${terraform.workspace}-rhel8-instance-${count.index + 1}",
      Maintenance_Schedule = var.maintenance_schedule[count.index % length(var.maintenance_schedule)],
    },
  )
  volume_tags = merge(
    var.sonar_aws_tags,
    {
        Name        = "plt-ia-sonarqube-${terraform.workspace}-rhel8-instance-${count.index + 1}",
        Environment = var.environment_name,
        AWSBackup   = "EBS-Daily",
    },
  )
}

Steps to Reproduce

  1. Ran the terraform apply to create the instances
  2. Created tags manually on the AWS console for EC2 instance as well as the volume created
  3. Ran the terraform plan to check if the changes are ignored by terraform
  4. Instance tags are getting ignored but not the volume tags

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAddresses a defect in current functionality.service/ec2Issues and PRs that pertain to the ec2 service.tagsPertains to resource tagging.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions