AWS Lambda
HTTP Response
Response Header
Serverless Computing
Cloud Functions

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.

Practice system design

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 headers field is utilized to define the headers we want to set. Headers like Content-Type , Access-Control-Allow-Origin , and custom headers such as Custom-Header can be configured as shown.
  • CORS: The Access-Control-Allow-Origin header 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.