How to overcome issues due to dynamic IP's in AWS EC2 instances(part-1) ?

By default all the AWS EC2 instances have a dynamic IP attached to them and it is subjected to change when the instance was shutdown[Stop] for considerable time. When you shutdown your EC2 instance it current internal private IP address is return to IP pool and you will get new internal private IP address once you up[Start] the instance back. If you Reboot your EC2 instance there will be a chance remain your IP as it is(i have experienced it).

In our normal staging/production server deployments and configurations we were dealing with Static IP addresses or hosts names configured with hosts file as a key value pair located in,

Linux : /etc/hosts

Windows : C:\Windows\System32\drivers\etc

So, if you are planning to migrate your existing or  new applications to AWS cloud, you need to follow design & deployment strategy to overcome issue due to  Dynamic IP's.

We can overcome above issue by writing script to access AWS infrastructure services to get latest IP information and update hosts file automatically. This post will describe a solution to overcome dynamic IP issue, with required additional steps based on following article(s).

Reference(s) :


To access  AWS infrastructure you need to create user  and grant required privileges/permissions. Since, our requirement is to get latest IP information we will create user to access Amazon EC2 service with read only permission.

Note : We no need to create group and assign user to any group. Just need to update Permissions.

Steps :
1. Create unprivileged user [ec2-reader] as described in How to create unprivileged user using AWS IAM ?.

NOTE : Remember to download the user credentials.csv file.

2. Your credentials.csv file will contain User Name, Access Key Id and Secret Access Key like this.


View source
"User Name","Access Key Id","Secret Access Key"

3. Create custom user policy as  describe in How to assign policies to user in AWS IAM ? With EC2 instance read only permission and assign to user. Then ec2-reader is able to retrieve information about  running instances and nothing else.

4. Now we can access AWS infrastructure services to get EC2 instance detail using created user account through certain API's provided by AWS. You can download PHP, Java, Android, Python , Ruby and .Net development tools form AWS official web site.

5. I will explain how to access  AWS infrastructure services using PHP scripts. To run this script you also need the Amazon PHP SDK to be installed in the machine that you are going to execute the script.

I have done small change to the script given in reference [1] to print instance alias name with private IP address instead of instance ID as shown below.

get-internal-ips php script

View source
  1. #!/usr/bin/php
  2. <?php
  3. require_once( 'AWSSDKforPHP/sdk.class.php' );
  5. $oEC2 = new AmazonEC2( 'AKIAI73BNJ2ESTUDENTS', 'X3V0XXstV8EXNfSpuG68q5sroJzbBu9/eStuDents+' ); # ip-reader credentials
  6. $oEC2->set_region( AmazonEC2::REGION_US_E1 );
  8. $oResponse = $oEC2->describe_instances();
  9. if( !$oResponse->isOK() ) {
  10. exit( 1 );
  11. }
  13. foreach( $oResponse->body->reservationSet->item as $oReservationSet ) {
  14. //print_r($oReservationSet);
  15. $sInstanceState = $oReservationSet->instancesSet->item->instanceState->name;
  16. $alias = $oReservationSet->instancesSet->item->tagSet->item->value;
  18. if( !strcmp( $sInstanceState, 'running' ) ) {
  19. $sPrivateIpAddress = $oReservationSet->instancesSet->item->privateIpAddress;
  20. echo "$sPrivateIpAddress\t$alias\n";
  21. }
  22. }

How it works ?
Line 1 : Your PHP binary installation directory. You can find it by executing following command and it will display the PHP binary installation location if you have already installed PHP.

ubuntu@mmm-monitor:~$ which php

Line 3: Include AWS PHP sdk libraries.

Line 5 : Create AmazonEC2 class instance with created user credentials. You have to update “Access Key Id","Secret Access Key" values according your  credentials.csv file.

Line 6 : Define the region where your instance are created.

Regions:US East (Virginia), US West (N. California), EU West (Ireland), Asia&nbsp;Pacific (Singap.).

Line 10 : If there is no valid response stop execution and exit.

Line 13-20 : Get the instance state and instance alias values, if the instance state is “running” print the instance private IP address and the alias followed by tab space.

6. I have created three instances in US East (Virginia) region and provide aliases as mmm-monitor, mysql-node1, mysql-node2. Host name of the instance is updated to its alias name.


Refer article How to create AWS EC2 instance ? To create EC2 instance if you don't have create instance  yet.

7. We can setup cron job to access AWS infrastructure and update hosts file, with EC2 instance current private IP address & alias. Since our host name is same as alias, you can access/configure other instance to communicate using hos name without knowing private IP address of that instance. Operating system will map hostnames to IP addresses via hosts file.

Please refer How to overcome issues due to dynamic IP's in AWS EC2 instances(part-2) ? For installation and configuration  instructions.

FaLang translation system by Faboba

Operating Systems

Database Systems

Software Languages

Mobile Application Development

Cloud Computing

Web/Application Servers

Software Development