Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update express-zip example to show Lambda Context usage #281

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions examples/expressjs-zip/hello-world/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})

Expand Down