I am working on tagging AWS resources, I want only few resources have optional_tags. Optional_tags variables are only passed if available. All variables declared in optional_tags are default null.
I have tried using tags and all vars in that but the for loop fails on null values.
Error: Missing required argument
on main.tf line 41, in resource "aws_ec2_tag" "ami-tagging":
41: value = each.value
Now I am trying to use two maps and merge on conditional as below. If value exist for a optional_tag variable then merge with it or merge with itself. It has been complaining about missing ( . I am very new to terraform, any help is appreciated.
locals {
optional_tags = {
docker_version = var.docker_version
kubernetes = var.k8s_version
cni_plugin_version = var.cni_plugin_version
containerd_version = var.containerd_version
target = var.target
}
default_tags = {
Description = var.ami_description
Name = var.ami_name
Creator = var.creator
}
tags = merge
(
local.default_tags,
local.optional_tags[contains(keys(local.optional_tags.containerd_version),local.optional_tags) ? local.default_tags]
)
}
resource "aws_ec2_tag" "ami-taggging" {
resource_id = data.aws_ami.eks_image.id
for_each = local.tags
key = each.key
value = each.value
}