Skip to content

Instantly share code, notes, and snippets.

View shentonfreude's full-sized avatar

Chris Shenton shentonfreude

View GitHub Profile
@shentonfreude
shentonfreude / tropo_ec2_vpc_pubip.py
Created December 15, 2015 16:49
EC2 is very different than ASGs, and params in VPC or with NetworkInterfaces are very different than without (inconsistent names of SecurityGroups, SecurityGroupIds, GroupSet, etc)
# EC2 only
# Can't use (EC2) SecurityGroups with Subnet
# Can't use (VPC) SecurityGroupIds with NetworkInterfaces
# Can't use SubnetId with NetworkInterfaces
def add_ec2(self):
name, tags = self._name_tags('ec2')
self.ec2 = self.t.add_resource(
ec2.Instance(
name,
@shentonfreude
shentonfreude / dynamodb_read_backoff.py
Created December 11, 2015 17:00
Wrap boto3 dynamodb in an exponential backoff to avoid ProisionedThroughputExceededException
#!/usr/bin/env python
# An exponential backoff around Boto3 DynamoDB, whose own backoff eventually
# fails on long multipage scans. We'd like to use this as a wrapper somehow,
# see: https://gist.github.com/numberoverzero/cec21b8ca715401c5662
from time import sleep
import boto3
from boto3.dynamodb.conditions import Attr
@shentonfreude
shentonfreude / searchcursor.py
Created December 7, 2015 20:37
AWS CloudSearch with pages using cursor (can only go forward, not back)
import boto3
# Returned 'cursor' can be of seemingly any length, and changes on each return
cs = boto3.client('cloudsearchdomain',
endpoint_url='https://search-thing-xxx.us-east-1.cloudsearch.amazonaws.com',
region_name='us-east-1')
size = 4
cursor = 'initial'
while True:
@shentonfreude
shentonfreude / searchpage.py
Last active December 7, 2015 20:38
AWS CloudSearch with pages: limit of 10,000 results but can go backward and forward
import boto3
# found is always the same
# start is the index into the total results, not a page of size results
cs = boto3.client('cloudsearchdomain',
endpoint_url='https://search-name-xxx.us-east-1.cloudsearch.amazonaws.com',
region_name='us-east-1')
size = 4
@shentonfreude
shentonfreude / cloudsearch_page_size.py
Last active December 7, 2015 20:39
Page through AWS CloudSearch results by pages of a given size with Next and Prev links
#!/usr/bin/env python3
"""
We want to query of a page 'size' of N results and return them, and also a
'next' and 'previous' link. Those CloudSearch 'start' offsets are based on the
page (for our app, starting at 1) and the size. So with size=100: page=1 has
start=0, page=2 has start=100, page=10 has start=100. We don't want to return
'previous' if we're at page=1, and don't want to return 'next' if it's beyond
the maximum results 'found'. So if size=100 and found=543, the last page=5,
there is no page=6. More generally: lastpage = int(found / size).
"""
@shentonfreude
shentonfreude / development.ini
Created November 30, 2015 15:57
Pyramid suppress boto3 log noise via .ini file
# ...
[loggers]
keys = root, rival, sqlalchemy, boto3
# ...
[logger_boto3]
# boto3.resources.action spews binary contents of file put_object at INFO, suppress
level = WARN
handlers =
@shentonfreude
shentonfreude / logtest.py
Created November 24, 2015 17:58
Show our app's logs at INFO but suppress boto's INFO logs (only show 3rd party at WARNING)
#!/usr/bin/env python3
# Setup logs so our app shows INFO but boto shows at WARNING
import logging
import boto3
# Don't show boto INFO; if we don't call this our own INFO logs aren't seen!
logging.basicConfig(level=logging.WARNING)
# Define our logger to show INFO logs
#!/usr/bin/env python3
# Get file path from sys.argv and upload as a presigned URL
import os
import sys
import boto3
import requests
asset_path = sys.argv[1]
_, asset_fname = os.path.split(asset_path)
@shentonfreude
shentonfreude / s3-sns-sqs.json
Last active March 28, 2018 19:35
S3 bucket with Notification to SNS which sends email and injects a message into SQS
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Apply policy to NEW bucket",
"Resources": {
"Queue": {
"Type": "AWS::SQS::Queue",
"Properties": {
"QueueName": "cshenton-tropo-queue"
#!/usr/bin/env python
import sys
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.view import view_defaults