MXNet package for AWS Lambda

This is a reference application that predicts labels along with their probablities for an image using a pre-trained model with Apache MXNet deployed on AWS Lambda. A Serverless Application Model template (SAM) and instructions are provided to automate the creation of an API endpoint.

You can leverage this package and its precompiled libraries to build your prediction pipeline on AWS Lambda with MXNet.

Additional models can be found in the Model Zoo

This repo shows how you can deploy Apache MXNet with AWS Lambda:


Option 1: Instructions to deploy on AWS Lambda

cd mxnet-lambda/src
zip -9r  * 
aws lambda create-function --function-name mxnet-lambda-v2 --zip-file fileb:// --runtime python2.7 --region us-east-1 --role MY_ROLE_ARN --handler lambda_function.lambda_handler --memory-size 1536 --timeout 60
aws lambda update-function-code --function-name mxnet-lambda-v2 --zip-file fileb://

Option 2: Creating an API endpoint with Serverles Application Model (SAM)

Else create a new bucket using the following AWS CLI command:

aws s3 mb s3://<your-bucket-name>

Before deploying the project to SAM for the first time, you'll need to update some variables in and template.yaml/swagger.yaml (found in sam/ folder).

# swagger.yaml
# <<region>> : AWS region set in Pre-Requisites, referenced twice in swagger.yaml
# <<accountId>> : your global AWS account ID (found in MyAccount)
uri: arn:aws:apigateway:<<region>>:lambda:path/2015-03-31/functions/arn:aws:lambda:<<region>>:<<accountId>>:function:${stageVariables.LambdaFunctionName}/invocations

# template.yaml
CodeUri: s3://<bucket-name>/ # name of S3 bucket created in Pre-Requiisites
DefinitionUri: s3://<bucket>/swagger.yaml # name of S3 bucket created in Pre-Requisites
aws cloudformation package \
--template-file template.yaml \
--output-template-file template-out.yaml \
--s3-bucket <your-s3-bucket-name>

aws cloudformation deploy \
--template-file <path-to-file/template-out.yaml \
--stack-name <STACK_NAME> \
--capabilities CAPABILITY_IAM
$ aws cloudformation describe-stacks --stack-name mxnet-lambda-v2 | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["Stacks"][0]["Outputs"][0]["OutputValue"];'

Alternately you can go to the AWS cloudformation console, click on your stack to see the output values
curl https://MY_URL/predict?url=
curl -H "Content-Type: application/json" -X POST https://MY_URL/predict -d '{"url": ""}'

[Advanced] Build MXNet package from source for AWS Lambda

In case you wondered how the package was created

$ git clone --recursive mxnet
$ cd mxnet 
$ make -j $(nproc) USE_OPENCV=0 USE_CUDNN=0 USE_CUDA=0 USE_BLAS=openblas 
$ cd python
$ python install
$ mkdir mxnet-lambda-pkg
$ cd mxnet-lambda-pkg 
$ cp -r /usr/local/lib/python2.7/site-packages/mxnet-0.10.1-py2.7.egg . 
$ mv mxnet-0.10.1-py2.7.egg/mxnet . 
$ mkdir lib

Copy all the following libraries from /usr/local/lib or /usr/lib/ to lib/ directory  

libatlas.a   libptcblas.a  liblapack.a
libcblas.a       libf77blas.a   libptf77blas.a


All the necessary libraries needed for MXNet have been copied to the src/lib folder. In addition, PIL for Python is also available for your use. OpenCV was available in the previous release, but has been taken out to reduce the size of the code package. Refer to opencv branch for the code