Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Terraform, how to output values from a list?

I am trying to get the output to show me the names of the IAM users being created.

resource "aws_iam_user" "lb" {
    name = var.elb_names[count.index]
    count = 3
    path = "/system/"
}

variable "elb_names" {
    type = list
    default = ["dev-lb", "qa-lb", "prod-lb"]
}

output "elb_names" {
    value = aws_iam_user.lb.name[count.index]
}

I expect to get the following as output

  1. dev-lb
  2. qa-lb
  3. prod-lb

But I am getting this error…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: Missing resource instance key
on countEC2.tf line 38, in output "elb_names":
38:     value = aws_iam_user.lb.name[count.index]

Because aws_iam_user.lb has "count" set, its attributes must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
aws_iam_user.lb[count.index]

>Solution :

You would have to use a slightly different approach:

output "elb_names" {
    value = aws_iam_user.lb[*].name
}

This is the splat expression [1]. Note that the output will be a list.


[1] https://developer.hashicorp.com/terraform/language/expressions/splat

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading