Skip to content

Instantly share code, notes, and snippets.

@jg75
jg75 / user-data
Created January 24, 2020 05:18
Automatically associate an elastic ip to the running ec2 instance
#! /bin/bash
allocation_id=<ALLOCATION_ID>
metadata=169.254.169.254/latest/meta-data
get-region() {
curl -s http://$metadata/placement/availability-zone | awk '{
print(substr($0, 0, length($0)-1))
}'
}
@jg75
jg75 / get_account.py
Last active December 16, 2019 18:22
Get the AWS account id for the current session user's credentials
import boto3
def get_account_id(session):
"""
Get Account Id.
Get the AWS account id for the current session user's credentials.
"""
client = session.client("sts")
@jg75
jg75 / child.yml
Last active November 14, 2019 21:42
Export value and import value is kind of annoying, because you can't change an export that's already being imported, but you can use a parameter and force an update if it changes.
AWSTemplateFormatVersion: 2010-09-09
Description: The child template needs the Arn for the task role and the bucket name for the task.
Parameters:
InputS3BucketArn:
Description: Parameter containing the Arn of the S3 Bucket for input
Type: AWS::SSM::Parameter::Value<String>
Default: /Yolo/Development/InputS3BucketArn
OutputS3BucketArn:
@jg75
jg75 / lambda_handler.js
Created October 16, 2019 20:13
Serverless VPC lambda skeleton
'use strict';
console.log('Loading function');
exports.handler = async (event, context) => {
console.log('Received event:', JSON.stringify(event, null, 2));
let responseBody = {
message: "This is a message",
input: event
};
@jg75
jg75 / static-website-cdn-template.yml
Created August 19, 2019 19:47
CloudFormation Template: S3 static website, CloudFront CDN, CloudFront origin access identity, optional alternate domain name and ACM certificate
AWSTemplateFormatVersion: 2010-09-09
Description: Cloudformation Template
Parameters:
S3BucketParameter:
Description: S3 Bucket
Type: String
AliasParameter:
Description: CNAME (alternate domain names)
@jg75
jg75 / test_things.py
Last active May 29, 2019 18:52
PyTest ExampleMemoized function embedded in a handler function
from unittest import mock
import thing
import pytest
@pytest.fixture()
def mock_client(monkeypatch, request):
"""Mock the client."""
@jg75
jg75 / rekognition.py
Created May 20, 2019 17:51
Playing with AWS Rekognition
"""Playing with AWS Rekognition."""
import argparse
import hashlib
import json
import logging
import boto3
logging.basicConfig(
"""An example of procedurally generated music using FoxDot."""
from atexit import register
from FoxDot import Clock, pluck, p1
def fibonacci(n):
"""Get a list of numbers in the fibonacci sequence."""
step = 1 if n >= 0 else -1
seq = [0, 1]
@jg75
jg75 / threadpool_example.py
Last active May 16, 2019 15:39
threading and multiprocessing, two great tastes that taste great together
"""Threading vs. Multiprocessing thread pools example."""
import argparse
import logging
import multiprocessing.pool
import random
import threading
import time
logging.basicConfig(
@jg75
jg75 / super_mro.py
Last active May 2, 2019 18:13
super's behavior and method resolution order in multiple inheritance
"""
MRO (Method Resolution Order) with multiple inheritance is resolved via
a linearization algorithm that flattens a tree. In some cases, super()
is not bound to a classes superclass, but instead to a sibling depending
on the object's MRO.
"""
class Base:
def __init__(self, field, **kwargs):