Send sms using aws simple notification service (SNS) and Spring boot

Photo by Alok Sharma from Pexels

In this blog post I'am going to demonstrate how to use aws Simple Notification Service to send SMS messages using aws java sdk with Spring boot. As in my previous blog post aws configurations are mostly similar, but I will go through the steps.
  • You need  to have an aws account, you can use aws free tier for this. If you don't have an aws account, set it up before you start. (https://aws.amazon.com/free/)
  • You will only get 1 dollar free SMS for start even for a paid account, you need to contact the AWS support and increase your SMS quota.
2) Getting aws credentials 
  • Since we are using aws SDK we need a way to verify that our application has the right authorization to access the aws resource. Aws provides us aws_access_key_id and aws_secret_access_key for this purpose.
  • It's a best practice to use a separate IAM user with only SNS related permissions rather than using the aws root account credentials for this purpose.
  • To create a IAM user go to IAM page (https://console.aws.amazon.com/iam), then go to add User. Under the Select aws access type, tick the Access type as Programmatic access.
  • Next will be the permissions page, click the Attach existing policies directly and search for SES from filter polices and select AmazonSNSFullAccess
  • Next will be Tags, Review and finally create the user. You will receive Access key ID and a Secret access key. Save them in a safe location and you should not share them with anyone.
3) Using the credentials.
  • Now we have the credentials from the aws. There are multiple ways to store the credentials and use in the application. Check out the below link to learn more about using the aws credentials.
          https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/credentials.html
  • I am going to save these credentials in ~/.aws/credentials file. (If you are using windows use C:\Users\USERNAME\.aws\credentials) the credentials file should look like.
[default]
aws_access_key_id = ACCESS_KEY
aws_secret_access_key = SECRET_KEY
4) Code 
  • Like in my previous blog post, you can have the source code at the end of the blog post. 
  • Let's see how to create the bean to build the AmazonSNSClient and Send Sms function using Service class.
AwsSnsConfiguration class AwsSnsService Class SMSTypes
One important thing here is AWS.SNS.SMS.SMSType, there are two types of messages
  • Promotional (default) – Noncritical messages, such as marketing messages. Amazon SNS optimizes the message delivery to incur the lowest cost.
  • Transactional – Critical messages that support customer transactions, such as one-time passcodes for multi-factor authentication. Amazon SNS optimizes the message delivery to achieve the highest reliability

5) Test the application
  • Run the spring boot application.
  • Send the below post request.

  • You will receive a sms to your mobile device.


That's it for this blog post, source code can be found in below link. I will be sharing in a future blog post about how to know about SMS delivery status. Thank you very much for reading.

https://github.com/Rajithkonara/aws-sns-sdk-spring-boot

References & Useful Readings.
https://www.rajith.me/2020/02/send-emails-using-aws-simple-email.html
https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html#sms_publish_sdk
https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/credentials.html




Comments