Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to the AWS services using IAM roles ARN in a Spring Boot application

I am using AWS SQS, SNS, and S3 services. So for that i have created the roles and queues in aws. Now I have roles ARNs and Queues ARNs. How can I connect to these services through my spring boot app?

I have gone through this link, but i didn't get how to use the cerdentials from AWSCredentialsProvider. Please help me in this.

Thanks in advance!

like image 813
Susmitha Avatar asked Oct 16 '25 00:10

Susmitha


1 Answers

"I didn't get how to use the cerdentials from AWSCredentialsProvider."

I am going to answer this question using the recommended SDK - which is AWS SDK for Java V2. You may find V1 in old online content - but using V1 is not best practice.

There are different ways of handling creds when writing a Java App that uses AWS SDK for Java V2 - including a Spring BOOT app.

You can use an Environment variable provider:

 Region region = Region.US_EAST_1;
 RdsDataClient dataClient = RdsDataClient.builder()
               
   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
   .region(region)
   .build();

You can use the shared credentials and config files. This reads your creds from a Credential file located under .aws.

Region region = Region.US_EAST_1;
RdsDataClient dataClient = RdsDataClient.builder()
                          .region(region)
                          .build(); 

You can use a StaticCredentialsProvider where you put your creds in the code.

 AwsBasicCredentials credentials = AwsBasicCredentials.create("<KEY>", "<Secret Key>");
    StaticCredentialsProvider staticCredentials = StaticCredentialsProvider.create(credentials);
    Region region = Region.US_EAST_1;
     DynamoDbClient ddb = DynamoDbClient.builder()
                    .region(region)
                    .credentialsProvider(staticCredentials)
                    .build();

All of these credential ways are explained in the AWS Java V2 Developer Guide -- which I strongly recommend that any developer programming with the AWS SDK for Java V2 SDK read.

Finally, you will find code examples of writing a Spring BOOT example with the AWS SDK for Java v2 in the AWS Github code repo. For example.

Creating your first AWS Java web application

This creates an example Spring Boot web app that submits data to an Amazon DynamoDB table.

enter image description here

like image 125
smac2020 Avatar answered Oct 18 '25 14:10

smac2020



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!