Terraform: How to get value of second public ip in module cloudposse ec2

594 Views Asked by At

How I can get value of second IP address from Terraform module EC2.

Module- https://github.com/cloudposse/terraform-aws-ec2-instance

I've created instance EC2 with parameter additional_ips_count = 1. In this situation instance has create with two network interface and I need get value public IP address of second network interface.

Normally the module allows me to extract the value of public ip from the value of public_ip. For example, if I create a module called server, I can get the value of the public IP address from the first network interface using the value module.server.public_ip but how to do it for the second network created using a variable additional_ips_count = 1.

1

There are 1 best solutions below

0
On

You can parse out the values for the value returned from the output module.multiple_ip.additional_eni_ids.

module "multiple_ip" {
  source                      = "git::https://github.com/cloudposse/terraform-aws-ec2-instance.git?ref=master"
  ssh_key_pair                = var.key_name
  vpc_id                      = var.vpc_id
  security_groups             = [data.aws_security_group.this.id]
  subnet                      = data.aws_subnet.private_subnet.id
  associate_public_ip_address = true
  name                        = "multiple-ip"
  namespace                   = "eg"
  stage                       = "dev"
  additional_ips_count        = 1
  ebs_volume_count            = 2
  allowed_ports               = [22, 80, 443]
  instance_type               = var.instance_type
}

output "multiple_ip_additional_eni_ids" {
  value = module.multiple_ip.additional_eni_ids
}

output "z_additional_eni_public_ip" {
  value = values(module.multiple_ip.additional_eni_ids)[0]
}

This will return the ip want.

Outputs:

multiple_ip_additional_eni_ids = {
  "eni-0cc853fb9301b2bc8" = "100.20.97.243"
}
z_additional_eni_public_ip = 100.20.97.243