Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have an AWS EC2 instance deployed as a server, 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?


EDIT

Really? Can someone explain me how a question about a virtualized server, in this case an EC2 AWS instance is off-topic when inside a forum to ask questions about servers?

This fully complies with the following two points of the rules:

  • Server and Workstation operating systems, hardware, and software;
  • Business/Enterprise grade virtualization.

Furthermore, there are plenty of questions about EC2 in this forum (even ones asking for programming answers) and the fact that someone actually answered my question proves my point - this is the right place to ask it.

I'm leaving this comment as a disapproval of your actions, it is people like you who drive new users away. I know I marked this as answered, but I am still doing it. Why? Because it is the principle that counts.

share|improve this question

closed as off-topic by Rex, Ward, Wesley, Mark Wagner, MichelZ Apr 16 at 10:43

  • This question does not appear to be about server, networking, or related infrastructure administration within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
This seems more like a programming question, which is off-Topic on serverfault. You might want to try StackOverflow.com –  MichelZ Apr 17 at 10:34
    
This is very plainly a programming question and should have been posted on Stack Overflow. You were not asking about administering the server at all. Since you did cross-post it there already, I am not migrating it. –  Michael Hampton Apr 17 at 13:42

1 Answer 1

up vote 1 down vote accepted

You can use the instance metadata documented here http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html

below I wrote a simple piece of java code which prints the running instance-id

import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public final class Main {

  public static void main(String[] args) throws Exception {
    URL url = new URL("http://169.254.169.254/latest/meta-data/instance-id");
    URLConnection conn = url.openConnection();
    Scanner s = new Scanner(conn.getInputStream());
    if (s.hasNext()) {
      System.out.println(s.next());
    } 
  }
}

The code must be executed from a running ec2 instance

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.