Search code examples
amazon-web-servicesamazon-ec2aws-auto-scalingaws-media-live

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


Solution

  • 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.