-2

I have the Terraform output of the module look like below:

ec2_multiple_instances = {
  "one" = {
    "private_dns" = "aaaaa.bbbb.cccc"
    "private_ip" = "xx.xx.xx.xx"
    "public_dns" = "ec2-xx-xx-xx-xx.compute.amazonaws.com"
    "public_ip" = "xx.xx.xx.xx"
    "spot_bid_status" = ""
    "spot_instance_id" = ""
    "spot_request_state" = ""
  }
  "two" = {
    "private_dns" = "xxxxx.yyyyy.zzzzz"
    "private_ip" = "yy.yy.yy.yy"
    "public_dns" = "ec2-yy-yy-yy-yy.compute.amazonaws.com"
    "public_ip" = "yy.yy.yy.yy"
    "spot_bid_status" = ""
    "spot_instance_id" = ""
    "spot_request_state" = ""
  }
}

I would like to get the public_ip values as example:
ec2_one_public_ip = module.ec2_multiple_instances.one.public_ip # "xx.xx.xx.xx"
ec2_two_public_ip = module.ec2_multiple_instances.two.public_ip # "yy.yy.yy.yy"

My terraform version: v1.2.8

Do you have any idea?

Nightt
  • 392
  • 1
  • 4
  • 18

1 Answers1

0

I add:

locals { 
  ec2_ips = [for s in module.ec2_multiple: s.public_ip]
}

And retrieve with:

ec2_one_public_ip = local.ec2_ips[0]
ec2_two_public_ip = local.ec2_ips[1]
Nightt
  • 392
  • 1
  • 4
  • 18