# Assign elastic ips to all created ec2 instances

**URL:** <https://discuss.hashicorp.com/t/assign-elastic-ips-to-all-created-ec2-instances/4779>\
**Category:** Terraform Providers\
**Created:** [December 25, 2019, 5:14pm UTC](https://discuss.hashicorp.com/t/assign-elastic-ips-to-all-created-ec2-instances/4779 "2019-12-25T17:14:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juanluisbaptiste](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/juanluisbaptiste/32/3637_2.png) [@juanluisbaptiste](https://discuss.hashicorp.com/u/juanluisbaptiste)\
**Post date:** [December 25, 2019, 5:14pm UTC](https://discuss.hashicorp.com/t/assign-elastic-ips-to-all-created-ec2-instances/4779/1 "2019-12-25T17:14:05Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![bentterp](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/bentterp/32/3450_2.png) [@bentterp](https://discuss.hashicorp.com/u/bentterp)\
**Post date:** [December 27, 2019, 10:27am UTC](https://discuss.hashicorp.com/t/assign-elastic-ips-to-all-created-ec2-instances/4779/2 "2019-12-27T10:27:07Z")

</div>

You do this by creating a aws\_eip resource with the same count. [Here is a good place](https://www.terraform.io/docs/configuration/resources.html#referring-to-instances) to start reading.

---

<div class="post-metadata">

**Author:** ![juanluisbaptiste](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/juanluisbaptiste/32/3637_2.png) [@juanluisbaptiste](https://discuss.hashicorp.com/u/juanluisbaptiste)\
**Post date:** [December 28, 2019, 4:02am UTC](https://discuss.hashicorp.com/t/assign-elastic-ips-to-all-created-ec2-instances/4779/3 "2019-12-28T04:02:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juanluisbaptiste](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/juanluisbaptiste/32/3637_2.png) [@juanluisbaptiste](https://discuss.hashicorp.com/u/juanluisbaptiste)\
**Post date:** [December 29, 2019, 3:15am UTC](https://discuss.hashicorp.com/t/assign-elastic-ips-to-all-created-ec2-instances/4779/4 "2019-12-29T03:15:44Z")

</div>

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}"
  }
}
```
