Package Python applications using Google or-tools for AWS Lambda.
Google Optimization Tools (OR-Tools) is a fast and portable software suite for solving combinatorial optimization problems.
Instead of creating and configuring your own EC2 instance to work around the
installations requirements to run the application on Lambda (for example, or-tools
doesn't qualify for inclusion in pip due to some of its dependencies), or-tools-lambda
uses Docker to create a deployable package locally.
This project is based on the discussion in google/or-tools#259.
You'll need to have Docker installed. Clone the repo
to package the handler.py
sample function that includes a basic program using
or-tools
.
To make this work with your existing Python project, simply add Dockerfile
and package.sh
.
The package.sh
creates a dist
folder and dist.zip
containing the folder
contents as well as or-tools
dependency.
First, build the Docker image.
$ docker build -t or-tools-lambda .
Run package.sh
within the Docker container.
$ docker run --rm -v $(pwd):/handler -w /handler -i or-tools-lambda ./package.sh
Note: The
package.sh
script does not check for or installs any additional that you may have added to your Python application. Should you require any, follow the instructions in the AWS Lambda documentation and adjustpackage.sh
accordingly, so all dependencies are included in the resultingdist.zip
.
If you want to test the application on AWS Lambda, upload dist.zip
using the
AWS console or the AWS CLI.
$ aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://dist.zip
After invoking the function you should see something like this.
"{\"x\": 1.0, \"y\": 2.0}"
The approach was originally described in tunamonster/aws_lambda_ortools.