Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform outputs for resources with count

Tags:

I have my s3 resource in terraform with configuration:

locals {     bucket_count = "${length(var.s3_config["bucket_names"])}" }  resource "aws_s3_bucket" "s3_bucket" {     count = "${local.bucket_count}"     bucket = "${format("%s-%s", element(var.s3_config["bucket_names"], count.index), var.region)}"     acl = "private"     region = "${var.region}"      tags {         Name = "${format("%s-%s", element(var.s3_config["bucket_names"], count.index), var.region)}"     } } 

and i want to set output variable for all created bucket so i created file names outputs.tf with content

output "buckets" {   value = "${aws_s3_bucket.s3_bucket.*.bucket}" }  output "buckets_arns" {   value = "${aws_s3_bucket.s3_bucket.*.arn}" } 

when i apply configuration its ok i see outputs in terraform.tfstate file but when i call terraform output i see information that is no output or output is empty what i do wrong ?

like image 305
jagwe danfesa Avatar asked Aug 27 '18 13:08

jagwe danfesa


People also ask

Can you use count in output Terraform?

count is a meta-argument defined by the Terraform language. It can be used with modules and with every resource type. The count meta-argument accepts a whole number, and creates that many instances of the resource or module.

How do you use for each and count together in Terraform?

What is count in Terraform? When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage several of the same resources, you can use either count or for_each , which removes the need to write a separate block of code for each one.

Can Terraform output have multiple values?

You can have multiple resources in a single output, but to make it work, you need to use some terraform function, or format the output depending on the type, if it is a list, string, map..


1 Answers

Try this:

output "buckets" {   value = ["${aws_s3_bucket.s3_bucket.*.bucket}"] }  output "buckets_arns" {   value = ["${aws_s3_bucket.s3_bucket.*.arn}"] } 
like image 176
Mithun Biswas Avatar answered Oct 13 '22 10:10

Mithun Biswas