Ravindra BagaleCourses & study guides मराठी Track your progress

Chapter 5: Amazon EC2 (Elastic Compute Cloud)

5.5 Elastic IP (static public IP)

1
AllocateGet a public IPv4 from AWS into your account
2
AssociateAttach it to your EC2 instance (or ENI)
3
UseIP stays the same after stop/start; put it in DNS
4
DisassociateUnlink it; it still sits in your account (still billed)
5
ReleaseGive it back to AWS so you stop paying

Why a normal public IP changes

Why

Priya in Pune runs a small shop site on one EC2 server. She stops the server at night to save money. In the morning the site URL is dead. Friends in Berlin still open the old IP. That IP is gone.

What

A public IPv4 address is an address the whole internet can reach. When AWS gives your instance a normal (auto-assigned) public IP, that IP belongs to the running instance only for a while.

  • Stop, then start → AWS takes the old public IP back. You usually get a new one.
  • Reboot (restart the OS) → the public IP stays the same.
  • The private IP inside the VPC does not change on stop/start.

How — see it yourself

  1. Open the EC2 console → Instances → pick your instance.
  2. Copy the Public IPv4 address (example: 13.233.45.67).
  3. Instance state → Stop instance → wait until stopped → Start instance.
  4. Look at Public IPv4 again. It is often a different number.

Example — before and after stop/start

# On the server, before stop:
curl -s https://checkip.amazonaws.com
# Sample output:
# 13.233.45.67

# Stop the instance in the console, then Start it.
# SSH again (new IP!), then:
curl -s https://checkip.amazonaws.com
# Sample output:
# 65.2.110.88

What it means: the address changed. Any DNS A record or bookmark that used 13.233.45.67 now fails.

Full ops story, including swapping servers without changing DNS, is in lesson 14.1 Elastic IP in depth.

What an Elastic IP is

Why

You need one public address that does not jump around when you stop and start the server.

What

An Elastic IP (EIP) is a static public IPv4 address that AWS allocates to your account in one Region. It stays yours until you release it. You can move it from one instance to another in the same Region.

  • Elastic IPs are IPv4 only. AWS does not offer Elastic IP for IPv6. IPv6 on EC2 uses a different model (assign an IPv6 address to the ENI; it is already fixed).
  • An EIP lives in one Region (for example ap-south-1). You cannot move the same EIP to another Region.
  • BYOIP (Bring Your Own IP): if you bring your own public IPv4 range to AWS, addresses from that pool do not count toward the normal EIP quota and are not billed as AWS-provided public IPv4. Most beginners can ignore BYOIP.

How — mental model

  1. AWS holds a pool of public IPv4 addresses.
  2. Allocate = “this address is now in my account.”
  3. Associate = “map this address to this instance’s network interface.”
  4. Internet traffic to the EIP hits the Internet Gateway, then your instance’s private IP.

Example — names you will see

  • Allocation ID — like eipalloc-0a1b2c3d4e5f67890 (the EIP itself in your account).
  • Association ID — like eipassoc-0123456789abcdef0 (the link to an instance or ENI).
  • Public IP — like 3.110.55.201 (what browsers and DNS use).

What it means: you release with the allocation ID. You disassociate with the association ID. The public IP is the human-facing number.

Allocate and associate (console)

Why

Omar in Dubai wants shop.example.com to keep working after every stop/start. He needs an EIP on his instance first.

What

Two console actions: Allocate Elastic IP address, then Associate Elastic IP address.

How — numbered steps

  1. Open the EC2 console in your Region (example: Mumbai ap-south-1).
  2. Left menu → Network & Security → Elastic IPs.
  3. Click Allocate Elastic IP address.
  4. Public IPv4 address pool: Amazon’s pool of IPv4 addresses (default).
  5. Optional tag: Name = mauli-web-eip (Mauli Dairy lab name).
  6. Click Allocate. Note the new Public IP and Allocation ID.
  7. Select that EIP → Actions → Associate Elastic IP address.
  8. Resource type: Instance → choose the instance → choose its private IP → Associate.
  9. Open the instance page. Public IPv4 address should now show the Elastic IP.

Example — what you should see

  • Elastic IPs list: 3.110.55.201 · Associated instance ID · Allocation ID eipalloc-…
  • Instance summary: Public IPv4 = 3.110.55.201 (same number).

What it means: the old auto-assigned public IP is gone. SSH and HTTP must use the new EIP from now on.

Note: when you associate an EIP, AWS releases the instance’s previous auto-assigned public IP. You cannot turn that old auto IP into an EIP.

Same job with AWS CLI

Why

Scripts and exams expect the CLI. Same ideas: allocate, associate, describe.

What

Main commands: allocate-address, associate-address, describe-addresses.

How

  1. Allocate in the VPC domain (today every EIP is VPC):

Example — allocate

aws ec2 allocate-address --domain vpc
{
  "PublicIp": "3.110.55.201",
  "AllocationId": "eipalloc-0a1b2c3d4e5f67890",
  "Domain": "vpc"
}

What it means: 3.110.55.201 is now in your account. Nothing is attached yet. You are already being billed for this public IPv4.

  1. Associate it to an instance:

Example — associate

aws ec2 associate-address \
  --instance-id i-0abc123def4567890 \
  --allocation-id eipalloc-0a1b2c3d4e5f67890
{ "AssociationId": "eipassoc-0123456789abcdef0" }

What it means: traffic to 3.110.55.201 now reaches that instance.

  1. List what you own:

