Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize AWS SDK in spring boot application?

I am writing a spring boot application which reads messages from SQS. I am able to run the application using environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. However, I was wondering it would be simpler to pass this configuration via a file similar to application.properties. How to achieve this?

like image 904
Adi Avatar asked Sep 19 '18 20:09

Adi


People also ask

How do I use AWS SDK without credentials?

To prevent the SDK from requiring the credentials, you need to use the makeUnauthenticatedRequest method to place any calls. This allows you to call "an operation on a service with the given input parameters, without any authentication data". var AWS = require('aws-sdk'); AWS.

How do I use AWS SDK profile?

Assume role with profile You can configure the AWS SDK for PHP to use an IAM role by defining a profile for the role in ~/. aws/credentials . Create a new profile with the role_arn for the role you will assume. Also include the source_profile of a profile with credentials that have permissions to assume the IAM role.

Can AWS deploy spring boot applications?

Amazon Web Services offers multiple ways to install Spring Boot-based applications, either as traditional web applications (war) or as executable jar files with an embedded web server. The options include: AWS Elastic Beanstalk. AWS Code Deploy.


1 Answers

In spring boot application you can access properties mentioned in application.yml file with @value annotation. You can create a service like this:

@Service
public class AmazonClient {  
    private AmazonSQS sqsClient;

    @Value("${amazonProperties.accessKey}")
    private String accessKey;
    @Value("${amazonProperties.secretKey}")
    private String secretKey;

    @PostConstruct
    private void initializeAmazon() {
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
        this.sqsClient = AmazonSQSClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .build();

    }
}

In application.yml file:

amazonProperties:
   accessKey: <your_access_key>
   secretKey: <your_secret_key>
like image 73
fatCop Avatar answered Oct 20 '22 00:10

fatCop