{"id":7634,"date":"2018-06-24T23:57:48","date_gmt":"2018-06-24T15:57:48","guid":{"rendered":"http:\/\/rmohan.com\/?p=7634"},"modified":"2018-06-25T00:00:15","modified_gmt":"2018-06-24T16:00:15","slug":"adding-sms-notifications-to-your-bash-scripts","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7634","title":{"rendered":"Adding SMS Notifications to Your Bash Scripts"},"content":{"rendered":"<p><strong>Cost Note:\u00a0<\/strong>The first 100 text messages \/ month are free. After that, you may start incurring charges.<\/p>\n<h2>Step 1: Create an Appropriate AWS User<\/h2>\n<h2><\/h2>\n<p>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 \u00a0if you want to do more things via AWS CLI.<\/p>\n<ul>\n<li>Visit the IAM home, in the AWS console:\u00a0<a href=\"https:\/\/console.aws.amazon.com\/iam\/\" target=\"_blank\" rel=\"noopener\">https:\/\/console.aws.amazon.com\/iam\/<\/a><\/li>\n<li>Click on \u201cUsers\u201d, then \u201cAdd User\u201d.<\/li>\n<li>Enter a User name, and then check \u201cProgrammatic Access\u201d<\/li>\n<li>On the \u201cPermissions\u201d Screen, you can either assign the user to a group with these permissions, or assign directly. You need a minimum of\u00a0<em>AmazonSNSFullAccess\u00a0<\/em>permission here.<\/li>\n<li>Review, then create your user.<\/li>\n<li><strong>Important\u00a0<\/strong>: Write down or download the CSV for the access and secret key. You CANNOT get these again, without regenerating and changing them.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Create a new folder in your HOME directory (<code>~\/<\/code> on Linux\/Mac or <code>C:\\Users\\USERNAME\\<\/code> on Windows) called <code>.aws<\/code> and then create a text file in that folder called <code>credentials<\/code> without any extension. Now add the following text to that file:<\/p>\n<pre><code class=\"hljs ini\"><span class=\"hljs-section\">[sns-reminders]<\/span>\r\n<span class=\"hljs-attr\">aws_access_key_id<\/span> = YOUR_AWS_ACCESS_KEY_ID\r\n<span class=\"hljs-attr\">aws_secret_access_key<\/span> = YOUR_AWS_SECRET_ACCESS_KEY\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>For a bit of extra security you can lock down the <code>credentials<\/code> file\u2019s permissions to <code>600<\/code>:<\/p>\n<pre><code class=\"hljs shell\"><span class=\"hljs-meta\">$<\/span><span class=\"bash\"> chmod 600 credentials<\/span>\r\n<\/code><\/pre>\n<p>Now you\u2019ll want to head over to your <a href=\"https:\/\/console.aws.amazon.com\/iam\/home#users\" target=\"_blank\" rel=\"noopener\">IAM console<\/a> and in the users tab, click \u201cCreate New Users\u201d, add a user called \u201csns-reminders\u201d, make sure that \u201cGenerate an access key for each user\u201d is checked and then click \u201cCreate\u201d.<\/p>\n<p>You should then be brought to a screen that looks like this:<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/06\/jjEK1gI.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7639\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/06\/jjEK1gI.png\" alt=\"\" width=\"700\" height=\"122\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/jjEK1gI.png 700w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/jjEK1gI-300x52.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/jjEK1gI-150x26.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/jjEK1gI-400x70.png 400w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/06\/create-subscription.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7637\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/06\/create-subscription.jpg\" alt=\"\" width=\"720\" height=\"332\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-subscription.jpg 720w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-subscription-300x138.jpg 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-subscription-150x69.jpg 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-subscription-400x184.jpg 400w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/a> <a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/06\/create-topic1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7638\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/06\/create-topic1.png\" alt=\"\" width=\"720\" height=\"317\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-topic1.png 720w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-topic1-300x132.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-topic1-150x66.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/06\/create-topic1-400x176.png 400w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<h2>Step 2:\u00a0Install and configure AWS CLI<\/h2>\n<p>There are many ways to install the AWS CLI. If your platform supports it, I strongly suggest installing with PIP: <code>sudo pip install awscli<\/code><\/p>\n<p>If you cannot or wish not to use PIP, see the <a href=\"http:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/installing.html\" target=\"_blank\" rel=\"noopener\">official guide here for installing AWS CLI.<\/a><\/p>\n<p>To configure, first enter: <code>aws configure<\/code> This will prompt you for the following, which you should have noted from the step above.<\/p>\n<p><code> AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE<br \/>\nAWS Secret Access Key [None]: wJalrXUtnFEMI\/K7MDENG\/bPxRfiCYEXAMPLEKEY<br \/>\nDefault region name [None]: us-west-2<br \/>\nDefault output format [None]: ENTER <\/code><\/p>\n<p>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.<\/p>\n<h2>Step 3: Test It Out!<\/h2>\n<h2><\/h2>\n<p>Try the following code in terminal, to test and make sure everything works. Be sure to substitute your phone number with the placeholder.<\/p>\n<pre class=\"language-bash line-numbers copytoclipboard\"><code class=\"language-bash\">aws sns publish --phone-number=1-555-555-5555 --message \"Your Message Here\"\r\n\r\n\r\naws sns publish --topic-arn arn:aws:sns::sms-ack --message \"website is up\"<\/code><\/pre>\n<p>If your default region does not support SMS, you can add this tag to force it to one that does:\u00a0<code>--region us-west-2<\/code> .<\/p>\n<p><code>--message<\/code> can also take a file name as an argument, with <code>--message file:\/\/file.txt<\/code>, instead of a string, and send the contents of that file.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>That\u2019s 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cost Note: The first 100 text messages \/ month are free. After that, you may start incurring charges.<\/p>\n<p> Step 1: Create an Appropriate AWS User <\/p>\n<p>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 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7634"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7634"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7634\/revisions"}],"predecessor-version":[{"id":7640,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7634\/revisions\/7640"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}