AWS python lambda functionNo module named requests
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In deploying Python Lambda functions on AWS, one might encounter an error that reads: "No module named requests." This error is a telltale sign that the function is attempting to import the `requests` library but it's unavailable in the Lambda execution environment. This article delves into what causes this issue, and how you can resolve it by deploying dependencies correctly with AWS Lambdas. Additionally, we will cover best practices for managing dependencies in AWS Lambda functions.
Understanding AWS Lambda Execution Environment
AWS Lambda allows you to run code without provisioning or managing servers. The underlying infrastructure is fully managed by AWS, which means:
- Lambda functions operate in a restrictive execution environment.
- Not all Python modules or packages are available by default.
- The size of the deployment package is a key consideration, with the unzipped deployment package size limit being 250 MB.
These constraints necessitate packaging any external modules or libraries with your Lambda functions.
The "No Module Named Requests" Error
What It Means
The error occurs because the `requests` library, a popular Python package for making HTTP requests, is not included in the AWS Lambda execution environment by default. To use this library, it must be installed and packaged with your function's deployment package.
Technical Explanation
When you use an `import` statement in your Python script, Python will look for the package in its current execution environment. For AWS Lambda, this means looking in the directories you've included in your deployment package plus any globally available directories.
If the requested module isn't found, Python raises the "No module named `requests`" error.
Solutions
Packaging Dependencies with AWS Lambda
- Use a Requirements File:
- Create a `requirements.txt` file that lists `requests`, along with any other dependencies.
- Use a virtual environment to install packages listed in the `requirements.txt`.
- Navigate to the virtual environment's `site-packages` directory and package your code with its dependencies.
- You can upload this ZIP file when creating or updating your Lambda function in the AWS Management Console or via an AWS CLI command.
- Package your dependencies in a suitable directory structure.
- Use the AWS Management Console or CLI to create a Lambda Layer from this ZIP file.
- Include the Layer in your Lambda function configuration either through the console UI or a command line.
Related reading
- AWS RDS for PostgreSQL cannot be connected after several hours
- AWS RDS instance upgrade down time
- AWS RDS instance upgrade down time
- AWS RDS PostgreSQL error remaining connection slots are reserved for non-replication superuser connections
- AxisError axis 1 is out of bounds for array of dimension 1 when calculating accuracy of classes
- aysncio cannot read stdin on Windows
- AWS RDS Provisioned IOPS really worth it?
- AWS RDS public access

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.