3

I am trying to use java sdk to get list of aws ec2 instances and s3 bucketsattached to a user. My main program class is

`

package com.aws.demo;

import java.util.List;




import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.Image;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;

@SpringBootApplication()
public class AwsApplication {

    public static void main(String[] args) {
        SpringApplication.run(AwsApplication.class, args);
        
        final String aws_access_key_id = ""; // deleted for security reasons
        final String aws_secret_access_key = ""; // deleted for security reasons
                
        
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_access_key_id, aws_secret_access_key);
        
        try {           AmazonS3 s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))                  .withRegion("us-east-1")
                    .build();           
            List<Bucket> buckets = s3.listBuckets();
            
            buckets.stream().forEach(bucket ->{
                System.out.println("Bucket Name : "+ bucket.getName()+", Bucket Owner : "+bucket.getOwner().getDisplayName()
                +", Bucket Creation Date: "+ bucket.getCreationDate());
            });
            
            AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder
                    .standard()
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                    .withRegion("us-east-1")
                    .build();
            List<Image> listEc2s = amazonEC2.describeImages().getImages();
            listEc2s.stream().forEach(image->{
                System.out.println("Image id : "+image.getImageId()+"Image owner : "+image.getImageOwnerAlias());
            });
                    System.out.println("Image list"+amazonEC2.describeImages().getImages());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getStackTrace().toString());
        }
            
        
        
    }
    
    

}

` The s3 buckets returns response of list of buckets but that of the ec2 instances throws the error below

Unable to execute HTTP request: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[29070,4] Message: Connection reset

my pom.xml file is

`

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aws-services</groupId>
    <artifactId>aws</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>aws</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.11.379</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-ec2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

` Please what can I do to return the correct response, the list of ec2 instances attached to my aws account

Oluchi
  • 41
  • 3

1 Answers1

1

I have fixed it. I used AmazonEC2AsyncClientBuilder instead of AmazonEC2ClientBuilder

Oluchi
  • 41
  • 3