Lightweight docker image for running and packaging python-based AWS lambda code
- Docker: python-lambda
- GitHub: python-lambda-docker
- Based on the python-lambda library by nficano
I needed a Docker-based environment in which to host AWS python lambda functions for the purpose of testing and building them... python-lambda works well under virtualenv for development, but build and deployment automation require a clean and reproducible environment to operate in. Our CI system already supported Docker as a containerization system, so it was the obvious choice.
In order to use this, you will have your project derive its own Dockerfile based on a base python-lambda image corresponding to which version of Python you wish to run.
An example of a usable project can be found in the example/ directory. This lambda function in service.py takes a JSON input file like the one provided in event.json and returns an ASCII-art version of the text described in it. The provided Dockerfile derives from this image and loads the current workspace into the image at the path /lambda, then installs dependencies from the requirements.txt file.
To build a docker image called example-lambda-image with the example lambda function in it, run:
$ cd example/
$ docker build --tag example-lambda-image .
If you want to execute the lambda function against the event.json input file:
$ docker run example-lambda-image lambda invoke
_ _ _ _ _ _ _
| || |___| | |___ __ __ _____ _ _| |__| | |
| __ / -_) | / _ \_ \ V V / _ \ '_| / _` |_|
|_||_\___|_|_\___( ) \_/\_/\___/_| |_\__,_(_)
|/
If you would like to see if your lambda function build (which will package the lambda and all dependencies within the container into a zip file), run:
$ docker run example-lambda-image lambda build
If you would like to build and test the lambda function and gather up the results, you can run:
$ docker run example-lambda-image lambda_build_tar | tar -x -v
build.log
test.log
dist/
dist/2017-09-01-003647-example-lambda.zip
Behind the scenes, what this script does is:
- Removes any log and dist files from prior runs
- Runs 'lambda build' and stores the log in /lambda/build.log in the container
- If present and executable, run /lambda/run_tests and stores the log in /lambda/test.log in the container
- Tars the log files, and the contents of the dist directory in /lambda on the container and pipes it to standard output
- Untars the contents bundled up within the container, and extracts them into your current directory
The example Dockerfile uses :latest in the FROM line, which is currently the same as :python-3.6, but if you wish to use different python versions you can change this.
For instance, if you want to use Python version 2.7 change the first line of your Dockerfile from:
FROM kilna/python-lambda:latest
To:
FROM kilna/python-lambda:2.7