0

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

Federico Stango
  • 548
  • 5
  • 21
  • 2
    Can your instances use [Elastic network interfaces](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html)? You could create few of them, and have your instance attach them when they are created? – Marcin Jul 26 '21 at 11:08
  • I have 2 ENI, one per AZ, they are assigned (in launch templ.) to net port 2 and 3. Net 1 is at default. This returns: "Incompatible launch template: Network interface ID cannot be specified as console support to use an existing network interface".. but I believe I am not doing it! And Google did not help. I have also tried removing network port 1 while keeping 2 and 3 but this other error shows up: "network interfaces must include a primary network interface with a device index of zero". This one is pretty clear... – Federico Stango Jul 26 '21 at 12:42

2 Answers2

1

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

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Hello John, I resorted to something similar the other day and wanted to provide my "self" answer but did not have time so far. I will write it down here for future. Thank you very much nonetheless! – Federico Stango Jul 28 '21 at 13:20
0

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.

Federico Stango
  • 548
  • 5
  • 21