Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

aws lambda - Serverless Framework - Python and Requirements.txt

Using the serverless framework v1.0.0, I have a 'requirements.txt' in my service root with the contents being the list of dependant python packages. (e.g. requests).

However my resulting deployed function fails as it seems these dependencies are not installed as part of the packaging

'Unable to import module 'handler': No module named requests'

I assume it is serverless that does the pip install, but my resulting zip file is small and clearly its not doing it, either by design or my fault as I am missing something? Is it because its Lambda that does this? If so what am I missing?)

Is there documentation on what is required to do this and how it works? Is it serverless that pip installs these or on aws lambda side?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to install serverless-python-requirements and docker

$ npm install serverless-python-requirements

Then add the following to your serverless.yml

plugins:
   - serverless-python-requirements

custom:
  pythonRequirements:
     dockerizePip: non-linux

Make sure you have your python virtual environment active in CLI:

$ source venv/bin/activate

Install any dependencies with pip - note that in CLI you can tell if venv is active by the venv to the left of the terminal text

(venv) $ pip install <NAME>
(venv) $ pip freeze > requirements.txt

Make sure you have opened docker then deploy serverless as normal

$ serverless deploy

What will happen is that serverless-python-requirements will build you python packages in docker using a lambda environment, and then zip them up ready to be uploaded with the rest of your code.

Full guide here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...