Skip to main content

Send SMS alerts from the Linux command line

Maybe you're writing a bash script and you want immediate feedback to reach you. Maybe you have a server health check and you need to have SMS alerts go out. Or maybe you just want to be able to send SMS to yourself or your opt-in friends from the Linux command line like a pro. Either way, this little tutorial will show you how you can use Amazon SNS and the AWS command line client to send SMS messages using a single command.

AWS configuration

Simple Notification Service (SNS) is an AWS service that can be used to send emails or SMS messages to a distribution list. In order to do this, you have a number of topics to which people can subscribe. Then, anything published to these topics will be received by everyone who subscribed to that topic.

The first step is to create an SNS topic. You'll need an AWS account, then log into the web console and go to the SNS page. There, you can click on the Topics section and create a new topic:

Once done, you will be brought to the details page about that new topic. Write down the ARN you see on that page, since we'll need it later.

Next, you should be able to create subscriptions. You can either create a way for people to subscribe themselves to your topic, like if you're building a mailing list, or you can just register yourself or others. In either case, anyone subscribed to your topic will need to confirm their subscription before they can receive messages.

Click on the new subscription button and select SMS. You could do email or even API requests as well. Then, you can enter your phone number:

Once subscribed, the status will show as pending and you will receive a confirmation message from Amazon. Once confirmed, the status will change accordingly.

Finally, you will need to create an IAM user that has API access to publish to SNS topics. To do that, search for IAM in the AWS console, then create a new user. Select Programmatic access, and give it a name:

On the permissions page, you can create a policy that only allows the user to publish on the topic you created, or you can just be lazy and select the policy Amazon created for us:

You can leave the other options at default. Once created, you will be able to get the access key and secret for that user. Be sure to write it down.

Linux configuration

Now that you have your SNS topic with one or more subscribers, let's install the AWS command line client. Assuming you have Python 3.x installed, you can install AWS like this:

    pip3 install awscli

Once installed, you will need to configure your access key and secret from the previous step:

    aws configure

Finally, you can create a simply bash script which will use the AWS command to send a message:

    aws sns publish --region "us-east-1" --topic-arn "arn:aws:sns:us-west-2:1234567890:MyAlerts" --message "$1"

Here, be sure you use the topic ARN you wrote down earlier, along with the region you used in the AWS console. Save the file, make it executable, then you can use that command anytime you want to send a SMS message!