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 =[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 line 38, in output "elb_names":
38:     value =[count.index]

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

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

>Solution :

You would have to use a slightly different approach:

output "elb_names" {
    value =[*].name

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


Leave a ReplyCancel reply