[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.

For the console UI operations, you can check out this vide.


References:

https://www.youtube.com/watch?v=3BH79Uciw5w&ab_channel=PyLenin

posted @ 2022-06-06 12:59  Grandyang  阅读(547)  评论(0编辑  收藏  举报
Fork me on GitHub