title: Get instance-id of EC2 instance via Java
body: <p>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.</p>
  
  <p><strong>Objective:</strong></p>
  
  <ul>
  <li>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.</li>
  </ul>
  
  <p>After reading Amazon documentation I came up with a Java method that returns the IP of all instances, <strong>but this is not what I want</strong>, I want a method that returns only the instance-id or the public IP address of the running instance.</p>
  
  <pre><code>    /**
       * 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&lt;String&gt; getPublicIPs(){
          List&lt;String&gt; publicIpsList = new LinkedList&lt;String&gt;();
  
          //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&lt;Reservation&gt; reservations = result.getReservations();
  
          List&lt;Instance&gt; 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;
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>