Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution for local ip changes of AWS EC2 instances

Tags:

Amazon only gives you a certain number of static ip address and the local (private) ips of each EC2 instance can change when the machine is restarted. This makes creating a stable platform where EC2 instances depend on each other ridiculously hard to use as far as I can tell.

I've search online a lot about various solutions and so far have found nothing reasonable outside of assigning an elastic ip address on ever EC2 even if its not public facing. Does anyone have any other good ideas that is actually easy to execute on?

Thanks!

See the AWS team's response to question Static local IP:

The internal IP address of EC2 instances is allocated via DHCP. On instance shutdown, or when the DHCP lease expires, the IP address is returned to the general EC2 DHCP pool of addresses available for other instances.

There is no way to guarantee that you will obtain the same DHCP address across reboots.

Edit: The answer is to use Amazon VPC. There is no downside except a trivial amount of extra setup because now you control the router. It's a world apart from plain old EC2 instance on AWS. It's so necessary in fact that VPC will be enabled for all future AWS setups by default. See this post for more information: http://www.reddit.com/r/aws/comments/1a3n0r/ec2_update_virtual_private_clouds_for_everyone/

like image 426
Mauvis Ledford Avatar asked May 24 '12 07:05

Mauvis Ledford


People also ask

Does IP address of EC2 instance change?

Once an EC2 instance is launched, it's assigned a private IP address at boot time. An instance's private IP address will never change during the lifetime of that instance.

How do I change my primary IP address on AWS EC2?

From the Networking tab, Expand Network interfaces, and then choose the Interface ID. Select the Network interface ID, Choose Actions, and then choose Manage IP Addresses. The primary private IPv4 IP address is already listed. Expand the network interface ID, and then choose Assign new IP address.

Can I change the private IP addresses of an Amazon EC2 instance while it is running and or stopped within a VPC?

The private IP address of an Amazon EC2 instance will never change. It will not change while an instance is running. It will not change while an instance is stopped.


2 Answers

The stock answers are:

  1. Use AWS VPC so you have complete control over instance addressing
  2. Use Elastic IPs, which will resolve to the instance's local address (not the public, as you'd expect) when used to communicate between EC2 instances
like image 167
gabrtv Avatar answered Oct 08 '22 03:10

gabrtv


I stumbled upon third option. There's ec2-ssh by the Instragram folks. It's a python shell script that you install globally and lets you both query the public dns of your ec2 instances by tag name and also ssh in via tag name as well.

The documentation for it is virtually nonexistent. I've written down the steps to install below:

To install ec2-ssh:

  1. sudo yum install python-boto (python wrapper for ec2 api)
  2. git clone https://github.com/Instagram/ec2-ssh
  3. In your ~/.bash_profile set your AWS access key and secret like so:

    export AWS_ACCESS_KEY_ID=XYZ123

    export AWS_SECRET_ACCESS_KEY=XYZ123

  4. cd into the bin folder of the repo, there will be two files:

    ec2-host and ec2-ssh

copy them to your /usr/bin or /usr/local/bin.

Now you can do awesome stuff like:

$ ec2-host ZenWorker ec2-999-xy-999-99.compute-1.amazonaws.com 

and

$ ec2-ssh ZenWorker Connecting to ec2-999-xy-999-99.compute-1.amazonaws.com. 

Note that in your regular shell scripts you can use backticks to call these global tools. I've timed these calls and they take between 0.25 and 0.5 second using an EC2 instance, so that's really the only downside. Perhaps you can live with the delay, or use the fact that public DNS only changes for an instance on reboot to work up a solution.

Note that these two programs are commandline scripts and you don't need any Python knowledge to use them. For PHP fans, or those that also want an easy way to scp files without knowing the changing public DNS, you can checkout ec2dns.

like image 39
Mauvis Ledford Avatar answered Oct 08 '22 02:10

Mauvis Ledford