AWS Lambda part 2 – packaging and deploying

I received positive feedback on my AWS Lambda presentation in London. This post discusses how to package and deploy your lambda function.

I’m sure there are other ways but I wanted something simple. This allows me to maintain separate enviroments (i.e “dev”, “production”).


I use Make to create a package (i.e “”). The dependencies are installed into a virtualenv which are appended to the package.

Create the function

Deploy the package

PROJECT = sample-python-app
REGION = us-east-1
.phony: clean
rm -f -r $(FUNCTION)*
rm -f -r site-packages
build-dev: clean
aws s3 cp s3://$(FUNCTION)/settings-dev.yml settings.yml
zip -r $(FUNCTION) . -x *.git* tests/*
mkdir -p site-packages
virtualenv $(FUNCTION)-dev
. $(FUNCTION)-dev/bin/activate
pip install -r requirements.txt
cd site-packages; cp -r $$VIRTUAL_ENV/lib/python2.7/site-packages/ ./
cd site-packages; zip -g -r ../$(FUNCTION) .
aws lambda create-function \
–handler main.lambda_handler \
–function-name $(FUNCTION)-dev \
–region $(REGION) \
–zip-file fileb://$(FUNCTION) \
–role arn:aws:iam::XXXX:role/$(FUNCTION)-dev \
–runtime python2.7 \
–timeout 120 \
–memory-size 512 \
aws lambda update-function-code \
–function-name $(FUNCTION)-dev \
–zip-file fileb://$(FUNCTION) \
–publish \
aws lambda delete-function –function-name $(FUNCTION)-dev

Lambda deployment package

Make sure you include all the dependencies in the package. You need to copy the contents of site-packages, NOT the directory itself. If you copy site-packages itself, lambda will not be able to find the modules.

The contents will look something similar to:

Testing locally

Instead of constantly deploying the function, I wanted to test my changes locally.

When creating an AWS lambda function, you need to specify the handler gets executes. In the gist above, I specify it with –handler main.lambda_handler. So testing locally is simple as:

Leave a Comment

Your email address will not be published. Required fields are marked *