@aws-lambda/http
http is a utility for simplifying response handling when using aws lambda functions with the http event proxy.
Table of Contents
Installation and Usage
Prerequisites: Node.js >=8
, npm version 6+.
You can install the http module using npm:
$ npm install @aws-lambda/http --save
Response Examples
AWS Lambda handler response with JSON body and header
const Response = require("@aws-lambda/http").Response;
module.exports.hello = async event => {
return new Response()
.status(200)
.body({"message" : "hello world!"})
.header("x-api-version", "1.0")
.json();
};
Lambda@Edge + CloudFront)
Adding HTTP security headers to response (alternative toconst HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;
module.exports.hello = async event => {
return new Response()
.status(200)
.body({"message" : "hello world!"})
.header("x-api-version", "1.0")
.headers(HTTP.SecurityHeaders)
.json();
};
Adding cookie to response
const HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;
const Cookie = HTTP.Cookie;
module.exports.hello = async event => {
return new Response()
.status(200)
.body({"message" : "hello world!"})
.header("x-api-version", "1.0")
.headers(HTTP.SecurityHeaders)
.cookie(new Cookie("SSID", "ubmHxGlgi71l8ayOMw9f6wknjGv2FwDKLt")))
.json();
};
Adding secure cookie to response
const HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;
const Cookie = HTTP.Cookie;
module.exports.hello = async event => {
return new Response()
.status(200)
.body({"message" : "hello world!"})
.header("x-api-version", "1.0")
.headers(HTTP.SecurityHeaders)
.cookie(Cookie.Secure("SSID", "ubmHxGlgi71l8ayOMw9f6wknjGv2FwDKLt"))
.json();
};
Adding multiple cookies to response
const HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;
const Cookie = HTTP.Cookie;
module.exports.hello = async event => {
return new Response()
.status(200)
.body({"message" : "hello world!"})
.header("x-api-version", "1.0")
.headers(HTTP.SecurityHeaders)
.cookies([
Cookie.Secure("SSID", "ubmHxGlgi71l8ayOMw9f6wknjGv2FwDKLt"),
Cookie.Secure("secret", "shhhhh").expireInMinutes(15),
new Cookie("lang", "en").path("/docs")
])
.json();
};