Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/ mxnet-lambda Public archive

Reference Lambda function that predicts image labels for a image using an MXNet-built deep learning model. The repo also has pre-built MXNet, OpenCV libraries for use with AWS Lambda.


Notifications You must be signed in to change notification settings


Folders and files

Last commit message
Last commit date

Latest commit



26 Commits

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:

  • Using AWS CLI to create a Lambda function
  • Using Serverless Application Model (SAM) to create a serverless application with API Gateway and AWS Lambda


  • MXNet 0.10.1
  • Numpy
  • PIL
  • boto3 is included in the Lambda environment, if you want to try it locally please pip install boto3 in your virtualenv

Option 1: Instructions to deploy on AWS Lambda

  • Create a Lambda function from the CLI by running the following commands:
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
  • Update the Lambda function code
aws lambda update-function-code --function-name mxnet-lambda-v2 --zip-file fileb://
  • Test the Lambda function:
aws lambda invoke --invocation-type RequestResponse --function-name mxnet-lambda-v2 --region us-east-1 --log-type Tail --payload '{"url": ""}' output_file

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

  • In the AWS Region you plan to deploy, make sure you have an existing Amazon S3 bucket in which SAM can create the deployment artifacts.

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
  • Execute the AWS Cloudformation SAM commands to create the Serverless application
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
  • Get the URL of the Serverless application that was created
$ 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
  • Test with GET request
curl https://MY_URL/predict?url=
  • Test with POST request
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

  • compile MXNet on Amazon Linux (AMI name: amzn-ami-hvm-2016.03.3.x86_64-gp2)
$ git clone --recursive mxnet
$ cd mxnet 
$ make -j $(nproc) USE_OPENCV=0 USE_CUDNN=0 USE_CUDA=0 USE_BLAS=openblas 
  • build python bindings
$ cd python
$ python install
  • copy python bindings
$ 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 . 
  • copy all the support libraries to lib folder
$ 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


Reference Lambda function that predicts image labels for a image using an MXNet-built deep learning model. The repo also has pre-built MXNet, OpenCV libraries for use with AWS Lambda.



Code of conduct

Security policy





No releases published


No packages published