AWS Java SDK - Get EC2 instance info

0 votes

I have done some research (i.e. looking at the sample code posted here Managing Amazon EC2 Instances) but there is only a sample code for getting the Amazon EC2 instances for your account and region.

I tried to modify the sample and here is what I came up with:

private static AmazonEC2 getEc2StandardClient() { // Using StaticCredentialsProvider final String accessKey = "access_key"; final String secretKey = "secret_key"; BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); return AmazonEC2ClientBuilder.standard() .withRegion(Regions.AP_NORTHEAST_1) .withCredentials(new AWSStaticCredentialsProvider(credentials)) .build(); } public static void getInstanceInfo(String instanceId) { final AmazonEC2 ec2 = getEc2StandardClient(); DryRunSupportedRequest<DescribeInstancesRequest> dryRequest = () -> { DescribeInstancesRequest request = new DescribeInstancesRequest() .withInstanceIds(instanceId); return request.getDryRunRequest(); }; DryRunResult<DescribeInstancesRequest> dryResponse = ec2.dryRun(dryRequest); if(!dryResponse.isSuccessful()) { System.out.println("Failed to get information of instance " + instanceId); } DescribeInstancesRequest request = new DescribeInstancesRequest() .withInstanceIds(instanceId); DescribeInstancesResult response = ec2.describeInstances(request); Reservation reservation = response.getReservations().get(0); Instance instance = reservation.getInstances().get(0); System.out.println("Instance id: " + instance.getInstanceId(), ", state: " + instance.getState().getName() + ", public ip: " + instance.getPublicIpAddress() + ", private ip: " + instance.getPrivateIpAddress()); }

It is working fine but I wonder if it's the best practice to get info from a single instance.


 

Apr 25, 2022 in AWS by Aditya
• 7,680 points
897 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In AWS

0 votes
2 answers

Launching an EC2 Instance using AWS SDK Java

package com.ec2application.ec2application; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.ec2.AmazonEC2; import ...READ MORE

answered Jan 22, 2019 in AWS by Priyaj
• 58,090 points
6,263 views
0 votes
1 answer

Stopping EC2 Instance using Java AWS SDK

You can follow the following steps:- 1. Create ...READ MORE

answered Jan 22, 2019 in AWS by Priyaj
• 58,090 points
1,376 views
0 votes
1 answer

Connecting AWS EC2 instance using Java

An Amazon EC2 instance is just like ...READ MORE

answered Mar 28, 2019 in AWS by Disha
3,845 views
+1 vote
1 answer
0 votes
1 answer

how to get aws account number /id based on EC2 instance which is hosted in amazon

You can obtain account number from an ...READ MORE

answered Feb 21, 2022 in AWS by Korak
• 5,820 points
1,676 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP