Each AWS instance comes with one primary private IP address assigned to first network interface named eth0. This primary IP address will remains unchanged throughout the life-cycle of that instance. AWS also allows assigning multiple secondary private IP address for same network interface. Re-allocating or Reassigning secondary addresses can be done straight away either through console or CLI.
Assume, application has dependency with primary private IP address. How to reassign primary private IP address to other instance?
Note: This article talks about only private IP addresses. No were referring to public IP / EIP.
- Take AMI backup of running instance.
- Terminate the instance. It will release the primary private IP address back to subnet pool. Assume the needed IP address is 10.20.30.100.
Now either of below three options should serve the need.
Assign the IP address as secondary private IP to existing instance. Assume existing instance is DR-prod-01 (i-00abcdefghjklm) with interface id as DR-eth0-xxx.
#aws ec2 assign-private-ip-addresses --network-interface-id DR-eth0-xxx --private-ip-addresses 10.20.30.100
If the instance supports, create new network interface with required IP set and attach to existing instance. Each instance type has different network interface attach limit. Refer this AWS document to find limits of your instance.
#aws ec2 create-network-interface --subnet-id subnet-9dxxxxx --description "Application network” --groups sg-903xxxxx --private-ip-address 10.20.30.100
From output, note network interface id (eni-e5xxxxx).
#aws ec2 attach-network-interface --network-interface-id eni-e5xxxxx --instance-id i-00abcdefghjklm --device-index 1
Choose this option if required IP address must to be primary private IP address. Simply, launch the new instance with primary private IP set as 10.20.30.100. Of-course, the new instance must be launched in same subnet where the IP address belongs to.
#aws ec2 run-instances --image-id <image> --instance-type c4.large --key-name <key> --private-ip-address 10.20.30.100 --count 1 --subnet-id subnet-9dxxxxx --security-group-ids sg-903xxxxx
Have anything to say, please post below. If you like the post comment as “+1”.