[AWS] Unable to import module ‘lambda_function’: No module named ‘requests’

AWS lambda function has some built-in packages, but when you use some other packages (like requests), you might get the following error:

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

At this time, we can use Lambda Layers to solve this. If you use SAM (Serverless Application Model) to deploy your application, you might want to add all your runtime dependency libraries in the requirements.txt file, and add the following line in your Makefile:

pip3 install -r requirements.txt -t "$(ARTIFACTS_DIR)/python"

If you are using the AWS console, you need to manually upload the compressed zip file for your dependency library. I use the requests package for an example, run the following scripts:

mkdir python
cd python
pip3 install requests -t .

And then compress the python folder to a zip file, and then upload it to the lambda layers.

Note: Try to put all your runtime dependency into one layer if possible.

References:

Original: https://www.cnblogs.com/grandyang/p/16347847.html
Author: Grandyang
Title: [AWS] Unable to import module ‘lambda_function’: No module named ‘requests’

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/545361/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球