At Vatu Ltd, we have 3 main goals: Create high-quality websites Give great customer service to our clients Create a happy, safe,...
Adding SMS Notifications to Your Bash ScriptsDan Dulaney
Hello! Today, we’re going to add text message notifications to our bash scripts. To do so, we’re going to use the Amazon Web Service’s SNS SMS api, via AWS CLI. For this tutorial, you will need to have an Amazon Web Services account. Signing up for an AWS account is beyond the scope of this tutorial.
Cost Note: The first 100 text messages / month are free. After that, you may start incurring charges.
Step 1: Create an Appropriate AWS User
Start by creating a new User or Group in AWS with the appropriate privileges. You do NOT want to use your root account in production, for security reasons. I would suggest creating a group here, and adding your user to that group, so you can easily expand privileges if you want to do more things via AWS CLI.
- Visit the IAM home, in the AWS console: https://console.aws.amazon.com/iam/
- Click on “Users”, then “Add User”.
- Enter a User name, and then check “Programmatic Access”
- On the “Permissions” Screen, you can either assign the user to a group with these permissions, or assign directly. You need a minimum of AmazonSNSFullAccess permission here.
- Review, then create your user.
- Important : Write down or download the CSV for the access and secret key. You CANNOT get these again, without regenerating and changing them.
Step 2: Install and configure AWS CLI
There are many ways to install the AWS CLI. If your platform supports it, I strongly suggest installing with PIP:
sudo pip install awscli
If you cannot or wish not to use PIP, see the official guide here for installing AWS CLI.
To configure, first enter:
aws configure This will prompt you for the following, which you should have noted from the step above.
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: ENTER
The access key and secret key should have been generated in step 1. The region can be any of the AWS regions, but if you choose one that does not support SMS, you will have to force that region in the command in step 3.
Step 3: Test It Out!
Try the following code in terminal, to test and make sure everything works. Be sure to substitute your phone number with the placeholder.
aws sns publish --phone-number=1-555-555-5555 --message "Your Message Here"
If your default region does not support SMS, you can add this tag to force it to one that does:
--region us-west-2 .
--message can also take a file name as an argument, with
--message file://file.txt, instead of a string, and send the contents of that file.
That’s it! Feel free to add the line above to any of your bash scripts or cron jobs, to alert you when something occurs. For example, I have a weekly backup in addition to my nightly, so I have it text me when that has been completed.
Do you have any novel uses? I’d love to hear about them below!
You must be logged in to post a comment.