Skip to content

Commit 80f585b

Browse files
authored
Update express-zip example to show Lambda Context usage (#281)
1 parent 36d62c6 commit 80f585b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ Please check out [FastAPI with Response Streaming](examples/fastapi-response-str
143143

144144
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.
145145

146+
147+
## Lambda Context
148+
149+
**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)
150+
151+
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.
152+
146153
## Graceful Shutdown
147154

148155
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 for: examples/expressjs-zip/hello-world/app.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ const port = process.env['PORT'] || 8080
55

66
app.get('/', (req, res) => {
77
// deserialize request context from the http header 'x-amzn-request-context'
8-
let context = req.headers['x-amzn-request-context'] || null
9-
let requestContext = context != null? JSON.parse(context) : null
8+
let requestContextHeader = req.headers['x-amzn-request-context'] || null
9+
let requestContext = requestContextHeader != null? JSON.parse(requestContextHeader) : null
10+
11+
// deserialize lambda context from the http header 'x-amzn-lambda-context'
12+
let lambdaContextHeader = req.headers['x-amzn-lambda-context'] || null
13+
let lambdaContext = lambdaContextHeader != null? JSON.parse(lambdaContextHeader) : null
14+
1015
res.send({
11-
messagge: 'Hi there!',
12-
requestContext
16+
message: 'Hi there!',
17+
requestContext,
18+
lambdaContext
1319
})
1420
})
1521

0 commit comments

Comments
 (0)