javascript AWS Lambda 函数调用中的事件和上下文是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53936773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are event and context in function call in AWS Lambda?
提问by Erik Olson
In the following code, what do the arguments eventand contextrefer to?
在下面的代码中,参数event和context指的是什么?
module.exports.convertTime = (event, context, callback) => {
const response = {
statusCode: statusCode,
body: JSON.stringify({
message: `${convertedTime}`
})
};
callback(null, response);
}
回答by Jarred Olson
Eventrepresents 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:
Event表示导致 lambda 调用的事件或触发器。例如,如果您的 lambda 是由上传到 S3 触发的,这将包含有关正在上传的对象的信息,例如:
{
"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.
以下是有关事件的详细信息,并附有示例。
ContextProvides 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.
Context提供有关 lambda 的调用、函数和执行环境的信息。因此,您可以使用它来检查内存分配或检索执行超时前剩余的毫秒数。这是有关上下文的详细文档,并附有示例。
回答by dsp_099
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.
当 Lambda 运行您的函数时,它会将上下文对象传递给处理程序。此对象提供了提供有关调用、函数和执行环境的信息的方法和属性。
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.
更简单地说,将事件视为常规函数的输入。上下文是 AWS 提供的额外输入,用于为您提供各种元上下文等。
回答by Erik Olson
The eventargument carries the input parameters for the function and is in JSON syntax.
所述事件参数携带用于该函数的输入参数,并且在JSON语法。
For example, we could access a variable stored as a key:value pair in the query string of a url passed in the eventby:
例如,我们可以通过以下方式访问在事件中传递的 url 的查询字符串中存储为键:值对的变量:
event.queryStringParameters.time
So for a POST request to https://fakename.execute-api.us-east-1.amazonaws.com/dev/convertTime?time=2:55:55AM:
因此,对于https://fakename.execute-api.us-east-1.amazonaws.com/dev/convertTime?time=2:55:55AM的 POST 请求:
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.
event.queryStringParameters.time 将等于“2:55:55AM”,我们可以在 lambda 函数的其余部分中使用该信息做我们想做的事情。
The contextargument 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. ..."
“当 Lambda 运行您的函数时,它会将上下文对象传递给处理程序。该对象提供方法和属性,这些方法和属性提供有关调用、函数和执行环境的信息。
上下文方法
getRemainingTimeInMillis() – 返回执行超时前剩余的毫秒数。
上下文属性
functionName – Lambda 函数的名称。functionVersion – 函数的版本。connectedFunctionArn – 用于调用函数的 Amazon 资源名称 (ARN)。指示调用者是否指定了版本号或别名。
memoryLimitInMB – 在函数上配置的内存量。……”

