acmesoft
acmesoft

Reputation: 17

Terraform output from several resources generated with count

I have these outputs:

output "ec2_id" {
   value = aws_instance.ec2instance[*].id
}
output "ec2_name" {
   value = aws_instance.ec2instance[*].tags["Name"]
}
output "ec2_mgmt_eip" {
   value = aws_eip.eip_mgmt_ec2instance[*].public_ip
}

I want to make an output like:

"<instanceName>: <instanceID> -> <publicIP>" (all data in same line for same ec2 instance). In any non-declarative language i can use something like for (var i=0; i<length(myarray);i++) and use "i" as index for each list, in every index concatenate in a new string, but in terraform I can't find how to do it.

Thanks!

Upvotes: 0

Views: 1143

Answers (1)

Marko E
Marko E

Reputation: 18138

Even though you got the answer in the comments, I will add an example. So, the thing you want does exist in terraform as it also has for loops [1]. A for loop along with the right syntax will give you a desired output, which is a map in terraform:

output "ec2_map" {
  value = { for i in aws_instance.ec2instance: i.tags.Name => "${i.id}:${i.public_ip}" }
}

The output you said you want is quite similar to this. Also, there is no concept of "same line" in terraform. In this case, since this is a map, the keys will be instance names and value will be a combination of instance id and the public IP, but that will be a string.


[1] https://www.terraform.io/language/expressions/for

Upvotes: 3

Related Questions