reassign private IP address – AWS

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.

Solution:

  1. Take AMI backup of running instance.
  2. 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.

Option 1

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

Option 2

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

Option 3

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”.

7 thoughts on “reassign private IP address – AWS

  1. It can be useful to assign multiple IP addresses to an instance in your VPC to do the following:

    Host multiple websites on a single server by using multiple SSL certificates on a single server and associating each certificate with a specific IP address.

    Operate network appliances, such as firewalls or load balancers, that have multiple IP addresses for each network interface.

    Redirect internal traffic to a standby instance in case your instance fails, by reassigning the secondary IP address to the standby instance.

  2. AWS reserves the first and the last private IP address in each subnet’s CIDR block so you do not have enough addresses left to launch all of the new EC2 instances.

    1. True, But this post will be applicable when Instance must needs to have specific private IP address. Reason could be anything like application bind with particular IP address.

  3. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

Comments are closed.