The context parameter can be omitted. But if you need it, it has to be the second parameter.
So both of these are fine:
exports.handler = async (event, context) => {};
and without context:
exports.handler = async (event) => {};
You can read more about NodeJS handlers and context in the official AWS documentation:
- AWS Lambda function handler in Node.js
- AWS Lambda context object in Node.js
Where is the context parameter in the current Node.js AWS Lambda function template? - Stack Overflow
javascript - What are event and context in function call in AWS Lambda? - Stack Overflow
Why would I use Node.js in Lambda? Node main feature is handling concurrent many requests. If each request to lambda will spawn a new Node instance, whats the point?
node.js - Access to aws-lambda context when running nodejs + expressjs - Stack Overflow
Videos
Event represents the event or trigger that caused the invocation of the lambda. For example, if your lambda is triggered by an upload to S3 this will contain information about the object being uploaded for example:
{
"Records": [
{
"eventVersion": "2.0",
"eventTime": "1970-01-01T00:00:00.000Z",
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"s3": {
"configurationId": "testConfigRule",
"object": {
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901",
"key": "HappyFace.jpg",
"size": 1024
},
"bucket": {
"arn": bucketarn,
"name": "sourcebucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
}
},
"s3SchemaVersion": "1.0"
},
"responseElements": {
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
"x-amz-request-id": "EXAMPLE123456789"
},
"awsRegion": "us-east-1",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"eventSource": "aws:s3"
}
]
}
Here is detailed information about events, with an example.
Context Provides information about the invocation, function, and execution environment of your lambda. So you can use this to check the memory allocation or to retrieve the number of milliseconds left before the execution times out. Here is detailed documentation about context, with an example.
From the docs
When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about the invocation, function, and execution environment.
Event (and the args) are described here.
To put it more simply, think of event as an input to a regular function. Context is an extra input supplied by AWS to give you a variety of meta context and such.
Maybe I'm missing something here, from an architectural point of view, I can't wrap my head on using node inside a lambda. Let's say I receive 3 requests, a single node instance would be able to handle this with ease, but if I use lambda, 3 lambdas with Node inside would be spawned, each would be idle while waiting for the callback.
Edit: Many very good answers. I will for sure discuss this with the team next week. Very happy with this community. Thanks and please keep them coming!
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;
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
I am writing a Python Lambda that will be invoked via HTTP. A web service will make an HTTP call to an API Gateway resource that I define, which will then invoke the Lambda. My Lambda handler will look like:
def lambda_handler(event, context):
// do stuff down here
return responseObjectI am trying to find documentation on event and context so I know how to do things like:
-
extract query string parameters from requests
-
extract path parameters from requests
-
inspect the request entity
-
etc.
Surprising I can find no official AWS documentation on what fields/properties these two objects have on them when they are invoked from an API Gateway resource action. I found this article which was sort of helpful but nothing official from AWS. Can anyone point me in the right direction?
» npm install aws-lambda-mock-context