diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d2b7d7f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM amazonlinux:2017.03 + +RUN yum -y install git \ + python36 \ + python36-pip \ + zip \ + && yum clean all + +RUN python3 -m pip install --upgrade pip \ + # boto3 is available to lambda processes by default, + # but it's not in the amazonlinux image + && python3 -m pip install boto3 + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c43aa46 --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# Script AWS Lambda deployment packages with Docker + +## Why? + +* Logging in to EC2 and +[creating a deployment package by hand](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html) +is clumsy +* Instead, script package creation around the [`amazonlinux` image](https://hub.docker.com/_/amazonlinux/) +(blessed as an _official repository_ and +linked from [this AWS user guide](https://docs.aws.amazon.com/AmazonECR/latest/userguide/amazon_linux_container_image.html)) + +## Example: Python 3.6 deployment package + +```sh +docker pull quiltdata/lambda + +docker run --rm -v $(pwd)/create_table:/io -t \ + -e SETUP_DIR -e GIT_REPO quiltdata/lambda \ + bash /io/package.sh +``` + +* Mount `/io` as a docker volume + * `/io` should contain `package.sh` and your lambda code \ + * `/io` is where the deployment package, lambda.zip, is written \ +* Pass environment variables with `-e` +* `--rm` so that, for example, secure envs aren't written to disk + + +## Customize +Modify `package.sh` to suit your own purposes. + +## Build container + +```sh +docker build -t quiltdata/lambda . +``` + +## Clone private GitHub repo in container +Use a [personal access token](https://github.com/settings/tokens): + +```sh +git clone https://${TOKEN}@github.com/USER/REPO +``` + +## Optimizations to reduce .zip size +Possible but not tried in this repo: +* [Delete *.py](https://github.com/ralienpp/simplipy/blob/master/README.md) +* [Profile code and only retain files that are used](https://medium.com/@mojodna/slimming-down-lambda-deployment-zips-b3f6083a1dff) + diff --git a/package.sh b/package.sh new file mode 100644 index 0000000..df508c8 --- /dev/null +++ b/package.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +mkdir tmp411 +git clone ${GIT_REPO} +python3 -m pip install ${SETUP_DIR} -t tmp411 + +# Untested optimization to reduce deployment size +# See https://github.com/ralienpp/simplipy/blob/master/README.md +# echo DELETING *.py `find tmp411 -name "*.py" -type f` +# find tmp411 -name "*.py" -type f -delete + +# remove any old .zips +rm -f /io/lambda.zip + +# grab existing products +cp -r /io/* tmp411 + +# zip without any containing folder (or it won't work) +cd tmp411 +zip -r /io/lambda.zip * +