Example — describe

aws ec2 describe-addresses --output table

What it means: you see each PublicIp, AllocationId, InstanceId (if any), and AssociationId. Empty InstanceId = idle EIP (still billed).

Disassociate and release

Why

Anita finishes a lab in Hyderabad. If she only terminates the instance and forgets the EIP, the bill keeps growing.

What

  • Disassociate — unlink the EIP from the instance. The EIP stays in your account.
  • Release — return the address to AWS. Billing for that address stops (once released).

How — console

  1. EC2 → Elastic IPs → select the address.
  2. Actions → Disassociate Elastic IP address → confirm.
  3. Select it again → Actions → Release Elastic IP addresses → Release.

How — CLI

Example — disassociate then release

aws ec2 disassociate-address --association-id eipassoc-0123456789abcdef0
aws ec2 release-address --allocation-id eipalloc-0a1b2c3d4e5f67890

What it means: after release, describe-addresses no longer lists that AllocationId. Always disassociate before release if it is still attached.

Recover a released Elastic IP (sometimes)

Why

You released the EIP by mistake. DNS still points to 3.110.55.201. Can you get it back?

What

Often yes, if AWS has not given that address to someone else yet, and if recovering it would not push you over your EIP quota. You recover only with the API/CLI (not the console button). Tags on the old allocation are not restored.

How

  1. Run allocate again and pass the exact address:

Example — try to recover

aws ec2 allocate-address --domain vpc --address 3.110.55.201

What it means: success → you get a new AllocationId for the same public IP. Failure → another account took it, or you hit quota. Then you must pick a new IP and update DNS.

Official docs: Work with Elastic IP addresses (recover section).

Pricing and quota

Why

Many students still believe “first EIP is free while attached.” That old rule is gone.

What — price

From 1 February 2024, AWS charges $0.005 per hour for every public IPv4 address in your account — including:

  • auto-assigned public IPs on running instances,
  • Elastic IPs that are attached,
  • Elastic IPs that are idle (allocated but not associated).

Rough maths: one address ≈ 0.005 × 24 × 30 ≈ $3.60 per month.

EC2 Free Tier (first 12 months) includes 750 hours/month of public IPv4 with EC2. Other services and idle EIPs can still cost money. Always check the current page.

What — quota

Default quota: 5 Elastic IP addresses per Region per account. You can request more in Service Quotas. BYOIP addresses do not count against this quota.

How — stay cheap

  1. Prefer one EIP only when you truly need a fixed address.
  2. After every lab: Elastic IPs list → release unused ones.
  3. For many public apps, put an Application Load Balancer (or similar) in front and point DNS at the load balancer — see Chapter 18. For a name on one server, learn DNS in Chapter 9: Domain Names, DNS and Subdomains.

Example — when EIP vs load balancer / DNS

  • Use an EIP — single server, you SSH to a stable IP, or you must remap fast to a standby instance in the same Region.
  • Use a load balancer + DNS — public website with more than one instance, or you want HTTPS and health checks. Point the domain at the load balancer DNS name, not at a raw EIP.
  • DNS alone — if your DNS TTL is short and you accept a short delay when the IP changes, you can update the A record after stop/start. Still painful for beginners; EIP is simpler for one box.

Sources (pricing & EIP basics):

Common mistakes

  1. Release forgotten after terminate — instance is gone; EIP remains and bills you.
  2. Thinking reboot = stop/start — reboot keeps the auto public IP; stop/start does not.
  3. Associating in the wrong Region — EIP in us-east-1 will not attach to an instance in ap-south-1.
  4. Expecting IPv6 “Elastic IP” — there is no EIP for IPv6.
  5. Assuming you can never recover a released IP — you often can with allocate-address --address, but only while it is still free (see above and 14.1).
  6. Putting an EIP on every instance “just in case” — prefer load balancer + DNS for fleets.

Checklist before you leave the lab

  • I know stop/start changes auto public IP; reboot does not.
  • I allocated an EIP and associated it to my instance.
  • I verified with curl -s https://checkip.amazonaws.com.
  • I disassociated, then released, when the lab ended.
  • Elastic IPs list in this Region is empty (or only has EIPs I still need).

Ravindra Bagale's Tip

Attach the Elastic IP before you create DNS records (Chapter 9), install WordPress, or set server_name — then nothing needs to change later. After every lab, do a one-minute EIP audit: EC2 → Elastic IPs — release every address with no associated instance. Your bill stays clean.

Lab — pin a stable public IP on one EC2

Goal: prove that an Elastic IP survives stop/start, then clean up so you are not billed.

  1. Launch (or reuse) a free-tier EC2 instance in ap-south-1. Name it mauli-web-1.
  2. Copy its current auto public IP into a notepad.
  3. Allocate an Elastic IP. Tag Name=mauli-web-eip.
  4. Associate it with mauli-web-1.
  5. From your laptop: ssh -i your.pem ec2-user@THE_EIP (Amazon Linux) or the matching user for your AMI.
  6. On the server run: curl -s https://checkip.amazonaws.com — it must print the EIP.
  7. Stop the instance. Start it again. Confirm the Public IPv4 is still the EIP (not a new random IP).
  8. Disassociate the EIP. Release the EIP. Confirm the Elastic IPs list is empty in this Region.

Pass: IP stayed the same across stop/start while associated; list is empty after release.

Quick quiz

Five questions. Only what this lesson teaches. Click an answer to check.