hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.85k stars 9.2k forks source link

[Bug]: updating the default_tags causes aws_subnet to be recreated #32639

Open vinlp opened 1 year ago

vinlp commented 1 year ago

Terraform Core Version

1.4.6

AWS Provider Version

5.7.0

Affected Resource(s)

aws_subnet

Expected Behavior

We have default_tags set at the provider level.

Each time the default_tags are updated, we want those tags to be updated on all the resources and especially on the aws_subnet ones. Our aws_subnet resources have their own tags: only Name

Actual Behavior

Each time we update those default_tags, we are having issues with aws_subnet resources trying to be replaced/re-created instead of being updated with the new default_tags + their current tag (Name).

plan output

      ~ tags_all                                       = {
          - "DeployedBy" = "Terraform"
          - "Env"        = "dev"
          - "Name"       = "db-private-subnet"
          - "Project"    = "myproject"
          - "Zone"       = "ew1"
        } -> (known after apply)

this ends up with errors during the apply.

Error: creating EC2 Subnet: InvalidSubnet.Conflict: The CIDR '10.X.XXX.0/22' conflicts with another subnet
ā”‚       status code: 400, request id: 24337a43-d090-421b-9831-a6fe3a8252f3

Relevant Error/Panic Output Snippet

Error: creating EC2 Subnet: InvalidSubnet.Conflict: The CIDR '10.X.XXX.0/22' conflicts with another subnet
ā”‚       status code: 400, request id: 24337a43-d090-421b-9831-a6fe3a8252f3

Terraform Configuration Files

locals {
  default_tags = {
    Env        = "dev"
    Project    = "my-project"
    DeployedBy = "Terraform"
    Owner      = "ProjectOwner"
    BU         = "BusinessUnit"
  }
}

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

  backend "s3" {
    encrypt = true
  }
}

provider "aws" {
  region              = "eu-west-1"
  allowed_account_ids = [local.config_file_json.infra.aws_account_id]

  default_tags {
    tags = merge(local.default_tags, {
      Zone = "ew1"
    })
  }
}

data "aws_availability_zones" "available" {
  state = "available"
}

resource "aws_vpc" "my_vpc" {
  cidr_block           = "10.2.0.0/16"
  enable_dns_hostnames = true

  tags = {
    Name = "vpc-${var.resource_suffix}"
  }
}

resource "aws_subnet" "my_public_subnets" {
  count = length(["10.2.0.0/22", "10.2.4.0/22", "10.2.8.0/22"])

  vpc_id                  = aws_vpc.my_vpc.id
  cidr_block              = ["10.2.0.0/22", "10.2.4.0/22", "10.2.8.0/22"][count.index]
  availability_zone       = data.aws_availability_zones.available.names[count.index]
  map_public_ip_on_launch = false

  tags = {
    Name = "public-subnet-${data.aws_availability_zones.available.names[count.index]}-${var.resource_suffix}"
    Tier = "Public"
  }
}

Steps to Reproduce

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

justinretzolk commented 1 year ago

Hey @vinlp šŸ‘‹ Thank you for taking the time to raise this! So that we have the information necessary to look into this, can you supply debug logs (redacted as needed)?

kristian-lesko commented 11 months ago

Hello, I'm seeing a similar issue. It seems at least the following resources are affected:

Note: in our case, aws_secretsmanager_secret_version depends on a aws_kms_secrets data source, which is reported as re-read during a plan when default_tags are added/changed.

eml-39502 commented 5 months ago

Had the issue with AWS provider 5.41.0 and 5.43.0 as well.