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.
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)
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.
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