简体   繁体   中英

In Terraform, how do you output a list from an array of objects?

I'm creating a series of s3 buckets with this definition:

resource "aws_s3_bucket" "map" {
  for_each = local.bucket_settings
  bucket = each.key
...
}

I'd like to output a list of the website endpoints:

 output "website_endpoints" {
    # value = aws_s3_bucket.map["example.com"].website_endpoint
    value = ["${keys(aws_s3_bucket.map)}"] 
 }

What's the syntax to pull out a list of the endpoints (rather than the full object properties)?

If you just want to get a list of website_endpoint , then you can do:

 output "website_endpoints" {
    value = values(aws_s3_bucket.map)[*].website_endpoint
 }

This uses splat expression.

You can loop over your buckets with for loop and output specific attribute, in this case website_endpoint.

output "endpoint" {
    value = [for s in aws_s3_bucket.map : s.website_endpoint[*]]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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