Sub topic 16
Stable public addresses with Elastic IPs
This chapter assumes you can launch an instance with the AWS CLI, know what a public subnet and an internet gateway are, and have read the earlier chapters on the instance lifecycle and VPC addressing. By the end you should be able to allocate, attach, move and release an Elastic IP, and decide when you need one.
The problem: public IPs that change
An instance in a public subnet can receive an automatically assigned public IPv4 address at launch. That address belongs to AWS's pool, not to you. AWS releases it when the instance stops or terminates. When you start the instance again, it gets a different address.
A reboot does not change the address. Only a stop followed by a start does.
This matters when something outside AWS depends on the address. Examples are a DNS A record, a partner's firewall allow-list, or a bookmark in a script. If the address changes, those references break.
What an Elastic IP is
An Elastic IP address (EIP) is a static public IPv4 address that you allocate to your AWS account in one Region. It stays yours until you release it. You can associate it with an instance, and later disassociate it and associate it with another instance in the same Region.
Three properties follow from this:
- It survives stop and start. The instance keeps the same public address.
- It is not tied to an instance. You can move it to a replacement instance, which makes it a simple failover tool.
- It is Regional. You cannot move an EIP from one Region to another.
Technically, the EIP is associated with a network interface, and by default with that interface's primary private IPv4 address. The instance reaches the internet through that interface. A network interface can hold several private addresses, and each one can have its own EIP.
The lifecycle of an Elastic IP
Allocation reserves the address for your account but does not attach it to anything. Association connects it to a network interface. Release returns it to AWS, and you may not get the same address back.
Allocating and associating with the CLI
Allocate an address and tag it so you can find it later:
aws ec2 allocate-address \
--domain vpc \
--tag-specifications 'ResourceType=elastic-ip,Tags=[{Key=Name,Value=tutorial-web-eip}]'The output includes an AllocationId (such as eipalloc-0123456789abcdef0) and the PublicIp. You use the allocation ID in later commands.
Associate it with an instance:
aws ec2 associate-address \
--instance-id i-0123456789abcdef0 \
--allocation-id eipalloc-0123456789abcdef0The response contains an AssociationId. Keep it if you plan to disassociate later.
To confirm the result, list your addresses and instance details:
aws ec2 describe-addresses \
--allocation-ids eipalloc-0123456789abcdef0 \
--query 'Addresses[].[PublicIp,InstanceId,AssociationId,PrivateIpAddress]' \
--output tableNow repeat the stop and start from the lifecycle chapter. The instance's PublicIpAddress stays the same, unlike the automatically assigned address you saw before.
What happens to the old public address
If the instance already had an automatically assigned public IPv4 address, associating an EIP releases that address. You cannot get it back. The instance then has exactly one public IPv4 address, the EIP.
When you disassociate the EIP, the instance does not get its old address back. In a subnet that auto-assigns public IPs, the instance may receive a new automatic address in some cases, but you should not depend on that. Treat disassociation as leaving the instance without a stable public address.
Moving an address between instances
Failover is the classic use. If instance A fails, you launch or choose instance B and move the address:
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 in a VPC. With it, AWS moves the association in a single step. Clients see a short interruption while the change propagates, and then traffic reaches instance B.
This is a manual or scripted failover. For automatic failover across many servers, use a load balancer instead (see the scaling chapter).
Cost: all public IPv4 addresses are billed
Since February 2024, AWS charges for every public IPv4 address, at about $0.005 per hour per address. The charge applies whether the address is attached, idle, or automatically assigned. Check current pricing on the AWS pricing page, because rates and free-tier terms can change. New accounts get a limited free-tier allowance of public IPv4 hours.
Practical consequences:
- An EIP is not free while attached. An idle EIP costs the same as an attached one.
- An idle address gives you nothing in return, so release any you do not need.
- Public IPv4 addresses are scarce, so AWS limits you by default to five EIPs per Region. You can request an increase.
When you need an Elastic IP, and when you do not
An EIP is a reasonable choice when:
- A single instance needs a fixed address that external parties allow-list.
- You want a NAT gateway, which requires an EIP for public NAT.
- You need to move one address between standby instances quickly.
Prefer other designs when:
- You only need a stable name. Use a DNS record in Route 53 pointing at a load balancer. Clients then follow the name, not an address.
- Several instances serve the same traffic. Put them behind an Application Load Balancer, which has its own DNS name and handles health checks.
- The instance only needs outbound access. Place it in a private subnet behind a NAT gateway, so it needs no public address at all.
- Your clients support IPv6. IPv6 addresses in a VPC are not billed per address in the same way and are globally unique.
Disassociating and releasing
To detach an address, use the association ID. To give it back to AWS, use the allocation ID:
aws ec2 disassociate-address --association-id eipassoc-0123456789abcdef0
aws ec2 release-address --allocation-id eipalloc-0123456789abcdef0You can release an address that is still associated in a VPC. AWS disassociates it first only if you do it in the right order, so the clean sequence above is the safer habit. After release, anything that still points at that IP now points at an address that may belong to another customer. Remove DNS records and allow-list entries first.
Common errors
| Symptom | Likely cause |
|---|---|
| `Resource.AlreadyAssociated` on associate | The address is attached elsewhere. Add `--allow-reassociation` or disassociate first. |
| `AddressLimitExceeded` on allocate | You reached the Regional limit of EIPs. Release unused ones or request an increase. |
| Instance unreachable after association | The subnet has no route to an internet gateway, or the security group blocks the port. |
| Unexpected public IPv4 charges | An idle EIP or an automatically assigned address you forgot about. |
| `InvalidAllocationID.NotFound` | Wrong Region, or the address was already released. |
Exercise
You attach an Elastic IP to an instance, then stop and start it. Later you terminate the instance. What do you expect for the public IP after the stop and start, and what must you still do after the termination to stop charges for the address?
Show answer
After the stop and start, the instance keeps the same public IP, because the Elastic IP stays associated. After termination, the address remains allocated to your account and keeps billing, so you must run aws ec2 release-address --allocation-id <id> (after removing any DNS records or allow-list entries that use it).
Exercise
A web server must keep the same public address when you replace it with a new instance built from an updated AMI. Which CLI call moves the address, and which flag is required if the address is still attached to the old instance?
Show answer
Use aws ec2 associate-address with the new instance ID and the allocation ID. Add --allow-reassociation when the address is still associated with the old instance.