简体   繁体   中英

Why does AWS Lambda time out when passing a function to a decorator?

I'm working on an python AWS Lambda function that utilises pydantic to perform input validation. I have recently found that Lambda times out (with a timeout of 15 seconds) when executing the following code:

def _stringify(v):
    return str(v)

class SomeModel(BaseModel):
    a: int
    _stringify = validator("a", allow_reuse=True)(_stringify)

SomeModel(a=12)

I have identified that the issue occurs when calling _stringify = validator("a", allow_reuse=True)(_stringify) which passes the _stringify function to the validator decorator. This is fully functional in a local environment but not within AWS Lambda. The following alternative definition of 'SomeModel' also works within the AWS Lambda environment.

class SomeModel(BaseModel):
    a: int

    @validator("a")
    def stringify(cls, v):
        return str(v)

Does anyone with a better understanding of AWS Lambda have any ideas as to why _stringify = validator("a", allow_reuse=True)(_stringify) results in a timeout and can you suggest any possible fixes?

(Note: The alternative definition of SomeModel is undesirable as it violates DRY principles as we want to use _stringify in multiple models.)

The code raises no exceptions when running both locally and in the Lambda environment.

Environment:
AWS Lambda
python 3.8 - Custom runtime build through docker. Lambda layers supporting:

  • pydantic - v1.6.1 (python 3.8)

Environment: python 3.8

AWS Lambda does not support Python 3.8 unless you are using your own Custom Runtime ( https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html ).

You need to make next steps:

  1. Check is your code works without an issues under the Python 3.7
  2. Check your Lambda's settings and ensure that it is setup to use Python 3.7
  3. Prepare a zip file with all your third-party depencies (pydantic, etc.) and upload it to your AWS Lambda's config web page or use it in your scripts during your Lambda's deployment process: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html . Do not forget to use Python3.7 and an appropriate Linux distro for this step. It is better to use CentOS (or an appropriate Windows Subsystem for Linux (WSL) package). However Ubuntu 18.04 (and an appropriate WSL package) is working properly most of the time. Download an appropriate general linux compatible.whl files from the pypi.org if you encountered an issue with some third-party python package and add this.whl file to generated.zip file with your third-party dependencies.

Withour step #3 you will not be able to use third-party libraries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM