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,670 points
65 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
• 56,520 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,560 points
92 views
0 votes
1 answer

Terminating an EC2 Intsance in Java

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

answered Jan 22 in AWS by Priyaj
• 56,520 points
100 views
0 votes
1 answer

Compile AWS function in Java

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

answered Jan 29 in AWS by Arunima
39 views
0 votes
1 answer
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,260 points
891 views
+3 votes
3 answers