AWS Under the Hood - Day 3 - How does an EC2 instance get an IP address?
Tags: aws, devops, cloud, networking
Published: 2024-04-24 04:00:04

When an Amazon EC2 (Elastic Compute Cloud) instance is launched, it is assigned an IP address through Amazon's internal infrastructure mechanisms and standard networking protocols. Here is how it works:

**1: Instance Launch**
Launching an EC2 instance, you specify a subnet within a Virtual Private Cloud (VPC). The subnet is pre-configured with a range of IP addresses that can be assigned to instances within it.
**2: IP Address Allocation**
Upon instance launch, the Amazon EC2 service automatically assigns an IP address to the instance. Two types of IP addresses can be assigned:
_Private IP Address_: This is the primary IP address assigned to the instance and is used for communicating within the Amazon network, including with other EC2 instances. This IP address is associated with the instance for its lifetime and is only returned to the pool when terminated.
_Public IP Address_: This address is assigned and allows communication with the internet. It can change if the instance is stopped and restarted unless you use an Elastic IP, a static public IP address you can reserve and assign to instances as needed.
**3: DHCP (Dynamic Host Configuration Protocol)**
Amazon uses DHCP to assign the IP address to the instance within the subnet's IP range. The VPC has a DHCP option set that includes Amazon DNS servers and other DHCP options.
When the instance boots up, it sends a DHCP request message seeking an IP address and other network configuration details. The DHCP servers in Amazon's infrastructure receive the request and assign an IP address based on the subnet's address range, a default gateway, DNS server details, and other necessary configuration.
**4: Routing and DNS Configuration**
Routing: The subnet is associated with a route table that directs the traffic to and from the instances to the internet or other parts of the Amazon network. This includes a default route typically pointing to an Internet Gateway (if public) or other devices like NAT Gateways or Virtual Private Gateways.
DNS: Amazon's DNS servers help resolve domain names into IP addresses and are automatically provided to EC2 instances through DHCP.
**5: Elastic IP (EIP) Association (if applicable)**
If you have requested an Elastic IP (EIP) and associated it with the instance, this IP address takes precedence over the automatically assigned public IP. This association is managed by updating the network interface's IP address mapping on the EC2 platform.
**6: Security Group and Network ACLs Configuration**
Security groups and Network Access Control Lists (ACLs) are evaluated along with IP address configuration to determine the inbound and outbound rules that apply to the instance. These rules can influence which requests can be sent to or from the EC2 instance.

**How it works: Under the Hood**
Amazon's highly available and scalable infrastructure efficiently orchestrates these components, such as DHCP services, routing, DNS, and IP address management. This includes various control planes and data planes that manage network traffic, configuration data, and the operational state of each instance, ensuring a reliable and robust system.
By leveraging these processes and technologies, Amazon ensures that each EC2 instance is effectively integrated into the VPC's network environment and is ready for use after the launch process is completed.

📚 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

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