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:
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?
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.
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.
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.
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.
You want the com.amazonaws.util.EC2MetadataUtils class from the aws-java-sdk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With