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

0

Hi, I used AWS free and used lambda. I have created function af tried to make layer to import requests module but after sevel hours stops and try to get help from you. I am not a developer but have expirens with on prem. OS, DB, WEB, etc. but need help.

I have tried to make pip install requests make a zip fil and downloaded into function og created a new layer at add into function but all do not work.

Please help - I am new at AWS, Regards Erwin

asked 5 days ago47 views
3 Answers
1
Accepted Answer

Hello.
There is a Lambda layer created by volunteers.
Using this one you may not have to create layers.
https://github.com/keithrozario/Klayers

Or you can create it yourself by running the command as described in the documentation shared by @Gary Mclean.
If you only want to use "requests", you can create it by running the following command on Linux OS.

mkdir python
pip install -t python requests
zip -r9 requests.zip python
aws lambda publish-layer-version --layer-name test_layer --compatible-runtime python3.12 --compatible-architectures x86_64 --zip-file fileb://requests.zip --no-cli-pager

I often use CloudShell to run commands.
https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html

After creating a layer with the above command, add the layer to the target Lambda function using the steps described in the document below.
https://docs.aws.amazon.com/lambda/latest/dg/adding-layers.html

profile picture
EXPERT
answered 4 days ago
profile picture
EXPERT
reviewed 4 days ago
0

I understand you're facing challenges with importing the 'requests' module in your AWS Lambda function. This is a common issue that many developers encounter when working with Lambda, especially those new to AWS. Let me provide some guidance to help you resolve this problem.

The error "Unable to import module 'lambda_function': No module named 'requests'" occurs because the 'requests' library is not included in the default Lambda execution environment. To use external libraries like 'requests' in Lambda, you need to include them in your deployment package or use Lambda layers correctly.

Here are some steps to resolve this issue:

  1. Create a Lambda layer with the 'requests' library:

    • On a Linux machine or EC2 instance (Lambda uses Linux-compatible packages):
    • Create a directory named 'python'
    • Inside the 'python' directory, run: pip install requests -t .
    • Zip the contents of the 'python' directory (not the directory itself)
  2. Upload the zip file as a new Lambda layer in the AWS console.

  3. Add this layer to your Lambda function.

Alternatively, you can include the 'requests' library directly in your function deployment package:

  • In your local project directory, run: pip install requests -t .
  • Zip your lambda_function.py file along with the installed 'requests' library
  • Upload this zip file to your Lambda function

When creating the zip file, make sure you're zipping the contents of the directory, not the directory itself. The lambda_function.py should be at the root of the zip file.

If you're still encountering issues, double-check that:

  • The Lambda function handler is correctly set (e.g., lambda_function.lambda_handler)
  • The Python runtime version in Lambda matches the version you used to install 'requests'
  • You've given Lambda enough time to process the layer or new deployment package

Remember, Lambda has size limits for deployment packages and execution timeouts, so be mindful of these constraints.

If you continue to face difficulties, you might want to consider using AWS CloudFormation or the Serverless Application Model (SAM) for more streamlined deployments, as they can help manage dependencies more easily.

Don't get discouraged – working with cloud services often involves a learning curve. With persistence, you'll get more comfortable with AWS Lambda and its intricacies.

Sources
error, "Unable to import module 'lambda_function': No module named 'requests'" | AWS re:Post
Error - AWS Lamda | AWS re:Post
Unable to Import Lambda Function Module: No module named 'lambda_function' | AWS re:Post
Lambda function python package import problem | AWS re:Post

profile picture
answered 5 days ago
profile picture
EXPERT
reviewed 5 days ago
  • Hi, thanks for you anwser, but I need help to AWS free on cloud, I do not have Linux OS or EC2, only cloud AWS free. Can you try again to explain how to on AWS Free server less. I think the problem is how to pip install requiest to zip file. I download the Zip til to my local windows notebook. Next I can make a layer or download into core - but how to activated or ..... from there. Thanks

0
profile picture
EXPERT
answered 5 days ago
profile picture
EXPERT
reviewed 4 days ago
  • I think to have missed to create the file lambda_function.py in the folder where files are - and will be in ZIP file with all modules - Tanks for all useres that have anwser my request.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions