
Stop Using requests in Your Lambdas: Your CTO Will Love It
Discover why switching to AWS Lambda's pre-installed urllib3 eliminates dependencies, reduces deployment package sizes, and offloads security patching to AWS.
Introduction: Two Libraries, Very Different Footprints in AWS Lambda
In the Python ecosystem,
requests has long been the gold standard for HTTP interactions. Coining the slogan "HTTP for Humans," requests provides an intuitive, high-level API that abstracts away the mechanics of headers, query parameters, cookies, and JSON serialization.Every Python developer loves
requests . It is elegant, intuitive, and practically synonymous with HTTP in Python. Naturally, when building an python function that calls an external webhook or internal microservice, the muscle-memory reaction is:
pip install requests
However, beneath its developer-friendly surface,
requests does not actually handle low-level networking itself. Instead, it relies on urllib3 as its underlying transport engine.What Both Libraries Actually Do
To understand why you should drop
requests in Lambda, you need to understand the relationship between both packages:
-
urllib3is a low-level HTTP transport client. It directly manages socket connections, connection pooling, DNS resolution, and SSL/TLS handshakes.
-
requestsis a high-level developer-experience wrapper built on top ofurllib3.
requests does not reinvent HTTP transport; it delegates the heavy lifting to urllib3 . What requests adds is ergonomic syntactic sugar: automatic dictionary encoding, cookie management, integrated JSON serialization, and high-level response wrappers.
Both libraries do similar work but, AWS Lambda runtimes pre-install boto3 and botocore, which directly depend on urllib3. As a result, urllib3 is already available natively in every Python AWS Lambda environment without adding a single kilobyte to your package size.
Runtime Updates, can’t someone else do it?.
One of the greatest operational perks of AWS Lambda is its Managed Runtime Lifecycle, Trash of the Titans teach us that is better when someone else do it for you.
How AWS Manages urllib3
AWS Lambda python runtimes ship with the AWS SDK (
boto3 / botocore ) pre-installed. Because botocore relies on urllib3 , urllib3 is already present inside your Lambda environment without needing to bundle it.
Furthermore, AWS automatically rolls out security patches, minor bug fixes, and maintenance updates to managed runtimes via two-phase runtime rollouts . When a vulnerability in
urllib3 is patched, AWS updates the underlying runtime environment for you across your fleet—without requiring a redeployment or pipeline trigger.What Happens When You Package requests
requests is not part of the native Lambda runtime. When you package it into your zip file or Lambda Layer all the dependencies are installed.
requests==2.34.2┣━━ ✓ certifi required: >=2023.5.7 installed: 2026.6.17┣━━ ✓ charset-normalizer required: >=2,<4 installed: 3.4.7┣━━ ✓ idna required: >=2.5,<4 installed: 3.18┗━━ ✓ urllib3 required: >=1.26,<3 installed: 2.6.3
This subtree of dependencies is not managed by AWS, and you are responsible for keeping it up to date with the following consequences:
- You own the maintenance lifecycle: AWS will never patch
requestsfor you. If a vulnerability is disclosed, every single Lambda function bundlingrequestsremains exposed until someone updatesrequirements.txt, rebuilds the artifact, and redeploys.
- Security Vulnerability Exposure: Security scanners flag unpatched dependencies in CI/CD. For instance, recent vulnerabilities like CVE-2026-25645 (predictable temp path extraction). I know that you know that maybe not affect an isolated environment like lambda but static security tools (like Amazon Inspector) will raise an issue automatically. Good luck explaining to your the security team that a specific CVE is a “false positive”.
- Artifact Bloat: Bundling
requests,certifi,idna, andcharset-normalizerinflates deployment packages, increasing upload times and slightly lengthening cold-start code unpacking. Also these additional dependencies can cause additional security problems like recent problems with certifi bundles and eMudhra certificates .
By switching to the runtime's native
urllib3 , you eliminate an entire subtree of external dependencies and offload security patching to AWS.
Mastering the Lambda INIT Phase
In AWS Lambda, execution is split into two distinct life-cycle phases:
- The Initialization (INIT) Phase: Runs all module-level code outside of
lambda_handler. This happens once when an execution environment is spun up.
- The Invocation Phase: Executes
lambda_handler(event, context)on every request.
A classic anti-pattern with
requests is calling requests.get() inside the handler:
def lambda_handler(event, context): # Creates a brand-new connection every single invocation! response = requests.get("https://api.example.com/data") return response.json()
Because
requests.get() is stateless, each invocation performs a brand-new DNS lookup, TCP handshake, and TLS negotiation. Under high throughput, this exhausts socket descriptors and adds tens of milliseconds of latency to every invoke.The Fix: Global urllib3.PoolManager
urllib3 centers around PoolManager , which is thread-safe and retains open sockets across invocations when declared in the static INIT scope:
import urllib3
# Initialized ONCE during Lambda INIT phasehttp = urllib3.PoolManager(maxsize=10)
def lambda_handler(event, context): # Reuses existing TCP/TLS connection across warm invocations response = http.request("GET", "https://api.example.com/data") ...
Because the execution environment remains warm between invocations, the TLS session stays established, reducing network overhead to near zero on warm calls.
A similar result can be obtained creating a session object with the requests library:
import requests
# Initialized ONCE during Lambda INIT phasesession = requests.Session()
def lambda_handler(event, context): # Reuses existing TCP/TLS connection across warm invocations response = session.get("https://api.example.com/data") ...
Performance & Cold-Start Benchmarks
To test the performance impact 2 different lambdas been tested with aws lambda power tuning with the same parameters
{ "lambdaARN": "lambda_arn", "powerValues": [128], "num": 100, "payload": {}}
Metric | requests (Handler) | urllib3.PoolManager (INIT) |
|---|---|---|
Package Size | 653 KB | 582 bytes |
Cold Start | 840 ms | 915 ms |
Average Invocation | 340 ms | 72 ms |
Memory Consumption | 63 MB | 56 MB |
I was a bit surprised by the cold start latency with urllib3, but the improved average execution time makes it well worth the trade-off.
Migration Guide: From requests to urllib3
Migrating is straightforward; any modern LLM can do it for you, just instruct the LLM to use the PoolManager.
Here are direct replacements for the most common patterns:
Pattern 1: Simple GET Request with Query Parameters
- Before (
requests):
import requests
params = {"status": "active", "limit": 20}resp = requests.get("https://api.example.com/items", params=params, timeout=5)data = resp.json()
- After (
urllib3):
import json import urllib3
http = urllib3.PoolManager()
params = {"status": "active", "limit": 20} resp = http.request( "GET", "https://api.example.com/items", fields=params, timeout=5.0 ) data = json.loads(resp.data.decode("utf-8"))
Pattern 2: POST with JSON Payload and Custom Headers
- Before (
requests):
import requests
headers = {"Authorization": "Bearer secret-token"}payload = {"name": "Widget", "price": 49.99}
resp = requests.post( "https://api.example.com/products", json=payload, headers=headers)
- After (
urllib3):
import json import urllib3
http = urllib3.PoolManager()
headers = { "Authorization": "Bearer secret-token", "Content-Type": "application/json", } payload = {"name": "Widget", "price": 49.99}
resp = http.request( "POST", "https://api.example.com/products", body=json.dumps(payload).encode("utf-8"), headers=headers, )
Pattern 3: Configuring Automatic Retries with Exponential Backoff
In
requests , configuring retries requires importing HTTPAdapter and Retry and mounting them onto a custom session. With urllib3 , retries are a first-class feature:
import urllib3from urllib3.util import Retry
# Configure backoff strategyretries = Retry( total=3, backoff_factor=0.5, status_forcelist=[429, 500, 502, 503, 504], raise_on_status=False,)
# Attach retries globally to the pool managerhttp = urllib3.PoolManager(retries=retries)
def lambda_handler(event, context): response = http.request("GET", "https://api.example.com/reliable-endpoint") return {"status": response.status}
Recap: Why You Should Drop requests in Your Lambdas
Switching from
requests to urllib3 in your Lambdas gives your engineering team immediate wins:- Zero third-party dependencies to pack, zip, or build into layers.
- Automatic vulnerability patching maintained by AWS.
- Faster average invocation and lightning-fast warm executions via persistent connection pools.
- Clean audit reports with zero transitive CVE warnings.
Your code stays clean, your Lambdas run faster, and your CTO doesn't have to worry about the next dependency zero-day.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article