Assign elastic ips to all created ec2 instances

Hi,

I’m new to terraform, I’m creating a swarm cluster (3 managers, 5+ workers) to run 30+ wordpress sites. As the instances IP addresses can change on instance restart, swarm nodes would loose connectivity to the cluster because of the new IP address and they would need to re-join the cluster.

So to avoid this problem I want to assign elastic IP’s to all the instances when they are created by terraform. It’s easy to assign an elastic IP to an instance using the aws_eip resource if you have the instance id at hand, but how to do it for multiple instances created using a single aws_instance resource with count parameter > 1.

How can I get the ids of those newly created instances so I can loop over them and create an elastic ip address associated to each of them ?

Thanks and merry christmas !

You do this by creating a aws_eip resource with the same count. Here is a good place to start reading.

@bentterp thank you, I will read that documentation and come back if I have any questions.

For anyone else looking for this here is the code I used:

resource "aws_eip" "eip_manager" {
  instance   = "${element(aws_instance.manager.*.id,count.index)}"
  count = "${var.manager_count}"
  vpc = true
  
  tags = {
    Name = "eip-${var.environment}-${count.index + 1}"
  }
}