How to send SMS using Amazon SNS and Python


AWS Amazon SNS Python


Table of contents:

We live in the World of messengers and this might be not relevant tutorial, however this is not true. Sometimes we may need short text message to notify someone about something (for example, someone with not fancy phone or someone who does not have Internet available on their phone or we just need to send short message about accident on the server or just to send message to our grandparents). With that in mind, let’s take a look at Amazon SNS service with SMS option.

I have been working with Twilio the other day, which appears to be the easiest to use, affordable pricing and very rich API. But in the scope of this tutorial I really want to focus on AWS services.

Prerequisites #

This is what we will need in order to successfully walk through this document:

I am not going to walk through all prerequisites since some of them are very straight forward.

IAM user and policy #

Let’s first log in to our AWS console and create our IAM user in order to make API calls towards our account. Let’s create IAM user and set Access type to “Programmatic access”. Grab access and secret key and now let’s assign new policy with our new user.

 1{
 2  "Version": "2012-10-17",
 3  "Statement": [{
 4      "Effect": "Deny",
 5      "Action": [
 6        "sns:Publish"
 7      ],
 8      "Resource": "arn:aws:sns:*:*:*"
 9    },
10    {
11      "Effect": "Allow",
12      "Action": [
13        "sns:Publish"
14      ],
15      "Resource": "*"
16    }
17  ]
18}

This policy is exactly what we need: it does not allow to publish to topics and applications (push notifications), but it does allow to publish SMS. Great!

If you want to deal with IAM policies like a boss, I encourage you to check CloudBerry Explorer since it has wizard to design your policies.

AWS SNS configuration #

Now, let’s go ahead and configure our SMS preferences in AWS account. At the time of this tutorial AWS offers only few regions with SMS support (three if I am not mistaken). So my region is eu-west-1 (Ireland).

AWS SES SMS configuration

You may want to change “Default message type” if you have critical text message and you want to process them accordingly. Set “Account spend limit” and other options as well if you feel you need it. And this point your configuration is done. Make sure you remember the region, since you need to use this in your SDK later on.

AWS SDK for Python #

I assume that you have Python and package manager on your computer. So we don’t spend time on this. If you have not I suggest to check Python Beginner’s guide, where you’ll learn what you need for the below.

The only thing we need here is boto3 which allows us to work with AWS from our Python code. Let’s quickly install it from package manager:

1pip3 install boto3

I am using Python 3.6.

Sending SMS #

Now we can send our SMS with the following few lines:

 1import boto3
 2
 3# Create an SNS client
 4client = boto3.client(
 5    "sns",
 6    aws_access_key_id=ACCESS_KEY,
 7    aws_secret_access_key=ACCESS_KEY,
 8    region_name="eu-west-1"
 9)
10
11# Send your sms message.
12client.publish(
13    PhoneNumber=YOUR_PHONE_GOES_HERE,
14    Message="This is Amazon SNS service talking!"
15)

Make sure YOUR_PHONE_GOES_HERE is set in E.164 format.

SMS pricing #

I did not make research and comparison with other available services. Before starting mass SMS, check out SMS pricing section of AWS. It supports 200+ countries and you can check it out for your location. For example United States sounds affordable to me:

Amazon SNS (SMS text messages) pricing

That’s it! You should receive SMS from “NOTICE” sender, which is default sender ID. You may want to change it in “Default sender ID” in SMS preferences in your SNS configuration. Hope it helps someone!

comments powered by Disqus