4

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!

smac2020
  • 9,637
  • 4
  • 24
  • 38
Susmitha
  • 259
  • 3
  • 14

2 Answers2

2

So the idea is that Assuming Roles is not application part, it's the infra service where your application is executing on.

For e.g.: If you have Spring Boot application running on EC2 (or Fargate, or Lambda, or Elastic Beanstalk or anywhere in AWS) that EC2 should have assumed the role. The "role" then should have rights to access SQS (or any service). Now when your application will try to use SQS running on EC2 with right role, everything will be fine.

If you're testing the code on your machine then it will not work as your machine has not assumed the role.

Hussain Mansoor
  • 2,934
  • 2
  • 27
  • 40
2

"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

smac2020
  • 9,637
  • 4
  • 24
  • 38
  • Thanks for your information. "I didn't get how to use the cerdentials from AWSCredentialsProvider." -- Meaning of this is, in the link which I have provided, according to the first answer code will return the awsCredentialsProvider which will have the credentials created from roles. So my question was did I can use these and if yes, how can I use? – Susmitha May 11 '22 at 14:32
  • 1
    First make sure you are using AWS SDK for V2. Then you can use the various cred providers that I showed in the above answer. To make it really easy - try a StaticCredentialsProvider that I showed in the above answer. That works fine. Also Read about creds here: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html – smac2020 May 11 '22 at 15:11