AmazonEC2Client describeInstances returns zero Reservations in Java

0 votes

While running "aws ec2 describe-instances" in command line, It gives list of all ec2 Instances but with Java AWS-SDK it's gives zero Reservations. Please see below code snippet,

 AmazonEC2 ec2;
 if (ec2 == null) {
    AWSCredentialsProviderChain credentialsProvider = new 
    AWSCredentialsProviderChain(
        new InstanceProfileCredentialsProvider(),
        new ProfileCredentialsProvider("default"));

      ec2 = new AmazonEC2Client(credentialsProvider);
  }

 for (Reservation reservation : ec2.describeInstances().getReservations()) {
         for (Instance instance : reservation.getInstances()) {
                System.out.println("TAG" + instance.getInstanceId());

      } 
   }

Oct 10, 2018 in AWS by eatcodesleeprepeat
• 4,710 points
623 views

1 answer to this question.

0 votes

This is the code in Java 8 which I use to describe all instances from all the regions:

    amazonEC2.describeRegions().getRegions().forEach(region -> {
        System.out.println("Region : " + region.getRegionName());

        amazonEC2 = AmazonEC2ClientBuilder.standard().withCredentials(awsprovider).withRegion(region.getRegionName()).build();

        amazonEC2.describeInstances().getReservations().forEach(reservation -> {
            reservation.getInstances().forEach(instance -> {
                System.out.println(instance.getInstanceId());
            });
        });
    });
answered Oct 10, 2018 by Priyaj
• 58,090 points

Related Questions In AWS

0 votes
1 answer

Unable to send email from Amazon EC2 Server in Java

It mostly could be due to two ...READ MORE

answered Nov 30, 2018 in AWS by Archana
• 5,640 points
1,115 views
0 votes
1 answer

Terminating an EC2 Intsance in Java

You can terminate the EC2 Instance in ...READ MORE

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

Compile AWS function in Java

Standard tools like Maven or Gradle can ...READ MORE

answered Jan 29, 2019 in AWS by Arunima
726 views
0 votes
1 answer

Using Shapely on AWS Lambda with Python 3

For some reason, the pip install of ...READ MORE

answered Oct 8, 2018 in AWS by Priyaj
• 58,090 points
2,594 views
0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers

How do I get my AWS Glue client in JAVA?

Hey, you've been using a correct code ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,670 points
4,427 views
+3 votes
3 answers
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