Sub topic 15
Stable public addresses with Elastic IPs
This chapter assumes you can launch an instance, you know the instance states from the lifecycle chapter, and you understand public and private IPv4 addresses in a VPC.
The problem: public IPs that change
An instance in a public subnet can receive an automatically assigned public IPv4 address. That address belongs to the instance only while it is running. If you stop and start the instance, it usually gets a different one. Rebooting keeps the address, and terminating releases it.
This breaks anything that points at the old address, such as a DNS record, a firewall allow list at a partner, or a bookmark in a teammate's SSH config.
An Elastic IP address is a static public IPv4 address that you allocate to your account. It stays yours until you release it, and you can attach it to an instance and move it to another one.
Allocate and associate an address
Allocating and associating are two separate steps. Allocation reserves an address from AWS's pool for your account in the current Region. Association attaches it to an instance, or to a network interface on that instance.
aws ec2 allocate-address --domain vpc
aws ec2 associate-address \
--instance-id i-0123456789abcdef0 \
--allocation-id eipalloc-0123456789abcdef0The first command returns an AllocationId that looks like eipalloc-.... Use that ID in later commands. Once the association succeeds, the instance's public IPv4 address is the Elastic IP. It stays the same across stop and start cycles.
aws ec2 describe-addresses \
--query 'Addresses[].[AllocationId,PublicIp,InstanceId]' \
--output tableAn empty InstanceId column means the address is allocated but not attached to anything.
Remapping to recover quickly
Because an Elastic IP is not tied to one instance, you can move it. If an instance fails, launch a replacement and re-associate the address. Clients keep using the same IP.
aws ec2 associate-address \
--instance-id i-0fedcba9876543210 \
--allocation-id eipalloc-0123456789abcdef0 \
--allow-reassociationWithout --allow-reassociation, the call fails if the address is already associated with another resource.
Costs and limits
AWS charges for every public IPv4 address, whether it is auto-assigned or an Elastic IP, and it charges for an Elastic IP whether or not it is attached. Check the current EC2 pricing page for the hourly rate. An allocated address that nothing uses still costs money, so release it when you finish.
By default each Region allows a small number of Elastic IPs per account. You can request a higher quota.
When not to use one
An Elastic IP is a simple fix, but it is often not the best one:
- For a web application, put a load balancer in front of your instances and use its DNS name. The instances can then be replaced freely.
- For internal traffic, use private IP addresses or private DNS names inside the VPC.
- For access to your own instances, a DNS record that you update is often enough.
Use an Elastic IP when a client needs a fixed IPv4 address, such as a partner that allow-lists your address.
Release the address
Disassociating does not stop the charge. You must release the address.
aws ec2 release-address --allocation-id eipalloc-0123456789abcdef0After release, you cannot rely on getting the same address back.
Exercise
You stop and start an instance that has an automatically assigned public IPv4 address, and its address changes. What can you do to keep the same public address, and what must you do when you no longer need it?
Show answer
Allocate an Elastic IP with allocate-address and attach it with associate-address. The address then stays the same across stop and start. When you no longer need it, release it with release-address, because an allocated address is billed even when it is not attached to an instance.