Skip to Content

IP addressing

Private and public IPv4 addresses, what happens to them when an instance stops, and Elastic IP addresses.

What it is

Every device on a network needs an IP address so others can find it. There are two versions:

  • IPv4, such as 1.160.10.240: four numbers from 0 to 255. It allows about 3.7 billion public addresses, which the world has nearly run out of. Still the most common.
  • IPv6, such as 3ffe:1900:4545:3:200:f8ff:fe21:67cf: a much larger address space, useful for the Internet of Things and growing fast.

The course, and most exam questions about EC2, use IPv4.

Private vs public IP addresses

  • A public IP address identifies a machine on the internet. It must be unique worldwide, and it can be roughly geo-located.
  • A private IP address identifies a machine only inside a private network. It must be unique within that network, but two different companies can use the same private addresses. Only certain ranges are private: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.

Machines with only a private IP reach the internet through a gateway that has a public IP (a NAT device). That's how a whole office network can share one public address.

IP addresses on an EC2 instance

By default an instance in a default VPC gets:

  • a private IPv4 address from its subnet, used inside the AWS network;
  • a public IPv4 address, used to reach it from the internet.

SSH goes to the public IP, because a local computer isn't on the same private network. The private IP is what other instances in the VPC use.

Across a stop and start, the private IP stays the same; an auto-assigned public IP is released and replaced; an Elastic IP stays the same and is still billed Across a stop and start, the private IP stays the same; an auto-assigned public IP is released and replaced; an Elastic IP stays the same and is still billed

What survives a stop and start.
  • The private IP stays with the instance until it's terminated, through stops and starts.
  • An auto-assigned public IP is released when the instance stops (or hibernates), and the instance gets a new one when it starts. Anything that remembered the old address breaks.
  • An Elastic IP stays attached through a stop and start, so the public address never changes. It's still billed while the instance is stopped.

Elastic IP addresses

An Elastic IP address is a static public IPv4 address that belongs to the account until it is released.

  • It is allocated to the account, then associated with one instance (or network interface) at a time.
  • It can be remapped to another instance in seconds, which hides the failure of an instance or its software from users.
  • It belongs to one Region.
  • The default limit is 5 per Region (an increase can be requested).
  • It costs $0.005 an hour whether it's attached or not, like every public IPv4 address.

Try to avoid Elastic IPs

Elastic IPs often point to a design that won't scale: one server that everyone connects to directly. Better options:

  • Give the instance a normal public IP and register a DNS name for it, updating the record when it changes.
  • Better still, put instances behind a load balancer, which has its own DNS name, and give the instances no public IP at all.

Elastic IPs remain the right answer when something outside the account's control needs a fixed IP, such as a partner's firewall allow-list.

Updated since the slides

Since February 2024 AWS charges $0.005 an hour for every public IPv4 address, attached or idle, Elastic or auto-assigned. Before, an Elastic IP attached to a running instance was free. That makes "avoid public IPs where you can" a cost point as well as a design one.

Exam tips

  • "Instance needs a fixed public IP that survives stop/start" → Elastic IP.
  • "Quickly move a public IP to a standby instance" → remap the Elastic IP.
  • "Public IP changed after restart" → auto-assigned public IPs change on stop/start; use an Elastic IP, DNS or a load balancer.
  • Private IPs don't change when an instance stops and starts.

Common traps

  • "Reboot changes the public IP." A reboot keeps it; a stop and start doesn't.
  • "An Elastic IP is free while attached." All public IPv4 addresses are billed now.
  • "Use an Elastic IP for each web server behind the load balancer." The load balancer is the entry point; the servers don't need public IPs at all.

Hands-on lab: watch the IPs change

Goal: see the public IP change on stop and start, then fix it with an Elastic IP.

Costs money

The saa-web instance (about $0.02 an hour) plus an Elastic IP ($0.005 an hour). Release the Elastic IP at the end: an idle one keeps billing.

Note the addresses

Select saa-web in the EC2 console and note its Private IPv4 address and Public IPv4 address.

Stop and start

Stop the instance, then start it. The private IP is the same; the public IP is different.

Allocate an Elastic IP

Open Network & Security → Elastic IPs → Allocate Elastic IP address, keep the defaults, add the tag Project=saa-lab, and allocate.

Associate it

Select the new address, choose Actions → Associate Elastic IP address, pick saa-web and associate. The instance's public IP is now the Elastic IP, and the web page loads on it.

Stop and start again

Stop and start the instance once more. This time the public IP doesn't change.

Expected result: an auto-assigned public IP changes on every stop and start; an Elastic IP doesn't.

Clean up: Actions → Disassociate Elastic IP address, then Actions → Release Elastic IP addresses. Terminate saa-web if you're not doing more labs today.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt