Flame_Phoenix Flame_Phoenix - 6 months ago 181
Java Question

Get instance-id of EC2 instance via Java

I have an AWS EC2 instance deployed, and I need to find out its public IP. Howver, to know that I must first know the instance-id of my instance.

Objective:


  • I have a Java code running in my instance, and I want that code figure out the current IP or Instance-ID of the instance where it is being run.



After reading Amazon documentation I came up with a Java method that returns the IP of all instances, but this is not what I want, I want a method that returns only the instance-id or the public IP address of the running instance.

/**
* Returns a list with the public IPs of all the active instances, which are
* returned by the {@link #getActiveInstances()} method.
*
* @return a list with the public IPs of all the active instances.
* @see #getActiveInstances()
* */
public List<String> getPublicIPs(){
List<String> publicIpsList = new LinkedList<String>();

//if there are no active instances, we return immediately to avoid extra
//computations.
if(!areAnyActive())
return publicIpsList;

DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setInstanceIds(instanceIds);

DescribeInstancesResult result = ec2.describeInstances(request);
List<Reservation> reservations = result.getReservations();

List<Instance> instances;
for(Reservation res : reservations){
instances = res.getInstances();
for(Instance ins : instances){
LOG.info("PublicIP from " + ins.getImageId() + " is " + ins.getPublicIpAddress());
publicIpsList.add(ins.getPublicIpAddress());
}
}

return publicIpsList;
}


In this code I have an array with the instance-ids of all active instances, but I do not know if they are "me" or not. So I assume that my first step would be to know who I am, and then to ask for my public IP address.

Is there a change I can do to the previous method to give me what I want? Is there a more efficient way of doing it?

Answer

I am not a java guy. However, my below ruby code does print the Instance ID and Public IP of the running instances as you needed:

ec2.describe_instances(
  filters:[
    {
      name: "instance-state-name",
      values: ["running"]
    }
  ]
).each do |resp|
  resp.reservations.each do |reservation|
    reservation.instances.each do |instance|
        puts instance.instance_id + " ---AND--- " + instance.public_ip_address
    end
  end
end

All you need to do is find out respective Methods/calls in JAVA SDK documentation. The code that you should be looking at is:

  filters:[
    {
      name: "instance-state-name",
      values: ["running"]
    }

In above block, I am filtering out only the running instances.

AND

resp.reservations.each do |reservation|
        reservation.instances.each do |instance|
            puts instance.instance_id + " ---AND--- " + instance.public_ip_address

In above code block, I am running 2 for loops and pulling instance.instance_id as well as instance.public_ip_address.

As JAVA SDK and Ruby SDK are both hitting the same AWS EC2 APIs, there must be similar settings in JAVA SDK.

Also, Your question is vague in the sense, are you running the JAVA code from the instance whose Instance id is needed? OR Are you running the java code from a different instance and want to pull the instance-ids of all the running instances?

UPDATE:

Updating the answers as the question has changed:

AWS provides meta-data service on each instance that is launched. You can poll the meta-data service locally to find out the needed information.

Form bash promp, below command provided the instance-id and the public IP address of the instance

$ curl -L http://169.254.169.254/latest/meta-data/instance-id

$ curl -L http://169.254.169.254/latest/meta-data/public-ipv4

You need to figure out how you will pull data from above URLs in java. At this point , you have enough information and this question is irrelevant with respect to AWS because now this is more of a JAVA question on how to poll above URLs.