Springboot with AWS SNS

Viewed 474

Actually Iam trying to set email subscription to SNS topic in AWS through springboot and publish the message to SNS topic. But facing issue in configuring the AWS access key and secret key. Do I need to create a separate IAM user for accessing SNS or it is fine to have a IAM user already created with administrator access to SNS topic. Below is the configuration file I created.

@Configuration public class AmazonSNSConfiguration {

@Bean
@Primary
public AmazonSNSClient amazonSNSClient() {
     return (AmazonSNSClient) AmazonSNSClientBuilder
                .standard()
                .withRegion(Regions.US_EAST_1)
                .withCredentials(
                        new AWSStaticCredentialsProvider(
                                new BasicAWSCredentials(
                                        "**********",
                                        "***********"
                                )
                        )
                )
                .build();
}

}

1 Answers

When working with Spring BOOT and the Java SNS V2 API, you can create an IAM role that has a policy to use SNS.

Also, there is no reason to Hard Code the key creds in the Java code. You can create an SNSClient object like this:

Region region = Region.US_WEST_2;
SnsClient snsClient = SnsClient.builder()
    .region(region)
    .build();

This code assumes you have set the creds in a file named credentials located in .aws. See this doc (under heading Configure credentials) for more information:

Get started with the AWS SDK for Java 2.x

Here is an AWS developer tutorial that steps you through How To create a Spring BOOT app that uses the SNS service to create a Sub/Pub app:

Creating a Publish/Subscription Spring Boot Application

Related