How to find the IP address of an amazon EC2 instance on reboot
Asked Answered
P

11

48

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

Pattison answered 23/9, 2011 at 7:34 Comment(1)
Why don't you assign a fixed elastic IP?Satterfield
H
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.

Hypnotherapy answered 23/9, 2011 at 8:57 Comment(3)
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.Conciliar
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)Squint
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!Hypnotherapy
B
105

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

Brey answered 24/9, 2011 at 1:16 Comment(1)
This is a solid complete answer. The "wget" provides the specific correct answer and the surrounding prose is also helpful/useful.Antigen
S
53
curl http://169.254.169.254/latest/meta-data/public-ipv4
Spectrometer answered 15/5, 2012 at 5:8 Comment(4)
This should be the accepted answer, and is also what they say in the docs. docs.aws.amazon.com/AWSEC2/latest/UserGuide/…Periodontal
What is that weird IP? Is it a security threat to call this unknown IP from one's server? :D I suggested an edit explaining that IP and looks like it wasn't acceptedAphesis
@RavindranathAkila no, you can use this command from inside your EC2 instanceWhiteness
This no longer works, they've updated the metadata API apparently.Amphibole
D
43

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
Demure answered 4/4, 2016 at 11:38 Comment(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.Indra
H
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.

Hypnotherapy answered 23/9, 2011 at 8:57 Comment(3)
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.Conciliar
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)Squint
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!Hypnotherapy
B
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

Bea answered 11/2, 2014 at 3:4 Comment(1)
Works on CentOS 7Vindication
S
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());
         }     
    }
}
Sirkin answered 8/8, 2014 at 9:56 Comment(0)
N
1

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

EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicHostname();
EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicIPv4s();
Nidus answered 13/7, 2015 at 10:1 Comment(0)
H
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

Harlie answered 23/9, 2011 at 8:9 Comment(0)
M
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;
                    }
                }
Mechanician answered 12/2, 2016 at 8:18 Comment(0)
U
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>
Unfriended answered 15/10, 2018 at 9:23 Comment(0)
L
0

I was using domain=$(hostname | cut -d "." -f1) however that was getting the ip as ip-1-2-3-4. Are you not able to use hostname -i

Luteous answered 21/6, 2021 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.