Skip to content

Instantly share code, notes, and snippets.

View bsmth's full-sized avatar

Brian Thomas Smith bsmth

View GitHub Profile
@bsmth
bsmth / json_flatten.py
Last active November 15, 2019 09:48
Small Python script to flatten JSON which is useful in AWS lambdas occasionally
'''
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",
@bsmth
bsmth / s3tosns.py
Last active November 13, 2019 12:43
Python Lambda which reads events from S3 and fans out messages to SNS based on content.
import csv
import json
import urllib.parse
import boto3
s3 = boto3.client('s3')
sns = boto3.client('sns')
def lambda_handler(event, context):
@bsmth
bsmth / SNStoSES.py
Last active November 13, 2019 12:42
Python Lambda which sends an Email based on the content of an SNS message.
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 / SNStoSlack.js
Last active November 13, 2019 12:52
Node Lambda example to send a Slack notification based on the content of an SNS message
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 / template.html
Last active November 15, 2019 15:34
Example HTML template to use in SNStoSES.py. Uses `{{handlebars}}` variables for flexibility.
<!--
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 / email_template.json
Created November 15, 2019 15:33
Basic Email Template used by SES
{
"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 / sendSESMailWithTemplate.py
Last active November 15, 2019 15:39
Use SES and feed variables into a pre-defined template
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 / IncomingSNSMessage.json
Created November 19, 2019 13:30
Example incoming SNS message with three custom fields
{
"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 / homepage_spec.rb
Created November 28, 2019 11:57 — forked from gjtorikian/homepage_spec.rb
Basic Capybara testing with Jekyll + Rack
require 'spec_helper'
describe "Homepage" do
before :each do
visit "/index.html"
end
it "has a title" do
expect(page).to have_css("h1", text: "GitHub Help")
# Descriptioon of the sequence below:
#
# AT+... <- AT Command sent to modem
# OK <- Modem Response
//Switch the modem to minimum functionality
AT+CFUN=0,0
OK
// Verbose Error Reporting to get understandable error reporting (optional)