Code Monkey home page Code Monkey logo

python-lambda-docker's Introduction

python-lambda-docker

Lightweight docker image for running, testing, packaging and deploying python-based AWS lambda functions

Links

Purpose

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.

Usage

An example of a usable project can be found in the example/ directory. The 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 Dockerfile derives from python-lambda as a base image, and loading the example directory contents into the image at the path /lambda, then installs dependencies from the requirements.txt file.

Building a Derived Docker Image

To build a docker image from the provided example/ called example-lambda-image with the ASCII-art lambda function in it, run:

$ cd example/
$ docker build --tag example-lambda-image .

Any time you make changes to the example project, you'll need to re-run the docker build command above, or you can investigate using docker volumes to sync local filesystem changes into the container.

Switching Python Versions

The example Dockerfile uses a :latest docker tag in the FROM line, which is currently the same as :3.6, but if you wish to use a different Python version you can change this. Supported Python versions are 2.7 and 3.6. To use Python version 2.7 change the first line of the example Dockerfile to:

FROM kilna/python-lambda:2.7

You will also need to change the following line in config.yaml

runtime: python2.7

Executing the Lambda Function

If you want to execute the lambda function against the event.json input file:

$ docker run example-lambda-image lambda invoke
 _  _     _ _                        _    _ _
| || |___| | |___    __ __ _____ _ _| |__| | |
| __ / -_) | / _ \_  \ V  V / _ \ '_| / _` |_|
|_||_\___|_|_\___( )  \_/\_/\___/_| |_\__,_(_)
                 |/

Building and Testing the Lambda Function

If you would like to build the lambda function (which will package the function and all its dependencies), run:

$ docker run example-lambda-image lambda build

The result will be a zip file created in the /lambda/dist directory inside the container.

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 does is:

  • Removes any log and dist files from prior runs
  • Executes 'lambda build' and stores the log in /lambda/build.log in the container
  • If present and executable, runs /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

Deploying the Lambda Function

You can deploy your lambda function to Amazon's infrastructure... you'll need to add AWS credentials into the config.yaml file. Alternately, if your local AWS CLI environment is working, you can add a .aws/ directory (you can cp ~/.aws) into the example/ directory), then re-build your image. Once AWS is working within your container, you can then run the following to deploy your function to Amazon:

$ docker run example-lambda-image lambda deploy

python-lambda-docker's People

Contributors

kilna avatar kilna-magento avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

python-lambda-docker's Issues

Integrate docker cloud tests

Currently testing is manual via a script, ideally testing would be performed via docker cloud's docker-compose.test.yml / sut configuration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.