How to send SMS using Amazon SNS and Python

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:

  • AWS account (you may want to check AWS free tier) and sign up super quickly if you have not yet;
    • IAM User with specific policy attached (if you want to make sure your user is able to do only what you need in the scope of this tutorial);
    • SMS properties configured properly (in the region where it is supported);
  • AWS SDK of your choice (here we are going to use Amazon SDK for Python) and computer with operating system of your choice (I am on mac Sierra);
  • Mobile phone to send it out.

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.


{
  "Version": "2012-10-17",
  "Statement": [{
      "Effect": "Deny",
      "Action": [
        "sns:Publish"
      ],
      "Resource": "arn:aws:sns:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "sns:Publish"
      ],
      "Resource": "*"
    }
  ]
}

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:


pip3 install boto3

I am using Python 3.6.

Sending SMS

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


import boto3

# Create an SNS client
client = boto3.client(
    "sns",
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=ACCESS_KEY,
    region_name="eu-west-1"
)

# Send your sms message.
client.publish(
    PhoneNumber=YOUR_PHONE_GOES_HERE,
    Message="This is Amazon SNS service talking!"
)


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!


In short, this is about:
#python
#amazon sns
#amazon

Start discussion:
Related articles:
122 what is possible with aws free tier preview
I made light talk on one of our recent #AWSRus community gathering in Moscow and decided to discover this topic with further series of posts in my blog covering various AWS services. ... more
about 1 month#free tier #amazon
81 mount aws s3 bucket to your el capitan preview
Learn how to mount S3 bucket to your Mac OS ... more
120 ec2 user data script example preview
Launching AWS EC2 can be supplied by something what is known as "user data" and it helps to have things ready before you access it. ... more
111 data backup and disaster recovery with aws preview
CloudAcademy webinar: how to use AWS services for Backup and DR challenges. I am presenter! ... more
79 mount aws s3 bucket to your debian preview
In this very short tutorial we are going to mount S3 bucket from AWS to the local folder of Debian. ... more
88 amazon solutions architect associate exam preview
August, 9th, 2016 - passed my Amazon CSAA. Few tips I'd love to share here. ... more
61 amazon web services cloudfront with subdirectory preview
In this article we are going to set up CloudFront for static site. And there is nice tip how to nested content (i.e. subfolders) available. ... more
101 aws ec2 linux backup preview
Amazon Web Services (AWS) has its own AWS Linux AMI (also free tier). There are several reasons you want to use this AMI for your projects. ... more
110 awsrus russian st petersburg user group for aws preview
AWSRus — Official AWS User Group Russia (St. Petersburg). ... more
116 aws efs windows preview
AWS Elastic File System is scalable file system in the cloud, requires NFSv4.1 client and does not support Windows at the moment. However let's see what we can do.. ... more
119 smtp server for notifications preview
AWS SES supports SMTP settings for sending through it-self. This is really helpful if we want to configure personal outbound server. ... more
117 seo friendly aws static website with ssl preview
It's been written many times, however there is no consistency how to make static web site with SSL and SEO friendly urls (301 redirect). Pure AWS based services static website ahead, check details inside! ... more
121 aws ec2 user data script windows preview
How to pre-deploy App using user data script for Windows OS with powershell powered script in EC2? Example inside. ... more