AWS Under the Hood - Day 2 Why does an AWS EC2 instance lose its public IP address after a restart and how can this be managed?
Tags: aws, cloud, devops, ebs
Published: 2024-04-24 00:28:35

When you restart an Amazon Web Services (AWS) Elastic Compute Cloud (EC2) instance, it may lose its public IP address if that IP is dynamically allocated. This simple behavior can be understood through several key aspects of how AWS manages networking for EC2 instances.

**Dynamic IP Address Allocation**
AWS EC2 instances can either have a dynamic public IP address (known as a public IPv4 address) or a static one, known as an Elastic IP (EIP). By default, when an EC2 instance is launched in a public subnet within a Virtual Private Cloud (VPC), it receives a public IP address that is dynamically allocated. This address remains associated with the instance until it is stopped or terminated. Here's what happens under the hood:
1. Instance Launch: When you launch an EC2 instance, AWS assigns a public IP from the pool of available IP addresses in the region of your VPC. This IP is only tied to the instance for the duration that the instance is running.
2. Stopping the Instance: When you stop an instance, AWS dissociates the public IP address assigned to it. This is because the public IP is not meant to be a permanent resource; it is intended for temporary use while the instance is running. The IP is returned to the pool and can be reassigned to another instance.
3. Starting the Instance: When you start the instance again, AWS assigns a new public IP from the pool. Because the previous IP was released back to the pool, the instance will likely receive a different public IP upon restart.

**Elastic IPs for Persistence**
If you require a persistent public IP address that does not change across stops and starts, AWS provides Elastic IPs (EIPs). An EIP can be associated with an instance and will remain with it until you dissociate it manually. Here are the steps to use an EIP:
1. Allocate an EIP: First, allocate an EIP to your account from the Amazon pool of IPv4 addresses.
2. Associate the EIP: You then associate this allocated EIP with an instance. This association can be changed to a different instance at any time.
3. Reassociation: If the instance is stopped and restarted, you can either leave the EIP associated, ensuring that it retains the same public IP address, or you can associate it with another instance.

**Why Use Dynamic Public IPs?**
Dynamic public IPs are suitable for many applications, particularly those that:
They are used for temporary or elastic workloads.
Do not require a fixed IP address for DNS or other networking needs.
Benefit from cost savings, as dynamic IPs do not incur charges when the associated instance is stopped.

NOTE: AWS has now started charging for Elastic IPs https://aws.amazon.com/blogs/aws/new-aws-public-ipv4-address-charge-public-ip-insights/

**Best Practices**
It's advisable to use an elastic IP for production environments or services that require a stable IP address for connections or DNS settings. It ensures that your IP address remains consistent through restarts, aiding in managing applications and services dependent on a fixed IP.
By understanding these details, you can better plan and manage your AWS resources, particularly around networking and instance deployment strategies.

📚 If you're interested in more in-depth explanation of these topics, please check out my new book "Cracking the DevOps Interview"
https://pratimuniyal.gumroad.com/l/cracking-the-devops-interview?layout=profile

📚 To learn more about AWS, check out my book "AWS for System Administrators"
https://www.amazon.com/AWS-System-Administrators-automate-infrastructure/dp/1800201532/