Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 762
Flame_Phoenix Avatar asked Apr 15 '14 11:04

Flame_Phoenix


People also ask

How do I find the IP address of an EC2 instance in Java?

This may be achieved with AWS Java SDK. And this is how you do it: // Getting instance Id String instanceId = EC2MetadataUtils. getInstanceId(); // Getting EC2 private IP String privateIP = EC2MetadataUtils.

How do I find my instance ID?

On the Home page, select DB Instances. The Instances page opens. Filter for the DB instance that you want the ID for. The instance details page opens and displays the ID.

How do I find my EC2 instance URL?

Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances and select your instance. The following information is available on the Networking tab: Public IPv4 address — The public IPv4 address.


2 Answers

I would suggest/recommend the usage of the AWS SDK for Java.

// Resolve the instanceId
String instanceId = EC2MetadataUtils.getInstanceId();

// Resolve (first/primary) private IP
String privateAddress = EC2MetadataUtils.getInstanceInfo().getPrivateIp();

// Resolve public IP
AmazonEC2 client = AmazonEC2ClientBuilder.defaultClient();
String publicAddress = client.describeInstances(new DescribeInstancesRequest()
                                                    .withInstanceIds(instanceId))
                             .getReservations()
                             .stream()
                             .map(Reservation::getInstances)
                             .flatMap(List::stream)
                             .findFirst()
                             .map(Instance::getPublicIpAddress)
                             .orElse(null);

Unless Java8 is available, this will need more boilercode. But in the nutshell, that's it.

https://stackoverflow.com/a/30317951/525238 has already mentioned the EC2MetadataUtils, but this here includes working code also.

like image 167
knalli Avatar answered Oct 23 '22 21:10

knalli


You want the com.amazonaws.util.EC2MetadataUtils class from the aws-java-sdk.

like image 26
heldeen Avatar answered Oct 23 '22 21:10

heldeen