On reboot, the IP address of an amazon instance changes. How to find the new IP address using java API?
问题:
回答1:
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.
回答2:
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).
回答3:
curl http://169.254.169.254/latest/meta-data/public-ipv4
回答4:
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
回答5:
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
回答6:
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());
}
}
}
回答7:
They have util class which facilitates access to EC2 metadata. For instance
EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicHostname();
EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicIPv4s();
回答8:
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
回答9:
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;
}
}
回答10:
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>