Ravindra BagaleCourses & study guides

Chapter 14: EC2 Operations — Elastic IP, AMI, Snapshots and Vertical Scaling

14.1 Elastic IP in depth

Why do we need it?

Remember Chapter 1 and 5: the auto-assigned public IPv4 address is released when you stop the instance, and a new one is given when you start it. Your DNS record, your WordPress site URL, your friends' bookmarks — all point to the old IP and break. Bagha, an Elastic IP (EIP) solves this: it is a static public IPv4 address that belongs to your account (not to an instance) until you release it.

Think of it like a landline number that you keep even when you shift to a new flat — you just "port" it to the new address. Technically, AWS keeps a 1:1 NAT mapping at the Internet Gateway from the EIP to the private IP of the instance (its network interface), and you can change that mapping at any time.

Point Auto-assigned public IP Elastic IP
Survives stop/start No — new IP after start Yes
Owned by The instance (temporarily) Your AWS account
Move to another instance Not possible Yes — re-associate in seconds
Default limit — 5 per region per account (a service quota; you can request more)
Charges Public IPv4 is charged hourly Public IPv4 is charged hourly — whether attached or idle

Public IPv4 addresses cost money

AWS charges an hourly fee for every public IPv4 address, including Elastic IPs — attached to a running instance, attached to a stopped instance, or just sitting unassociated in your account. Check the current rate on the Amazon VPC pricing page. Lakshat theva: release EIPs you no longer need.

Allocate and associate (console)

Step 1 — Allocate: EC2 → Network & Security → Elastic IPs → Allocate Elastic IP address → Network border group: your region (e.g. ap-south-1) → Public IPv4 address pool: Amazon's pool of IPv4 addresses → add tag Name=web-eip → Allocate.

Step 2 — Associate: select the EIP → Actions → Associate Elastic IP address → Resource type Instance → choose the instance (and its private IP) → Associate.

Step 3 — Verify: the instance's Public IPv4 address now shows the EIP. From the server:

curl -s https://checkip.amazonaws.com       # should print the Elastic IP

The old public IP is gone

When you associate an EIP, the instance's previous auto-assigned public IP is released immediately. Reconnect SSH using the new Elastic IP. If you later disassociate the EIP, the instance gets a fresh auto-assigned IP (if its subnet setting allows) — not the old one.

Server replacement with zero DNS change

This is the most useful real-world trick with Elastic IPs. Suppose web-1 is old and you have built a new, upgraded web-2:

  Before:  www.example.com ──► EIP 13.232.50.10 ──► web-1 (old)
  After:   www.example.com ──► EIP 13.232.50.10 ──► web-2 (new)      DNS record unchanged!
  1. Build and test web-2 using its own temporary public IP (or an AMI of web-1, see 14.2).
  2. Select the EIP → Actions → Associate Elastic IP address → choose web-2 → tick Allow this Elastic IP address to be reassociated → Associate.
  3. Traffic now goes to web-2 within seconds. Keep web-1 stopped for a day as a rollback option, then terminate it.

Disassociate and release

  • Disassociate: select the EIP → Actions → Disassociate Elastic IP address. The EIP stays in your account (still charged).
  • Release: select the EIP → Actions → Release Elastic IP addresses → Release. The address goes back to AWS; you usually cannot get the same IP again.

AWS CLI equivalents (optional)

aws ec2 allocate-address --domain vpc
aws ec2 associate-address --instance-id i-0123456789abcdef0 --allocation-id eipalloc-0abc1234def567890
aws ec2 associate-address --instance-id i-0fedcba9876543210 --allocation-id eipalloc-0abc1234def567890 --allow-reassociation
aws ec2 describe-addresses --output table
aws ec2 disassociate-address --association-id eipassoc-0123abcd4567ef890
aws ec2 release-address --allocation-id eipalloc-0abc1234def567890

Ravindra Bagale's Tip

Attach the Elastic IP before you create DNS records (Chapter 9), install WordPress or configure server_name — then nothing ever needs to change. And when you finish a lab, do a quick "EIP audit": EC2 → Elastic IPs — anything without an associated instance should be released. That's all, and your bill stays clean.