Terraform 1.4.3: Difficulty Applying Multiple Tags Conditionally on AWS Resources
I'm following best practices but I've been struggling with this for a few days now and could really use some help... I'm currently working with an scenario with applying multiple tags conditionally to AWS resources using Terraform 1.4.3... I have a module that provisions EC2 instances, and I want to apply tags based on the environment (dev, staging, prod). However, I noticed that when I use a `for_each` loop along with a conditional statement, the tags aren't being applied correctly. Here's a simplified version of my code: ```hcl variable "environment" { description = "The environment for the resources" type = string } locals { common_tags = { "Owner" = "team@example.com" } environment_tags = var.environment == "prod" ? { "Environment" = "Production" } : var.environment == "staging" ? { "Environment" = "Staging"} : { "Environment" = "Development"} } all_tags = merge(local.common_tags, local.environment_tags) } resource "aws_instance" "example" { ami = "ami-12345678" instance_type = "t2.micro" tags = local.all_tags } ``` When I run `terraform apply`, I get the following behavior: ``` behavior: Invalid function argument on main.tf line 12, in locals: 12: all_tags = merge(local.common_tags, local.environment_tags) Invalid value for "maps" argument: all maps must have unique keys. ``` I checked the `local.environment_tags` and confirmed that it returns the expected values. I suspect the behavior might be due to how I’m merging the tags, but I’m not sure how to resolve it. I’ve also tried using `lookup` instead of a conditional to create `local.environment_tags`, but it still results in the same behavior. Can someone guide to understand how to resolve this scenario while ensuring the tags are applied appropriately based on the environment? Thanks in advance! For context: I'm using Hcl on macOS. I'd really appreciate any guidance on this. My development environment is Debian. Any suggestions would be helpful.