Federico Stango
Federico Stango

Reputation: 578

Static private IPs on known numbers of Autoscaling EC2 instances

I am building a workflow that goes from AWS MediaLive to a set of 2x EC2 instances residing on 2 different availability zones. The EC2 instances are spun from and autoscaling group that is set to always keep 2 instances running (1 per AZ).

Per sé this would not be a problem except that I need the two MediaLive pipelines to always point to a specific instance and not the other. As you may know a private IP is changed every time an instance is spawned so I have a hard time to point MediaLive in a way that does not require restarting the stream every time instances change.

So question is: how can I reuse the same private IP and assign it to the new instance spawning in the same availability zone?

Consider that so far I have been trying several combinations of Launch Templates (that always fail to launch) and tried to create (and assign) a /31 subnet to each AZ only to find out a max of /28 can be created.

Thanks a lot, Federico

Upvotes: 0

Views: 1330

Answers (2)

Federico Stango
Federico Stango

Reputation: 578

Ok, so after serveral failed attempts from the AWS console I decided to try something different and worked my way using the User Data script.

  • I first created 2 ENIs (one per AZ I needed) with manual static private IP assigned.
  • I then used the User Data Script to call a few AWS CLI commands to find the instance ID and it's Availability Zone.
  • With those informations at hand I simply had to match in bash the current AZ with the proper ENI and attach a new network interface to the EC2 instance with the proper AWS CLI command.

As the max number of EC2 instances is known in advance, it is trivial to create enough network interfaces and then manually attach them until all are assigned.

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269861

When a new instance is launched, it could run a User Data script that checks where it is (which AZ) and then assigns an Elastic IP address to itself.

I would recommend:

  • Add a tag to each of the two Elastic IP addresses that identifies a specific AZ
  • In the User Data script, use the EC2 metadata service to discover the AZ of the instance
  • Then, associate the Elastic IP address that has the matching tag

Here's a blog post with a similar concept, that updates a Route 53 domain name with a User Data script: Amazon Route 53: How to automatically update IP addresses without using Elastic IPs - DEV Community

Upvotes: 2

Related Questions