Home > database >  Outputting resources generated by for_each in Terraform
Outputting resources generated by for_each in Terraform

Time:03-14

I am creating S3 buckets from an array like so:

resource "aws_s3_bucket" "aws_s3_zenity_bucket" {
  bucket = "${each.value}"
  for_each = toset(var.s3_buckets)
}

where s3_buckets is just a list with s3 buckets names I want to create.

I want to module to output a list that contains all the ARNs of the generated buckets.

pseudo-code of what I want:

output "s3_buckets_arns" {
    for_each = aws_s3_bucket.aws_s3_zenity_bucket
    select_as_output = each.value.arn
} # should look something like ["arn1", "arn2", ....]

How do I do that in TF?

CodePudding user response:

You can use the splat operator combined with the values function:

output "s3_buckets_arns" {
  value = values(aws_s3_bucket.aws_s3_zenity_bucket)[*].arn
}
  • Related