I am trying to deploy a python lambda function using serverless. I need the psycopg2 library so I used the serverless-python-requirements plugin and added it to my serverless.yml file.
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
I can deploy the function successfully:
Serverless: Installing requirements from
/Users/Desktop/postgresql/.serverless/requirements/requirements.txt ...
Serverless: Docker Image: lambci/lambda:build-python3.6
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Injecting required Python packages to package...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (43.07 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
But when I check my Cloudwatch logs, it says that there is "No module named 'psycopg2' ".
The structure of my zip file is:
|--node_modules
|--.gitignore
|--handler.py
|--package-lock.json
|--package.json
|--serverless.yml
No .serverless/ dir was created when I ran serverless deploy...
Any help would be greatly appreciated!
The plugin serverless-python-requirements uses cache. Make sure to clean it before package your service. Furthermore, psycopg requires to be compiled with a lambda environment:
# serverless.yaml
custom:
pythonRequirements:
dockerizePip: true
# bash
$ serverless requirements cleanCache
$ serverless deploy --stage <stage>
Did you installed python dependencies correctly?
serverless is trying to install requirements from
Serverless: Installing requirements from
/Users/Desktop/postgresql/.serverless/requirements/requirements.txt
I would suggest following
virtualenv ./envsource env/bin/activatepip install psycopg2pip freeze > requirements.txtAssuming your code is importing your dependency, deploy using serverless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With