Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change public IP address of EC2 instance without stop/start or elastic IP

Tags:

I'm running an ubuntu AMI on EC2. Is it possible to assign/request a new public IP address for a running EC2 instance without terminating it and starting it again? Note that I'm not interested in using the Elastic IP feature here, I just want to use the regular random public IP addresses assigned by EC2.

like image 700
Niels Kristian Avatar asked Apr 30 '14 08:04

Niels Kristian


People also ask

Can we change public IP of EC2 instance?

As per AWS, when an instance is launched in EC2-Classic, it is automatically assigned a public IP address to the instance from the EC2-Classic public IPv4 address pool. This behavior cannot be modified.

Is it possible to change the private IP addresses of an EC2 while it is running stopped in 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. You cannot change a private IP address.

When you start and stop an EC2 instance with an elastic IP what happens to the public IP address?

Stopped or hibernated instances receive a new public IP address when started again. If an instance's associated Elastic IP address is disassociated from the instance, then it receives a new public IP address.


1 Answers

Update
As Alex B points out in the comments, AWS EC2 instances now have per-second billing, with a minimum billing period of 1 minute. That's a huge, and welcome improvement. It means that starting and stopping an instance should refresh the IP without racking up extra costs.

The one important thing to keep in mind is that per-second billing only applies to Amazon Linux and Ubuntu instances. Other OSs are billed hourly as before. In those cases, the original method explained below is likely the best option.


Stopping and starting an instance is one way to change your IP, but it isn't the fastest or even the cheapest, however it does meet your criteria of avoiding Elastic IPs.

Stopping and starting an instance, from a billing perspective is the same as terminating/relaunching an instance.

Pricing is per instance-hour consumed for each instance, from the time an instance is launched until it is terminated or stopped. Each partial instance-hour consumed will be billed as a full hour. http://aws.amazon.com/ec2/pricing/

This means that, if you start an instance, stop it half an hour later, then start it again and run it for half an hour then stop it again, for that one hour, you're actually going to be billed for two hours of usage.

Elastic IPs are very likely a better solution in that scenario, but the added cost of Elastic IPs is something most people want to avoid. We don't actually want a fixed IP. We just want a new one. Paying for unique IPs per instance doesn't make sense for this. The interesting thing is, we don't need an EIP for each instance in order to release/renew the instance's external IP. We just need one for the entire VPC.

When you assign an EIP to an instance, the old IP is completely gone, released into the void. When you remove the EIP from the instance, the instance is then forced to request a new external IP from the pool.

Keeping a single Elastic IP attached to an instance in the Oregon region (us-west-2) is free, as long as it's attached to something. If it's not attached, it costs $0.05/hour to sit there.

The first 100 IP remaps each month are free. To us, that works out to 50 free IP refreshes (1 remap for release, 1 for renew). If you break that 100 remap limit, the price jumps quickly, to $0.10/remap (or $0.20/IP refresh), so try to keep track. http://aws.amazon.com/ec2/pricing/

TL;DR

The free EIP powered solution? A single EIP for your entire VPC, assigned to a single instance. When you want to release/renew, transfer that IP to the instance in need of a new IP, then transfer it back to the original instance. You can now quickly change an instance's IP up to 50 times a month at no extra cost.

The IP shuffle, ladies and gentlemen :)

like image 160
Steve Avatar answered Nov 05 '22 17:11

Steve