0

I have a "hello-world" python project with serverless framework. I'm trying to deploy a simple lambda function that needs numpy. But currently, when I run sls deploy and try to call the lambda function, I get an internal server error.

Here is the structure of my project:

test_api/
    serverless.yml
    test_api.py
    Pipfile

in test.py I have:

import numpy as np


def handler(event, context):
    return {
        "statusCode": 200,
        "body": f"numpy version = {np.__version__}"
    }

In my serverless.yml:

service: test_api
frameworkVersion: "3"
provider:
  name: aws
  runtime: python3.8
  region: eu-west-3
  stage: dev

functions:
  test:
    handler: tes_api.handler
    events:
      - http:
          path: test_api
          method: get
          cors: true

plugins:
  - serverless-python-requirements

In Pipfile:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
numpy = "*"

[requires]
python_version = "3.8"

When I deploy, I have no error, but when I call the end point, I get: enter image description here

When I test it on AWS: AWS_test_lambda

If I remove numpy from Pipfile and lambda (I just return "hello" in my lambda), everything works fine.

Why do I get this error and how can I fix it ?

djvg
  • 11,722
  • 5
  • 72
  • 103
Vince M
  • 890
  • 1
  • 9
  • 21
  • https://stackoverflow.com/questions/46185297/using-numpy-in-aws-lambda – Mark B Jun 16 '22 at 12:52
  • @MarkB Thank for the answer but i picked numpy as an example. I have the same probleme for psychog2 or any other library. The plugin serverless-python-requirements is supposed to work with Pipfile according to the documentation of serverless framework: https://www.serverless.com/plugins/serverless-python-requirements https://www.serverless.com/blog/serverless-python-packaging/ – Vince M Jun 16 '22 at 13:36

2 Answers2

0

Is it a typo in your serverless.yml file?

you have

handler: tes_api.handler 

I imagine it's meant to be test_api.handler

Mark Sailes
  • 727
  • 4
  • 16
0

Looking at the structure of your project, it seems you missed an important step mentioned in the documentation - no package.json. You need to run the following command in your project directory:

$ sls plugin install -n serverless-python-requirements

This will automatically add the plugin to your project's package.json and the plugins section of its serverless.yml.

If you want to start from the beginning, then run the following commands in your active venv of your project directory:

$ npm init

then

$ npm install --save serverless-python-requirements

I would add the following to your serverless.yml

# serverless.yml

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: non-linux

Then deploy while your venv is active. Please note serverless-python-requirements requires Docker for it to work.

Luv_Python
  • 194
  • 6