Skip to content

Instantly share code, notes, and snippets.

@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

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)"
@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)
import boto3
import collections
import datetime
ec = boto3.client('ec2')
def lambda_handler(event, context):
reservations = ec.describe_instances().get(
'Reservations', []
)
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']},
@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.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.