Day 26 – How does an EC2 instance get an IP address
For the time being, let’s forget about AWS and see how your laptop/desktop will get an ipaddress
Laptop → Router or Modem(Also serves as a DHCP server)
DHCP stands for Dynamic host configuration protocol and automatically assigns IP addresses and other network settings to a device. DHCP begins with a Layer 2 broadcast which allows it to discover a DHCP server on the local network. Once discovered, the DHCP server and the device known as a DHCP client communicate using Layer 2, which means frames containing MAC addresses. The result is the device obtaining Layer 3 networking information such as IP addresses, subnet masks, gateways, and other settings like NTP servers, domain names, and DNS.
So you have a laptop connected to a wireless Router(wifi). Your router will act as a DHCP server. Your device starts with a hard-coded MAC address. This is the layer 2 address, unique to a specific device, and consists of a vendor part and a device part. . When you connect new devices to WIFI, your device gets an IP address. That IP address and all the associated configuration for that IP address is assigned by the DHCP service running on your router.
DHCP will lease your device an IP address. It reserves one from the pool and other dhcp option sets)
Similarly, when you create a VPC, a DHCP option set is created.
- Go to the VPC console https://console.aws.amazon.com/vpc/home
- As you can see, with the default VPC, a DHCP option set is created.
- Click on DHCP option set.
- As you can see you can’t modify the default DHCP option set. The only option you have is View details, Manage tags and Delete DHCP option set.
- The default DHCP option set provides Domain name(ec2.internal), Domain name servers(AmazonProvidedDNS)(its a Route53 resolver and its VPC+2 address), and the default gateway, which is always subnet +1 address. If you want your custom value, you need to create a new dhcp option set and assign it to your VPC.
Private DNS Address Format: ip-private-ipv4-address.region.compute.internal
Public DNS Address Format: ec2-public-ipv4-address.region.compute.amazonaws.com
- Also, if you go to the EC2 console https://console.aws.amazon.com/ec2/ and check any of your EC2 instances, you will see that the Hostname and Private IP DNS name have the domain name(ec2.internal) assigned via the DHCP options set.
- If you want to modify any of these values, Go to the VPC console https://console.aws.amazon.com/vpc/home , click on Create DHCP option sets
- For this blog, I am only modifying the domain name as prashant.com. So give your DHCP option set name — optional some meaningful name(for e.g. my-demo-dhcp-option-set) and Domain name as prashant.com.
- Keep rest of the settings as default and click on Create DHCP option set.
- Go back to the VPC console and under Actions click on Edit DHCP option set
- Choose the new DHCP option set from the drop-down and click Save changes.
- Go to the EC2 console https://console.aws.amazon.com/ec2/ and create new EC2 instance https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html#ec2-launch-instance
- You may see the new changes after some time, and the reason for that is associating a new option set is immediate, but changes require a DHCP lease renew, which takes time. But if you go to the EC2 console of the instance and run the hostname command, you will see the changes.
Some points to remember
- Once you create a DHCP option set, you can’t modify it.
- One DHCP options set can be associated with 0 or more VPCs. But VPC can only have one DHCP option set associated with it.