0
variable "managed_addons" {
      description = "EKS manged addons"
      type        = map(string)
    }


resource "aws_eks_addon" "this" {
      for_each = var.managed_addons
    
      cluster_name                = aws_eks_cluster.this.name
      addon_name                  = each.key
      addon_version               = each.value
      resolve_conflicts_on_update = "OVERWRITE"
    }

managed_addons = {
        vpc-cni    = "v1.19.2-eksbuild.1"
        coredns    = "v1.11.4-eksbuild.2"
        kube-proxy = "v1.29.11-eksbuild.2"
      }

I want the output as

key = addon_name

value = addon_version.

example output

vpc-cni    = "v1.19.2-eksbuild.1"
coredns    = "v1.11.4-eksbuild.2"
kube-proxy = "v1.29.11-eksbuild.2"

I tried many things but its gives different error

output "managed_addons" {
      # value = { aws_eks_addon.this[*].addon_name = values(aws_eks_addon.this).*.addon_version }
      value = {
        for k, v in aws_eks_addon.this : k.addon_name => v.addon_version
      }

For loop throws below error

Can't access attributes on a primitive-typed value (string).

1 Answer 1

1

In Terraform, when you use for_each on a resource block, the map key (k) is just a string, so k.addon_name will fail because you are trying to access an attribute on a string. Meanwhile, the value (v) is the actual resource object, so v.addon_name and v.addon_version are valid.

You can fix your output by using:

output "managed_addons" {
  value = {
    for k, v in aws_eks_addon.this : k => v.addon_version
  }
}

This will generate a map like:

{
  "vpc-cni"    = "v1.19.2-eksbuild.1"
  "coredns"    = "v1.11.4-eksbuild.2"
  "kube-proxy" = "v1.29.11-eksbuild.2"
}

Alternatively, if you'd rather rely on the resource attribute itself (instead of the for_each key), you could do:

output "managed_addons" {
  value = {
    for _, v in aws_eks_addon.this : v.addon_name => v.addon_version
  }
}

Either way, you'll get the same result. The key point is that k is a string (the map key), and v is the resource object, so make sure you reference the correct variable for each attribute you need.​​​​​​​​​​​​​​​​

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you . the solution worked perfectly.
Also thank you for the explanation. I now understand the logic behind
You’re welcome @psaikia

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.