Multiple AWS Cred in Java Spring

In my POC project, we would like to use S3 from one AWS user account (AWS api key set 1) and SQS from other AWS user account (AWS api key set 2)?

I checked article below:
https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/credentials.html

As per this article we can build credential provider, but (as per my understanding) often credential provider is global value in java application… Can we use multiple key sets in single Spring boot application?

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Credentials Providers in Spring are not global, but you can make them global by creating a Bean. Instead what you can potentially do is create two separate components: one for SQSClient another for S3Client, both configured using a different AWS Account and then autowire them as per need. import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration public class AwsClientConfig { //AWS Account 1: region1/accessKey1/secretKey1 @Bean public AmazonSQS awsSQSClient(SqsConfig sqsConfig) { AmazonSQS sqs = AmazonSQSClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey1, secretKey1))) .withRegion(Regions.fromName(region1)) .build(); return sqs; } //AWS Account 2: region2/accessKey2/secretKey2… Read more »