Skip to content

Instantly share code, notes, and snippets.

@ryansb
ryansb / README.md
Last active September 24, 2021 17:35
SQLAlchemy/JSON Notebook - requires Python 3, SQLAlchemy, psycopg2, and Jupyter (formerly IPython Notebook)

Before running this notebook, run:

pip3 install jupyter SQLAlchemy psycopg2

This will install the notebook server and database drivers needed to run these examples. For more information on installing Jupyter (formerly IPython notebook) see their install guide.

Once you've installed the dependencies, run jupyter notebook and it will open your web browser to the notebook's main page. Then upload this notebook (SQLTest.ipynb) and run it.

@ryansb
ryansb / pretty_ddb_client.py
Created June 30, 2021 16:10
Boto3 DynamoDB client with default table names and serialization/deserialization
import boto3
from boto3.dynamodb.transform import copy_dynamodb_params, TransformationInjector
from boto3.dynamodb.conditions import Attr
def pretty_ddb(*args, default_table_name=None, **kwargs):
"""Builds a DynamoDB client with automatic serialization/deserialization.
Instead of `.get_item(...) -> {'key': 'S': 'Value', 'count': 'N': '9'}` this
changes the client to automatically serialize and deserialize as
@ryansb
ryansb / sns_to_slack.js
Created August 23, 2015 14:27
Forward SNS messages to Slack Webhook receivers with AWS Lambda
console.log('Loading function');
const https = require('https');
const url = require('url');
// to get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration
const slack_url = 'https://hooks.slack.com/services/<KEY STUFF>';
const slack_req_opts = url.parse(slack_url);
slack_req_opts.method = 'POST';
slack_req_opts.headers = {'Content-Type': 'application/json'};
@ryansb
ryansb / check_ssl_expiry.py
Created January 7, 2016 22:53
Check expiring certificates
import datetime
import logging
import socket
import ssl
YOUR_DOMAIN = 'serverlesscode.com'
WARNING_BUFFER = 14
logger = logging.getLogger()
logger.setLevel(logging.INFO)
@ryansb
ryansb / cjdns
Created April 19, 2013 18:27
A slightly modified init script for CJDNS that puts the config in a sane location.
#!/bin/bash
#
# You may redistribute this program and/or modify it under the terms of
# the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import boto3
import collections
import datetime
ec = boto3.client('ec2')
def lambda_handler(event, context):
reservations = ec.describe_instances(
Filters=[
{'Name': 'tag-key', 'Values': ['backup', 'Backup']},

Is there a better way to handle having an application inside a container that's executed from outside pass logs up?

Ansible-Container has a CLI tool that invokes a conductor container (which contains Ansible, a bunch of deps, etc) and has to get & display logs from the executions inside. To make this work & display logs, we have kind of an odd setup that I'm not sure is right.

In the container

We're logging out a combination of structlog and plain Ansible output to stdout. This means not all the CLI output is parseable as JSON, so we format it inside the container to be shown on the host stdout.

Outside the container

{
"LANG": "en_US.UTF-8",
"AWS_SECRET_ACCESS_KEY": "A+CKZFOmdRVhPYOELkrMGOD/RXViQPjbTAsaF1d4",
"AWS_DEFAULT_REGION": "us-east-1",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024",
"AWS_LAMBDA_FUNCTION_NAME": "testenv-dev-hello",
"TESTSTR": "hello",
"AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
"PYTHONPATH": "/var/runtime",
"AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/testenv-dev-hello",

sample of a playbook that just uses localhost for a bunch of AWS tasks

---
- hosts: localhost
  tasks:
  - name: make a VPC for my app
    register: vpc
    ec2_vpc:
       region: us-west-2
@ryansb
ryansb / strip_venv.sh
Last active February 18, 2016 15:55
Strip down a Python virtualenv by removing symbols from compiled files, thanks to @ogrisel for the tip!
#!/bin/bash
set -e
set -o pipefail
echo "venv original size $(du -sh $VIRTUAL_ENV)"
find $VIRTUAL_ENV/lib/python2.7/site-packages/ -name "*.so" | xargs strip
echo "venv after stripping $(du -sh $VIRTUAL_ENV)"