cloudmicro / lambda-dynamodb-local

A Container-based local runtime for AWS Lambda (Python) + DynamoDB
MIT License
67 stars 6 forks source link

Cloudmicro for AWS

This is a Docker-driven local run-time for AWS Python Lambda + DynamoDB.

Usage

All Docker and Docker-compose commands are run in the directory containing the docker-compose.yml file

Mac/Linux

  1. docker-compose up -d
  2. docker-compose run --rm -e FUNCTION_NAME={your function name} lambda-python

To re-initialize DyanmoDB Tables

docker-compose run --rm init

Windows

(this requires Docker Toolbox 1.9.1g and must be run with msysgit/Docker Quickstart terminal)

  1. docker-compose -f docker-compose.yml -f docker-compose-win.yml -p myproject up -d
  2. docker run -i --rm -v /$(pwd):/usr/src --add-host=dynamodb:$(docker-machine ip default) -e FUNCTION_NAME={your function name} myproject_lambda-python

To re-initialize DyanmoDB Tables

docker start -i init

For example (Mac/Linux)

Running the following Docker Compose commands will run the hello function contained in this project

  1. docker-compose up -d
  2. docker-compose run --rm -e FUNCTION_NAME=hello lambda-python

Project Structure

Three directories are used to create and test Lambda functions and DynamoDB tables.

db_gen

The db_gen directory contains a node.js application that will create DynamoDB tables and populate them with data.

For example:

lambda_functions

Each Lambda function handler file should be placed in a subdirectory under lambda_functions. The subdirectory name must match the name of the handler file and the name of the handler must follow the format {subdirectory name}_handler.

For example:

Each subdirectory under lambda_functions can also include a requirements.txt file that will include any Python package dependencies required for the Lambda function.

local_events

Each Lambda function must have a corresponding test event. The test event is a json file whose name must match the corresponding subdirectory name in lambda_functions

For example:

Integrating your Lambda function with DynamoDB Local

A @import_config directive needs to be added to any Lambda function that integrates with DynamoDB.

For example, this is how @import_config is used in lambda_functions/demo/demo.py:

@import_config
def demo_handler(event, context, config):
    dynamodb = dynamodb_connect(config)
    words_table = dynamodb.Table(config.Dynamodb.words)
    words = words_table.scan()
    return words["Items"]

Additional configuration items can be injected by editing the config\docker-config.py file.

More info about the @import_config directive (as well as how to handle environment-specific configurations in Python Lambda) can be found on Gist.

TODO

See https://github.com/cloudmicro/lambda-dynamodb-local/issues

Thanks