Terraform执行plan时重复增删相同标签问题求助
问题:Terraform Plan显示重复添加/删除EC2标签
导入若干EC2实例并修改Terraform代码后,执行terraform plan时,计划显示会重复添加和删除相同标签。此前参考过相关问题方案,但仅修复资源参数问题,未解决标签问题。
Plan示例
# 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)
EC2及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" }
内容的提问来源于stack exchange,提问作者Maya Ray
相关产品推荐
相关产品推荐

