Set up AWS Lambda Python environment on local machine

I learned how to construct AWS Lambda environment (Python) in local linux. This method uses lambci, docker images that mimic the live AWS Lambda. The enviroment configurations are constructed by following files.
  • Dockerfile
  • requirements.txt
  • params.json


This Dockerfile generates a docker image that collects Python libraries using pip and creates archived libraries to upload to AWS during docker run action. Python libraries are defined in requirements.txt and a zipped archive is outputed as I set environment value for AWS region to us-west-2 because my lambda and S3 are located in US WEST region.

#docker image
FROM lambci/lambda:build-python3.6 

#set environment values

WORKDIR /var/task
ADD . . 

#install python libraries and create an archive to upload
CMD pip3 install -r requirements.txt -t /var/task && \
zip -9 && \
zip -r9 *

This is Lambda Ptyhon source code. This file must include python entry point lambda_handler. In this example code, it calls a HTTP(S) GET request.
import json 
import requests

def lambda_handler(event, context):
	url = event["url"]
	response = requests.get(url)
	return response.text


This file defines mandatory Python library for Lambda Python code. In the example, only requests library that is used in is defined.



This file includes JSON input parameter for test execution.


Let's try it when the preparation is complete.

Step0 Write a Lambda Python code

Write your lambda function code!

Step1 Define python libraries

Define python libraries that used in Lambda Python source code on requirements.txt.

Step2 Install Python libraries

Build a docker image that will gather specified Python libraries. And install Python libs by starting docker container. The Python libs are located on current directory of docker host.
docker build -t mylambda .
docker run -v "$PWD":/var/task --name mylambda mylambda:latest

Step3 Test python code

Note that docker image that executes test code is lambci/lambda:python3.6 not lambci/lambda:build-python3.6. I can specify JSON input parameters with params.json.
docker run -v "$PWD":/var/task lambci/lambda:python3.6 lambda_function.lambda_handler $(printf '%s' $(cat params.json))

Step4 Upload code and libraries to AWS

Upload to AWS lambda function!
I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
To write down my technical knowledge in the place where I can access from anywhere. To share my program source code. To train my writing skill.
  • LinkedIn (preparing)

  • Twitter

  • Facebook (preparing)

  • GitHub

  • StackOverFlow (preparing)