46

On reboot, the IP address of an amazon instance changes. How to find the new IP address using java API?

figaro
  • 2,105
  • 3
  • 19
  • 25

10 Answers10

104

On reboot, the IP addresses of an EC2 instance do not change. They do generally change on stop/start of a non-VPC EBS boot instance.

See my answer to your related question here:

https://stackoverflow.com/questions/7533871/difference-between-rebooting-and-stop-starting-an-amazon-ec2-instance

That said, you can find the private and public IP addresses through the API call for DescribeInstances in your particular language.

If you are on the instance itself, you can also find the IP addresses through the user-data API using simple HTTP:

http://instance-data/latest/meta-data/local-ipv4
http://instance-data/latest/meta-data/public-ipv4

For example,

wget -qO- http://instance-data/latest/meta-data/public-ipv4

Elastic IP addresses are recommended for keeping a consistent (static) externally facing IP address for a particular service or server. These need to be re-assigned to an instance after a stop/start (but not after a reboot).

Community
  • 1
  • 1
Eric Hammond
  • 21,255
  • 5
  • 62
  • 72
  • 1
    This is a solid complete answer. The "wget" provides the specific correct answer and the surrounding prose is also helpful/useful. – StephenBoesch Feb 20 '15 at 17:57
51
curl http://169.254.169.254/latest/meta-data/public-ipv4
buley
  • 24,595
  • 17
  • 76
  • 99
40

The public IPv4 address is also available from the EC2 instance like so:

curl checkip.amazonaws.com

And the public hostname is:

dig -x $(curl -s checkip.amazonaws.com) +short
Tushar Gupta - curioustushar
  • 54,013
  • 22
  • 95
  • 103
holmb
  • 532
  • 1
  • 5
  • 10
  • 1
    this actually worked better. The above answer /public-ipv4 was working until recently, unsure if AWS changed something or restricting traffic through the ELB made a difference, but I was getting my elastic IP reported back on both instances. The curl checkip however did it correctly, my elastic ip on one and a random assigned on the other. – Kris White Sep 28 '16 at 02:52
5

Assuming you don't want to assign an Elastic IP address (and there are reasons why this is not always a solution) then simply call DescribeInstances on the rebooted instance, which returns a bunch of information including Public IP Address.

Here's the AWS EC2 Java API Documentation on the topic.

Eight-Bit Guru
  • 9,109
  • 2
  • 47
  • 60
  • I upvoted you Eight-Bit-Guru. Do you know of any easy way to get the InstanceID of the last created instance for immediately associating an ElasticIP? To my knowledge it requires parsing the returned info from `ec2-run-instances` or parsing JSON using bash with the return JSON from `aws ec2 run-instances`. Trying to avoid additional dependencies. – JohnAllen Aug 18 '14 at 21:14
  • 1
    To clarify the `DescribeInstances` command above, I like to use this: `aws ec2 describe-instances | grep "Value\|PublicIpAddress"` (of course you'll need the aws cli installed) – modulitos Mar 30 '16 at 20:54
  • Why thank you, kind anonymous downvoter. Your reason for chipping my rep down slightly by chastising my six-year-old accepted answer is so blatantly obvious that no comment is required! – Eight-Bit Guru Jan 31 '17 at 19:21
3

this is how I did it on an Ubuntu 12.04 EC2 instance within a VPC:

curl ifconfig.me

not sure why but public-ipv4 was not found under the meta-data url

Amir Aliabadi
  • 181
  • 1
  • 4
1

In order to fetch the public IP of the instance you first need to get the instance id of that instance. You can get the instance id of the instance by using the following java code.

 List<Instance> instances = runInstancesResult.getReservation().getInstances();

 String instanceId = instances.get(0).toString().substring(13, 23);

And now in order to get the public IP you can use the following java code.

public void fetchInstancePublicIP() {
    DescribeInstancesRequest request = new   DescribeInstancesRequest().withInstanceIds("i-d99ae7d2");
    DescribeInstancesResult result= ec2.describeInstances(request);
    List <Reservation> list  = result.getReservations();

    for (Reservation res:list) {
         List <Instance> instanceList= res.getInstances();

         for (Instance instance:instanceList){

                 System.out.println("Public IP :" + instance.getPublicIpAddress());     
                 System.out.println("Public DNS :" + instance.getPublicDnsName());
                 System.out.println("Instance State :" + instance.getState());
                 System.out.println("Instance TAGS :" + instance.getTags());
         }     
    }
}
Sachin Gaykar
  • 129
  • 1
  • 2
1

They have util class which facilitates access to EC2 metadata. For instance

EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicHostname();
EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicIPv4s();
Igor
  • 153
  • 1
  • 8
0

you Can Use This.

                var ipofnewServer = string.Empty;    
                while (string.IsNullOrEmpty(ipofnewServer))
                {
                    var statusRequest1 = new DescribeInstancesRequest
                    {
                        InstanceIds = new List<string>() { instanceId }
                    };

                    var result = amazonEc2client.DescribeInstances(statusRequest1);
                    var status = result.Reservations[0].Instances.FirstOrDefault();
                    if (status != null)
                    {
                        ipofnewServer = status.PublicIpAddress;
                    }
                }
0

You can use CLI CRUD aplication to AWS resources. AWS-CRUD-Manager

To find all ec2 instances

./awsconsole.py ec2 all

To find all of data for one ec2 instances (including IP priv and public)

./awsconsole.py ec2 find -i <id-insta`ce>
Rafal
  • 389
  • 2
  • 9
0

Assuming your rebooting the instance and not launching from scratch than you can assign an elastic IP which always remains with the ec2 instance(unless you move the IP to another server). This allows you to point all your DNS to that one IP and not worry that a reboot will cause you issues.

I think thats what your asking but there are other things you could be asking. The internal IP of the server changes(if you relaunch the instance not reboot) and you can't 'reserve' it so you may need to create a script to keep you the new IP(if your pointing internal services to it).

hope that helps

Lostsoul
  • 21,503
  • 39
  • 122
  • 205