Instantly share code, notes, and snippets.

View logging.md

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

View env.json
{
"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",
View sample.md

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
View strip_venv.sh
#!/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)"
View check_ssl_expiry.py
import datetime
import logging
import socket
import ssl
YOUR_DOMAIN = 'serverlesscode.com'
WARNING_BUFFER = 14
logger = logging.getLogger()
logger.setLevel(logging.INFO)
View gist:08c264d0d17bf672bb55
import boto3
import collections
import datetime
ec = boto3.client('ec2')
def lambda_handler(event, context):
reservations = ec.describe_instances().get(
'Reservations', []
)
View gist:c5807c6785f326e9510d
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']},
View README.md

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.

View SQLAlchemy and namedtuples.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View sns_to_slack.js
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'};