Home > Net >  In Terraform, how to output values from a list?
In Terraform, how to output values from a list?

Time:01-18

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...

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]

CodePudding user response:

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

  • Related