Reputation: 423
In the following code, what do the arguments event
and context
refer to?
module.exports.convertTime = (event, context, callback) => {
const response = {
statusCode: statusCode,
body: JSON.stringify({
message: `${convertedTime}`
})
};
callback(null, response);
}
Upvotes: 26
Views: 46988
Reputation: 3243
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.
Upvotes: 29
Reputation: 423
The event argument carries the input parameters for the function and is in JSON syntax.
For example, we could access a variable stored as a key:value pair in the query string of a url passed in the event by:
event.queryStringParameters.time
So for a POST request to https://fakename.execute-api.us-east-1.amazonaws.com/dev/convertTime?time=2:55:55AM :
curl -X POST -H "x-api-key: FAKEAPIKEY23423402394" https://fakename.execute-api.us-east-1.amazonaws.com/dev/convertTime?time=2:55:55AM
event.queryStringParameters.time will equal "2:55:55AM", and we can do what we wish with that information in the rest of the lambda function.
The context argument provides methods and properties that provide information about the invocation, function, and execution environment.
From the AWS Lambda Documentation:
"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.
Context Methods
getRemainingTimeInMillis() – Returns the number of milliseconds left before the execution times out.
Context Properties
functionName – The name of the Lambda function. functionVersion – The version of the function. invokedFunctionArn – The Amazon Resource Name (ARN) used to invoke the function. Indicates if the invoker specified a version number or alias.
memoryLimitInMB – The amount of memory configured on the function. ..."
Upvotes: 3
Reputation: 6121
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.
Upvotes: 2