14

I'm, just starting out with AWS-Lambda, AWS-API Gateway and ExpressJs. I'm having trouble finding how the AWS-Lambda "context" is available in my "ExpressJs" application.

I'm using:

  • AWS-Lambda
  • AWS-API Gateway
  • NodeJs v4.3.2
  • ExpressJs 4.14.1
  • ClaudiaJs 2.7.0

In Aws Lambda I use aws-serverless-express to receive the API-Gateway request and initialize the node application. The following is the structure I have found from different tutorials, etc

lambda.js (Initiated from API-Gateway. Supplying the "context" variable in the call to "app.js")

'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)

The core of my app.js express is:

var express = require('express');
...
var app = express();
...
app.use('/', index);
...
module.exports = app;

My questions:

  1. Is there a way to access the AWS-Lambda "context" with this structure?
  2. If not, what would be the best "pattern" to make it available?

Any input appreciated.

2 Answers 2

19

From v4.0 you need to use getCurrentInvoke as described in @cooow's answer.

Prior versions supported serverless-express/middleware:

... which exposes the event and context objects. You add it like this:

const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
app.use(awsServerlessExpressMiddleware.eventContext())

Once this middleware is configured the event and context objects will be added to the request. You access those objects like so:

var event = req.apiGateway.event;
var context = req.apiGateway.context;
Sign up to request clarification or add additional context in comments.

1 Comment

Thx, Works fine. A few tips for others though they might be obvious: (1.) "app.use(awsServerlessExpressMiddleware.eventContext())" must be done before any "app.use('/', index);" that will use the API-Gateway context/event (2.) General info available here github.com/awslabs/aws-serverless-express
3

With version 4.x (2023-24) of the @codegenie/serverless-express package, you access the API Gateway event context using getCurrentInvoke in your controllers :

const { getCurrentInvoke } = require('@codegenie/serverless-express')
...
const { event, context } = getCurrentInvoke()

Source

3 Comments

I looked into the implementation of the getCurrentInvoke and it seems rather buggy to me. raw.githubusercontent.com/CodeGenieApp/serverless-express/… This doesn't factor in the fact that Lambda will reuse the same instance to serve multiple requests concurrently if you're using Node.js 10.x runtime or later. This pattern does not account for concurrent execution within the same Lambda instance, which could lead to incorrect event and context data being accessed across different invocations.
Very interesting point @MridangAgarwalla ! Do you have any suggestions to avoid this issue, maybe another way to extract context from event directly ?
for anyone who is concerned about this, apparently it isn't a problem: github.com/CodeGenieApp/serverless-express/issues/674

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.