pythonamazon-web-servicestensorflowaws-lambda

Deploying a microservice with Tensorflow at AWS Lambda


I have been stuck here for too long. I am trying to deploy a microservice that uses tensorflow. There is a single file by the name of handler.py which has the simple code below:

import json
import tensorflow as tf
import numpy as np

def main(event, context):
    # a = np.arange(15).reshape(3, 5)

    body = {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "input": event
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    return response

    # Use this code if you don't use the http event with the LAMBDA-PROXY
    # integration
    """
    return {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "event": event
    }
    """

To make my work easier I am using serverless to deploy a microservice but it fails saying, unzipped size is too big. Here is how my directory looks like:

-- handler.py
-- serverless.yml
-- requirements.txt

requirements.txt looks like:

numpy
tensorflow

I also tried to upload without installing the above modules thinking that lambda will itself initialize from requirements.txt but then get an error that Unable to import module 'handler': No module named 'tensorflow'. What should I do? I have spent lots of time in this and still not convinced that AWS Lambda would not allow me to do this.

If you wanted to see serverless.yml, it looks as follows:

service: numpy-new-test

provider:
  name: aws
  runtime: python3.6
  profile: nsp
  role: arn:aws:iam::xxxxxxxxxxx7:role/AdminRole

functions:
  numpy:
    handler: handler.main
    events:
      - http:
          path: test
          method: get 

Solution

  • As you've mentioned from the error you received it looks like your zipped package is too large. You received that other error because you have a module requirement in your script to use tensorflow.

    Keep in mind the AWS Lambda Limits has a 50MB deployment package size limit. The Tensorflow package by itself is close to 50MB, so adding the Numpy package will take it well over the limit.

    Have a look at this blog which does some investigation into the package limit sizing in AWS Lambda

    https://hackernoon.com/exploring-the-aws-lambda-deployment-limits-9a8384b0bec3