[Python] Deploying a microservice with Tensorflow at AWS Lambda

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página