Getting json body in aws Lambda via API gateway
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
AWS Lambda is a serverless computing service that runs your code in response to events and automatically manages the underlying compute resources. When paired with API Gateway, Lambda can provide a powerful platform for building and deploying APIs. One common scenario is needing to extract JSON data from an incoming HTTP request processed by API Gateway. This article guides you through this process and explores the mechanics of efficiently handling JSON payloads in AWS Lambda.
Understanding the Event Object
When an API Gateway method triggers a Lambda function, it sends an event object to the Lambda function containing data about the HTTP request. This includes details such as headers, query parameters, and importantly, the body of the request if applicable. The event object is a JSON structure, and for HTTP API methods, the event object has the following notable fields:
httpMethod: The HTTP method used (GET, POST, PUT, DELETE, etc.).headers: A map of request header names to their values.queryStringParameters: A map of query parameters.body: The request payload as a string.
Accessing the JSON Body
To read the JSON body from the event object in Lambda, you need to extract and parse it. Here's how you can achieve this in a Node.js environment:
In this example, the Lambda function:
- Extracts the string representation of the JSON body from
event.body. - Attempts to parse this string using
JSON.parse(). - Accesses and uses the parsed JSON data.
- Returns an HTTP response back to the client.
Handling Various Content Types
API Gateway can pass different types of payloads to Lambda. It's crucial to set up your integration request to allow for different content types if needed. This typically involves configuring the API Gateway method settings to accept a content type, such as application/json.
Configuring API Gateway
Ensure that your API Gateway configuration includes:
- Integration Type: Lambda Proxy integration is the simplest and straightforward method for passing requests to Lambda functions. This involves API Gateway passing the full HTTP request to Lambda.
- Method Request: CORS settings, required query parameters, and headers might need to be configured based on how your API is being accessed.
Example Configuration via AWS Console
- Create an API: Navigate to the API Gateway in the AWS Management Console. Create or select an existing REST API or HTTP API.
- Define Resources and Methods: Add resources (e.g.,
/items) and HTTP methods (e.g., POST, GET). - Set Integration: For each method, set the integration type to “Lambda Function” and select the appropriate handler.
- Deploy API: Create a deployment stage for accessing the API via the defined endpoint.
Key Considerations
- Performance: API Gateway and Lambda are serverless but have limits on request size. The payload passed to Lambda is limited by default at 6 MB for REST and 256 KB for HTTP APIs.
- Security: Use AWS IAM roles to ensure that only authorized requests can trigger Lambda functions. Consider enabling AWS Web Application Firewall (WAF) for additional protection.
- Error Handling: Robust error handling inside the Lambda function ensures that clients receive meaningful and consistent responses.
Summary Table
| Component | Key Detail |
| Event Object Structure | Contains method, headers, query parameters, body. |
| Accessing JSON body | Use JSON.parse(event.body), with error handling. |
| Integration Configuration | Use Lambda Proxy integration for simplicity. |
| Content-Type Management | Configure API Gateway to accept application/json. |
| Size Limitations | 6 MB for REST, 256 KB for HTTP APIs. |
| Security Practices | Use IAM roles and AWS WAF for securing endpoints. |
By following this guide, you should be able to leverage AWS Lambda and API Gateway effectively to handle JSON bodies, enabling robust serverless APIs. With the robust ecosystem and tools AWS provides, managing serverless functions is simplified, empowering you to build responsive and scalable applications.
Related reading
- getting message forbidden reply from AWS API gateway
- Getting S3 objects' last modified datetimes with boto
- Getting started with secure AWS CloudFront streaming with Python
- getting The bucket does not allow ACLs Error
- Getting not supported media type error
- Getting the IP address of the current machine using Java
- GKE - How to serve HTTPS via the L7 load balancer?
- GKE autopilot has scaled up my container resources contary to resource requests

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.