I created an AWS EKS Cluster with the terraform-aws-eks module. Terraform version is 1.0.6, aws provider version is 3.60.0. With these versions i should be able to use aws_autoscaling_group_tag resource to tag the ASGs which are created by EKS.
My problem is that nodegroups in the module is a map of maps (described here) and i don't know how to iterate over my nodegroups to tag all ASGs within them. Here is the example from terraform:
resource "aws_eks_node_group" "example" {
cluster_name = "example"
node_group_name = "example"
# ... other configuration ...
}
resource "aws_autoscaling_group_tag" "example" {
for_each = toset(
[for asg in flatten(
[for resources in aws_eks_node_group.example.resources : resources.autoscaling_groups]
) : asg.name]
)
autoscaling_group_name = each.value
tag {
key = "k8s.io/cluster-autoscaler/node-template/label/eks.amazonaws.com/capacityType"
value = "SPOT"
propagate_at_launch = false
}
}
In this case there is one specific nodegroup. But in my case there are 3 nodegroups and i want all ASGs to be tagged. I haven't worked much with loops in terraform so far and i am even not sure if it will work. I appreciate any help!