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 in AWS by Aditya
• 7,280 points
43 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,060 points
3,721 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,060 points
811 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
2,876 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 in AWS by Korak
• 4,460 points
102 views
+2 votes
3 answers

Is it possible to ping AWS EC2 instance

1.Go to EC2 Dashboard and click "Running ...READ MORE

answered Nov 13, 2020 in AWS by shivam
• 140 points

edited Nov 13, 2020 by Gitika 10,473 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP