简体   繁体   English

在 Terraform 中,如何从列表中获取 output 的值?

[英]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.我正在尝试获取 output 以显示正在创建的 IAM 用户的名称。

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我希望得到以下 output

  1. dev-lb开发lb
  2. qa-lb 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]

You would have to use a slightly different approach:您将不得不使用稍微不同的方法:

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

This is the splat expression [1].这是splat表达式 [1]。 Note that the output will be a list.请注意,output 将是一个列表。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM