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
  • lambda_function.py
  • requirements.txt
  • params.json

Dockerfile

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 deploy_package.zip. I set environment value for AWS region to us-west-2 because my lambda and S3 are located in US WEST region.

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

#set environment values
ENV LANG C.UTF-8 
ENV AWS_DEFAULT_REGION us-west-2 

WORKDIR /var/task
ADD . . 

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

lambda_function.py

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.

lambda_function.py
import json 
import requests

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

requirements.txt

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

requirements.txt
requests

params.json

This file includes JSON input parameter for test execution.

params.json
{ 
		"url":"https://www.yahoo.co.jp"
}

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 deploy_package.zip to AWS lambda function!
Profile
I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
Objective
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.
Link
  • LinkedIn (preparing)

  • Twitter

  • Facebook (preparing)

  • GitHub

  • StackOverFlow (preparing)

Archives