AWS
EC2
IP Address
Cloud Computing
Instance Management

An IP address of EC2 instance gets changed after the restart

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

An EC2 instance's IP address is a crucial component for network communications, as it uniquely identifies the instance on a network. However, many users notice that when an EC2 instance is stopped and then restarted, the IP address often changes. This article delves into why this occurs, the types of IP addresses in AWS, and how to manage them effectively.

Understanding IP Addressing in AWS EC2

Types of IP Addresses

AWS Elastic Compute Cloud (EC2) instances are typically associated with two types of IP addresses:

  1. Public IP Address:
    • Assigned automatically to instances in a default or custom subnet with an associated internet gateway.
    • Changes when the instance is stopped and started again.
  2. Private IP Address:
    • Assigned to instances within a Virtual Private Cloud (VPC).
    • Remains the same when an instance is stopped and started.

AWS also provides an Elastic IP that users can associate with an instance to ensure the public IP remains constant.

Why IP Address Changes on Restart

When an EC2 instance is stopped, the public IP is released back to the AWS public IP pool. By default, AWS instances have dynamic public IPs allocated via DHCP. Upon restarting, the instance requests a new IP allocation, and thus, a new public IP address is assigned.

This behavior occurs because:

  • Dynamic Allocation: AWS dynamically allocates IP addresses from a pool based on availability and the customer's region.
  • Cost Efficiency: Releasing unused IPs helps AWS optimize their pool's availability and performance.

Elastic IP as a Solution

Elastic IP addresses prevent the changing of public IPs across restarts. Here’s how they work:

  • Static IP: Elastic IP does not change on instance stop/start.
  • Reassociation: Once allocated, an Elastic IP can be reassociated with different instances/life-cycles, maintaining a consistent entry point.
  • Cost Allocation: Elastic IPs are free when associated with a running instance but incur costs if unattached.

Technical Implementation Examples

Assigning an Elastic IP

  1. Allocate an Elastic IP in the AWS Management Console:
    • Navigate to EC2 Dashboard > Network & Security > Elastic IPs.
    • Click "Allocate new address," then "Allocate," and note the allocated IP.
  2. Associate the Elastic IP with your running instance:
    • Select the newly allocated IP, click "Associate."
    • Choose your instance and click "Associate."

Reassociating an Elastic IP

To move your Elastic IP to another instance:

  • Disassociate from the current instance.
  • Associate with a new instance using the steps above.

Implications and Considerations

  • Cost Management: Be mindful of the costs associated with Elastic IPs. They are free only when attached to a running instance.
  • Network Architecture: Plan the usage of static IPs based on architecture needs — especially for fixed entry points for external communications.
  • Security: Ensure proper IAM permissions for managing Elastic IP to safeguard against unauthorized changes.

Summary Table

Key AspectDetailsConsiderations
Public IPChanges after restartUse Elastic IP for consistency
Private IPRemains constant within a VPCIdeal for internal communications
Elastic IPStatic; reassociableCosts if unattached, manage wisely
Allocation/AssociationSteps needed to bind a static public IP to instanceRequired for persistent public presence

Additional Considerations

  • Automating IP Management: Use AWS SDKs or CLI to automate the process of allocating and associating Elastic IPs, especially in dynamic scaling scenarios.
  • IPv6 Considerations: With increasing adoption of IPv6, consider configuring IPV6 addresses which have different allocation considerations.
  • Cross-Region Deployment: Elastic IPs are region-specific. Plan IP allocation strategies across regions if deploying multi-region architectures.

In conclusion, while EC2's default behavior assigns dynamic public IPs, AWS offers tools like Elastic IP to maintain IP consistency. Understanding the nuances of AWS IP addressing can help optimize cost, performance, and usability of cloud resources.


Course illustration
Course illustration

All Rights Reserved.