Setting http response header from AWS lambda
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In the realm of cloud computing, AWS Lambda provides a powerful tool for running code without the need for provisioning or managing servers. Common scenarios where AWS Lambda sees extensive use include back-end services, data processing, and as a function of AWS’s event-driven architecture. Among its many features, Lambda's ability to interact actively with AWS API Gateway to respond to HTTP requests is crucial. A vital component of this interaction involves setting HTTP response headers.
Understanding HTTP Response Headers
HTTP response headers allow the server to pass additional information about the response, which is not part of the body itself. They are instrumental in controlling aspects such as caching, content type, and even security.
Common HTTP Response Headers
- Content-Type: Indicates the media type of the resource.
- Cache-Control: Directs how and for how long the browser should cache the response data.
- CORS Headers: Facilitates secure cross-origin requests and data sharing between browsers and servers in different origin domains.
- Custom Headers: Designed to carry application-specific metadata or control parameters.
Setting HTTP Response Headers in AWS Lambda
AWS Lambda functions can be integrated with AWS API Gateway to handle incoming HTTP requests. When a Lambda function is triggered via API Gateway, the function can return a response containing headers, a status code, and the body of the response. Here's how to set the response headers in a Node.js Lambda function:
Example: Node.js Function
Below is a practical example of an AWS Lambda function written in Node.js that sets headers in its HTTP response.
- Headers Setting: In the response object, the
headersfield is utilized to define the headers we want to set. Headers likeContent-Type,Access-Control-Allow-Origin, and custom headers such asCustom-Headercan be configured as shown. - CORS: The
Access-Control-Allow-Originheader is crucial for enabling CORS (Cross-Origin Resource Sharing), allowing requests from any origin by using*. CORS is a crucial security feature for web applications that need to access resources from different domains. - Content-Type: This header is set to
application/json, indicating that the response body should be interpreted as JSON.
Related reading
- Setting the capability for aws cloudformation template-validate
- Setting up FTP on Amazon Cloud Server
- Setting up Intellij and AWS environment to work with DynamoDB
- Setting up JMeter for Distributed testing in AWS with connectivity issues
- Setup Kubernetes Pods via API Call using Go and Operator SDK
- Should API and message consumer be in the same microservice?
- Shared dependencies with HELM
- Ship an application with a database

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.