Quickly deploying Python code to Lambda

Thursday, December 12, 2019

Lambda is pretty awesome, and we use it for a ton of things at work. I’ve deployed around 20 different scripts at this point, that do everything from monitoring type of tasks to a full Slackbot. Although generally we use Ansible to deploy everything (including our Lambda tasks), there are times when things need to go and they need to go now. For this I wrote a simple script that zips up a package that can be manually deployed to Lambda with all the necessary libraries and things your script needs.

I use pipenv exclusively for my development, so this script assumes you are using the same.

#!/bin/bash

# this is b/c pipenv stores the virtual env in a different
# directory so we need to get the path to it
SITE_PACKAGES=$(pipenv --venv)/lib/python3.7/site-packages
echo "Library Location: $SITE_PACKAGES"
DIR=$(pwd)

# Make sure pipenv is good to go
echo "Do fresh install to make sure everything is there"
pipenv install

cd $SITE_PACKAGES
zip -r9 $DIR/package.zip *

cd $DIR
zip -g package.zip *.py

It’s dead simple once you see it, but was something that took me a few tries and some Googling around to get right, especially with because of pipenv.

devopsawslambdapython

JSON to Data Object in Python

Writing a Custom Model Field for Encryption