Basically, the first public IP will be auto-assigned when launching the instance. After that, we create a secondary private IP and then associate an Elastic IP with it. Because we only use ONE Elastic IP, that IP is free for therunning instance according to EC2 pricing.
Here are the detailed steps:
Step 1: Create a new VPC instance in AWS console. At “Step 3: Configure Instance Details”, select a VPC and subnet. Be sure to take note of the subnet’s size (e.g. /24). Next, enable “Auto-assign Public IP”. Proceed to configure other settings. In the EC2 security group, allow SSH (port 22) and “ICMP Echo Request”. Finally, launch your new instance.
Step 2: Confirm that your new instance has finished launching by navigating to “Instances” from the menu on left. You should see a green checkmark with “2/2 checks passed”. Take note of the assigned public IP.
Step 3: Now navigate to “Network Interfaces” from the menu. Right-click on your new instance and select “Manage Private IP Addresses”.
Step 4: Click on “Assign new IP” in the popup window. A new line should appear with “Auto-assign”. Now click on “Yes, Update”. Take note of the assigned secondary private IP on the new line, e.g. 192.168.x.y.
Step 5: Allocate a new Elastic IP for attaching to the secondary private IP. Navigate to “Elastic IPs” from the menu. Click on “Allocate New Address”. In the popup that opens, choose “VPC”. Then confirm with “Yes, Allocate”. Take note of the assigned Elastic IP.
Step 6: Navigate to “Network Interfaces” from the menu on left. Right-click on your new instance and select “Associate Address”.
Step 7: In the “Associate Elastic IP Address” popup that opens, double check that the Elastic IP and secondary private IP both match those noted in Step 5 and Step 4, respectively. Confirm by clicking “Associate Address”.
Step 8: Now SSH into your new instance using the assigned public IP you noted in Step 2. Run command:
sudo ip addr add dev eth0 192.168.x.y/24
In the command above, replace 192.168.x.y with the private IP you noted in Step 4, and replace “/24” with your VPC subnet size you noted in Step 1. You may want to add this command to your
/etc/rc.local so that the second IP is automatically enabled following a reboot.
Now you can ping the second IP from your local PC to test it, and optionally run
sudo tcpdump -n icmp on the EC2 instance to observe the ping packets.
Congratulations! You have completed all the steps to set up a second public IP address for your EC2 instance.
Note: The “auto-assigned” public IP at instance launch is NOT reserved to your account, and will be released at instance stop/termination. Also, before starting a stopped instance, you must disassociate that Elastic IP in order to get a new auto-assigned public IP. You may reassociate it after launching.