From f376836293faaccafc91d6f3c3cafd49a41a027f Mon Sep 17 00:00:00 2001 From: mbfreder Date: Tue, 29 Aug 2023 15:32:25 -0700 Subject: [PATCH] Update express-zip example to show Lambda Context usage --- README.md | 7 +++++++ examples/expressjs-zip/hello-world/app.js | 14 ++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6ccfb5c5..d0dac6f0 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,13 @@ Please check out [FastAPI with Response Streaming](examples/fastapi-response-str Lambda Web Adapter forwards this information to the web application in a Http Header named "x-amzn-request-context". In the web application, you can retrieve the value of this http header and deserialize it into a JSON object. Check out [Express.js in Zip](examples/expressjs-zip) on how to use it. + +## Lambda Context + +**Lambda Context** is an object that Lambda passes to the function handler. This object provides information about the invocation, function, and execution environment. You can find a full list of properties accessible through the Lambda Context [here](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html) + +Lambda Web Adapter forwards this information to the web application in a Http Header named "x-amzn-lambda-context". In the web application, you can retrieve the value of this http header and deserialize it into a JSON object. Check out [Express.js in Zip](examples/expressjs-zip) on how to use it. + ## Graceful Shutdown For a function with Lambda Extensions registered, Lambda enables shutdown phase for the function. When Lambda service is about to shut down a Lambda execution environment, diff --git a/examples/expressjs-zip/hello-world/app.js b/examples/expressjs-zip/hello-world/app.js index 7993354f..f6bbb6e4 100644 --- a/examples/expressjs-zip/hello-world/app.js +++ b/examples/expressjs-zip/hello-world/app.js @@ -5,11 +5,17 @@ const port = process.env['PORT'] || 8080 app.get('/', (req, res) => { // deserialize request context from the http header 'x-amzn-request-context' - let context = req.headers['x-amzn-request-context'] || null - let requestContext = context != null? JSON.parse(context) : null + let requestContextHeader = req.headers['x-amzn-request-context'] || null + let requestContext = requestContextHeader != null? JSON.parse(requestContextHeader) : null + + // deserialize lambda context from the http header 'x-amzn-lambda-context' + let lambdaContextHeader = req.headers['x-amzn-lambda-context'] || null + let lambdaContext = lambdaContextHeader != null? JSON.parse(lambdaContextHeader) : null + res.send({ - messagge: 'Hi there!', - requestContext + message: 'Hi there!', + requestContext, + lambdaContext }) })