- Newest
- Most votes
- Most comments
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
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:
-
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)
-
Upload the zip file as a new Lambda layer in the AWS console.
-
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
All details here to create a layer https://docs.aws.amazon.com/lambda/latest/dg/python-layers.html
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.
Relevant content
- Accepted Answerasked a year ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 5 months ago
- AWS OFFICIALUpdated 2 years 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