HTTP Requests in an AWS Lambda
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
HTTP work inside AWS Lambda usually means two different things: receiving an HTTP-style event from API Gateway or Function URLs, and making outbound HTTP requests to other services. Both sides need explicit timeout, error, and logging behavior because Lambda’s short-lived execution model amplifies weak network assumptions.
Handle Inbound HTTP Events Predictably
When Lambda is fronted by API Gateway proxy integration, the event contains method, path, headers, query parameters, and body. Your handler should parse defensively and return the exact response shape that the integration expects.
The key detail is that body must be a string for the normal proxy response contract.
Reuse Outbound HTTP Clients Across Warm Invocations
For outbound requests, create the HTTP client at module scope so warm Lambda invocations can reuse connections. Recreating a client on every invocation adds latency and can increase connection churn unnecessarily.
That pattern is simple, fast, and works well for many Python Lambda functions.
Fit HTTP Timeouts Inside the Lambda Timeout Budget
Lambda itself has a hard execution timeout. Your outbound HTTP calls should time out well before that limit so the function still has room to log, retry if appropriate, and return a controlled response.
A good mental model is:
- short connect timeout
- bounded read timeout
- minimal retry count
- enough remaining time for cleanup and response shaping
Unbounded HTTP waits are especially expensive in serverless code because they burn both time and money while producing poor diagnostics.
Map Upstream Failures Intentionally
Do not let every network problem collapse into the same generic error. Different failure modes should map to predictable caller behavior.
That gives both clients and operators clearer signals than a generic unhandled exception.
Node.js Lambda Follows the Same Principles
If the function runs on Node.js, the same operational rules apply even though the syntax changes.
The runtime differs, but the design does not: explicit timeouts, clear mapping, and structured output.
VPC Networking Can Break Outbound HTTP
If Lambda runs inside private subnets, outbound internet requests need proper NAT or another egress path. Missing egress often looks like a slow timeout, not a clean connection-refused error. When HTTP calls fail only in VPC mode, check route tables, security groups, and DNS first.
This is one of the most common reasons “the same code works locally but times out in Lambda.”
Protect Secrets and Improve Observability
Do not hardcode API keys or auth tokens into the function source. Use Secrets Manager or Parameter Store, and avoid logging sensitive headers. At the same time, log request IDs, destination hostnames, elapsed time, and high-level failure category so production debugging stays possible.
Good Lambda HTTP code is not just about making the request. It is about making the request diagnosable.
Common Pitfalls
- Returning a non-proxy-compatible response shape to API Gateway.
- Recreating the HTTP client on every invocation instead of reusing it.
- Letting outbound HTTP timeouts exceed the Lambda execution budget.
- Running Lambda in a VPC without correct outbound network configuration.
- Logging credentials or auth headers while debugging failed requests.
Summary
- Lambda HTTP work includes both inbound event handling and outbound requests.
- Reuse HTTP clients across warm invocations for better latency and efficiency.
- Keep outbound timeouts shorter than the Lambda timeout budget.
- Map upstream failures into clear, stable response semantics.
- Treat networking, secrets, and observability as part of the HTTP design.
Related reading
- https on S3 WITHOUT cloudfront possible?
- I am using Azure Devops to build and push my Docker image. How can I pass arguments while doing buildAndPush using Docker task?
- I cannot install aws cli on mac os with pip - awscli command not found
- I can't delete my VPC
- HTTP response code for POST when resource already exists
- http server respnd with an output from an async function
- I can't find callback parameter in python lambda handler
- I can't run more than 5 tasks in AWS ECS Fargate

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.