Use Amazon Simple Notification Service with Python

Synopsis

It's common to want to constantly check something and send an email when certain conditions occur. However, the method of sending an email directly from an EC2 instance on AWS is restricted by default, and it may require a procedure to cancel.

If you want to cancel, click here EC2 Email Send Restriction Release Form

Amazon Simple Notification Service is overwhelmingly easy if you just send an email to a pre-specified notification destination.

Issuing ARN from the screen of Amazon SNS

  1. Move to Amazon SNS from the service list of AWS Management Console
  2. Find and click the Create and Add pull-down menu from the left navigation bar
  3. Click Create new topic
  4. Enter the Topic Name on the screen that pops up. The Topic Name is actually part of the ARN, so enter it within 256 characters (symbols can only be hyphens and underscores). Display Name is for SMS, so you can proceed without entering it.
  5. Click Crate Topic to create
  6. After completion, the screen will display Topic ARN, so check the value like "arn: aws: sns: ap-northeast-1: 111111111111: testtopicname" on the right side. ap-northeast-1 should contain your AWS account ID as the 12-digit number that the region you created is 111111111111. This is important so make a note somewhere
  7. Click the Create Subscription button at the bottom of the same screen
  8. For Protocol, select Email from the pull-down menu, for Endpoint, enter the email address you want to receive, and click the Subscribe button at the bottom right.
  9. Click the close button to return to the original information. The item called Subscription ID should be Pending Confirmation on the screen
  10. The first email you will receive confirmation of receipt will be sent to the registered email address. Be sure to click the Confirmation link in the email to indicate your intention to receive it. Please note that if you do not do this, you will not receive subsequent notification emails.
  11. If you follow the above steps properly, the Subscription ID "arn: aws: sns: ap-northeast-1: 111111111111: testtopicname: 8851ea7b-0610-4def-9b38-exxxxxxxxxxxxx" will be entered in the place where Pending Confirmation was done. There should be. But I don't think there was any particular scene where I used this. Only use the Topic ARN in step 6

Write code

I don't do much as much as writing. I just copied the sample code, rewrote the content, and added it after the original code.

snstest.py


import boto.sns

region = 'ap-northeast-1'
#Since this region specifies which region to use, enter the region that was actually acquired by ARN.

if conditionmatched:
#If a certain condition is met

        topic = 'arn:aws:sns:ap-northeast-1:111111111111:testtopicname'
#Using the Topic ARN from earlier

        subject = 'Your Email Subject!!'
#With the title of this email

        body = 'You got %s !' % (messagebody)  
#Send the contents of the body of such an email. English is because the character code was troublesome, but maybe it's not so troublesome...

        conn = boto.sns.connect_to_region(region)
        conn.publish(topic, body, subject)

At the end

Amazon Simple Notification Service now feels like Mobile Push is in the limelight, but it's an unsung hero that hasn't come to the forefront. I'm particularly impressed by how quietly it works behind the CloudWatch alarm settings ... but it's cute enough to just stick a few lines behind the original code and it works. It's a guy, so there's no reason not to use it.

Actually, if the API call fails, you may have to think about retrying with Exponential back off, but this time it's not so critical, so this is fine.

References

See here for common API call errors Amazon Simple Notification Service API Guide --Common Errors (English only)

See here for how to retry when the API call fails Amazon Web Service General Reference (Japanese translation)

Recommended Posts

Use Amazon Simple Notification Service with Python
Try Amazon Simple Workflow Service (SWF) with Python and boto3
[Python] Use JSON with Python
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Use Python / Django with Windows Azure Cloud Service!
Use Trello API with python
Use Twitter API with Python
Use TUN / TAP with Python
Use subsonic API with python3
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Automate simple tasks with Python Part0
Use OpenCV with Python 3 in Window
Use PostgreSQL with Lambda (Python + psycopg2)
Python service
Creating a simple PowerPoint file with Python
Building a Python3 environment with Amazon Linux2
Use smbus with python3 under pyenv environment
Use DeepL with python (for dissertation translation)
Simple Slack API client made with Python
Automate simple tasks with Python Part1 Scraping
Machine learning with python (2) Simple regression analysis
[Python] Use string data with scikit-learn SVM
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
Use cryptography library cryptography with Docker Python image
Use Application Insights with Python 3 (including bottles)
[Azure] Hit Custom Vision Service with Python
Use C ++ functions from python with pybind11
Send an email with Amazon SES + Python
Use selenium phantomjs webdriver with python unittest
Until you can use opencv with python
Use Python and MeCab with Azure Functions
I made a simple blackjack with Python
FizzBuzz with Python3
Scraping with Python
I want to use MATLAB feval with python
Statistics with python
Use additional Python packages with Serverless Framework (v1.x)
Scraping with Python
Python with Go
Specify the Python executable to use with virtualenv
Use logger with Python for the time being
Twilio with Python
Integrate with Python
Start a simple Python web server with Docker
Python> Run with run-time arguments> Use import argparse
Use multiple versions of python environment with pyenv
Wrap C with Cython for use from Python
Play with 2016-Python
AES256 with python
Tested with Python