Serverless framework with serverless-python-requirements does not work

151 Views Asked by At

I opened this thread on github

I'm trying to use libraries in my python backend. I have it deployed with serverless framework. This is my configuration:

serverless.yml:

    plugins:
       - serverless-offline
       - serverless-python-requirements
    
    custom:
       pythonRequirements:
         dockerizePip: non-linux
         zip: true
    
    functions:
       function1:
         handler: handlerPV.function1
         events:
          - httpApi:
              path: /function1
              method: post
       function2:
        handler: handlerPV.function2
        events:
         - httpApi:
             path: /function2
             method: post
       function3:
        handler: handlerPV.function3
        events:
          - httpApi:
              path: /function3
              method: post

(I have changed the names of the functions to show it here)

My .requirements.txt:

cachetools==5.3.2
certificate==2023.11.17
charset-normalizer==3.3.2
dnspython==2.3.0
idna==3.6
numpy==1.21.6
numpy-financial==1.0.0
pymongo==4.6.1
requests==2.31.0
urllib3>=1.26.0,<2.0.0
boto3==1.33.13

The problem I'm having is that when I upload it to AWS with the serverless deploy command I get this error in only some lambda functions. It's strange, because some functions use this library and it works, but in others, it doesn't work..

This is the error un cloudWatch:

[ERROR] Runtime.ImportModuleError: Unable to import module 'handlerSimulationDatabase': No module named 'cachetools' Traceback (most recent call last):

EDIT

I'll tell you about the changes I've made that haven't been successful: I've tried to upload the requirements to a lambda layer, but I haven't had any success with my problem either. I've switched to python 3.8, and have been using pipenv instead of python's native venv. This is the new serverless.yml. I have also tried deleting the lambda functions and creating them again. I am still surprised, since in some functions the cachetools library is used and there is no error, and yet in others, the error appears using both the same function that uses cachetools

service: service-name
frameworkVersion: "3"

provider:
  name: aws
  runtime: python3.8
  stage: staging
  region: us-east-1
  timeout: 29
  memorySize: 1024
  httpApi:
    cors: true

package:
  patterns:
    # Folder
    - "!node_modules/**"
    - "!Lib/**"
    - "!build/**"
    - "!__pycache__/**"
    - "!Include/**"
    - "!venv/**"
    - "!.serverless/**"
    - "!.vscode/**"
    - "!preproduction/**"
    - "!production/**"
    # Files
    - "!README.md"
    - "!cspell.json"
    - "!package-lock.json"
    - "!package.json"

plugins:
  - serverless-offline
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: non-linux
    usePipenv: true
    zip: true
1

There are 1 best solutions below

0
Ismael Treviño On BEST ANSWER

I have already solved. I have simply commented on the custom configuration part of the python requirements plugin. I don't understand what the error is, since it only appeared in a library in some functions.

plugins:
  - serverless-offline
  - serverless-python-requirements

# custom:
#   pythonRequirements:
#     dockerizePip: non-linux
#     zip: true