Hi,
I’d appreciate any help I can get. I’ve been stuck on this for days and can’t seem to figure it out. My goal is to create an output like this:
Goal:
Outputs:
ec2_instances = [
"i-07a8ed7dda10ecc12": 35.155.XXX.XX,
"i-06e93292096ad880f": 35.155.XXX.XX,
]
This is what the current output (local.ec2_comb
) looks like:
ec2_instances = [
{
"ID" = 0
"IP" = "i-0bcfff94e8d2f9849"
},
{
"ID" = 0
"IP" = "i-01fba46588f5ecee7"
},
{
"ID" = 1
"IP" = "35.155.XXX.XX"
},
{
"ID" = 1
"IP" = "35.155.XXX.XX"
},
I am using this to create the output:
output "ec2_instances" {
value = local.ec2_comb
}
locals {
ec2_ids = flatten(tolist([module.ec2_instances.id, module.ec2_instances_dynamodb.id]))
ec2_ips = flatten(tolist([module.ec2_instances.public_ip, module.ec2_instances_dynamodb.public_ip]))
pair = concat([local.ec2_ids, local.ec2_ips])
ec2_comb = flatten([
for ID, IPs in local.pair : [
for IP in IPs : {
ID = ID
IP = IP
}
]
])
}
Output of locals.ec2_ids
:
ec2_instance_id = [
"i-0eda41e4452281af2",
"i-018baefc331b68cb3",
]
Output of locals.ec2_ips
:
ec2_instance_public_ips = [
"35.155.XXX.XX",
"35.155.XXX.XX",
]
Output of locals.pair
:
ec2_instances = [
[
"i-0eda41e4452281af2",
"i-018baefc331b68cb3",
],
[
"35.155.XXX.XX",
"35.155.XXX.XX",
],
]
The reason I am using the tolist()
function is because the modules create a tuple type value (e.g module.ec2_instances.id
) for the EC2 instance.
So in this case, the instance IDs and public IPs are tuples.
Thank you in advance for the help.