Skip to content

Instantly share code, notes, and snippets.

Avatar
👋

Brian Thomas Smith bsmth

👋
View GitHub Profile
@bsmth
bsmth / IncomingSNSMessage.json
Created Nov 19, 2019
Example incoming SNS message with three custom fields
View IncomingSNSMessage.json
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:eu-west-1:489024636830:email-notifications:91018fd5-5f62-434f-9162-fd4635c5fef4",
"Sns": {
"Type": "Notification",
"MessageId": "8bd15777-93da-5f6e-8577-01335b0028ee",
"TopicArn": "arn:aws:sns:eu-west-1:.....my-email-topic",
@bsmth
bsmth / sendSESMailWithTemplate.py
Last active Nov 15, 2019
Use SES and feed variables into a pre-defined template
View sendSESMailWithTemplate.py
import csv
import json
import urllib.parse
import boto3
from botocore.exceptions import ClientError
SENDER = "EMnify Event Notifications <b.smith@emnify.com>"
RECIPIENT = "endpoint.is.online@gmail.com"
AWS_REGION = "eu-west-1"
@bsmth
bsmth / email_template.json
Created Nov 15, 2019
Basic Email Template used by SES
View email_template.json
{
"Template": {
"TemplateName": "NotificationTemplate",
"SubjectPart": "New {{event_description}} Notification",
"HtmlPart": "<div><style type=\"text/css\"><!--.notification body, .notification .x_background_main, .notification p, .notification table, .notification td, .notification div{font-family:'Lato','Helvetica Neue',Helvetica,Arial,sans-serif}.notification img{border:none; max-width:100%}.notification p{padding-bottom:2px}.notification body{background:#fff; font-size:17px; line-height:24px; margin:0; padding:0}.notification table{border-collapse:collapse; width:100%}.notification td{font-size:17px; line-height:24px; vertical-align:top}.notification .x_email_footer td, .notification .x_email_footer p, .notification .x_email_footer span, .notification .x_email_footer a{font-size:15px; text-align:center; color:#434245}.notification .x_email_footer td{padding-top:20px}.notification h1, .notification h2, .notification h3, .notification h4{color:#434245; font-weight:400; line-height:1.4; margin:
@bsmth
bsmth / template.html
Last active Nov 15, 2019
Example HTML template to use in SNStoSES.py. Uses `{{handlebars}}` variables for flexibility.
View template.html
<!--
Basic Use:
<h1 >{{event_description}}</h1>
<p>New {{event_description}} detected on the EMnify platform:</p>
<p >To view the status of <strong>{{endpoint_name}}</strong>, visit:
<a href="https://cdn.emnify.net/#/endpoints/{{endpoint_id}}">Endpoints / {{endpoint_id}}</a></p>
-->
<div><style type="text/css">
@bsmth
bsmth / SNStoSlack.js
Last active Nov 13, 2019
Node Lambda example to send a Slack notification based on the content of an SNS message
View SNStoSlack.js
var https = require('https');
var util = require('util');
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
console.log('From SNS:', event.Records[0].Sns.Message);
// The webhook defaults can be overridden by these fields:
var postData = {
"channel": "#endpoint-alerts",
@bsmth
bsmth / SNStoSES.py
Last active Nov 13, 2019
Python Lambda which sends an Email based on the content of an SNS message.
View SNStoSES.py
import csv
import json
import urllib.parse
import boto3
from botocore.exceptions import ClientError
SENDER = "EMnify Event Notifications <b.smith@emnify.com>"
RECIPIENT = "brian.smith@emnify.com"
AWS_REGION = "eu-west-1"
@bsmth
bsmth / s3tosns.py
Last active Nov 13, 2019
Python Lambda which reads events from S3 and fans out messages to SNS based on content.
View s3tosns.py
import csv
import json
import urllib.parse
import boto3
s3 = boto3.client('s3')
sns = boto3.client('sns')
def lambda_handler(event, context):
@bsmth
bsmth / json_flatten.py
Last active Nov 15, 2019
Small Python script to flatten JSON which is useful in AWS lambdas occasionally
View json_flatten.py
'''
Originally found in https://towardsdatascience.com/flattening-json-objects-in-python-f5343c794b10
edited to shorten key names as far as possible.
'''
import json
json_example = {
"glossary": {
"title": "example glossary",