![](/img/trans.png)
[英]How to combine context object with task input in step function Lambda invoke?
[英]Pass and use input (parameters) to a lambda task from a step function
我有一个启动 lambda 的简单步骤函数,我正在寻找一种将参数(事件/上下文)传递给几个后续任务中的每一个的方法。 我的步骤函数如下所示:
{
"Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Parameters": {
"TableName": "table_example"
},
"Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync",
"End": true
}
}
}
在用 Python 编写的 lambda 中,我使用了一个简单的处理程序,它是:
def lambda_handler(event, context):
#...
事件和上下文如下所示(检查日志):
开始请求 ID:f58140b8-9f04-47d7-9285-510b0357b4c2 版本:$LATEST
我找不到将参数传递给此 lambda 并在脚本中使用它们的方法。 本质上,我想要做的是运行相同的 lambda,将几个不同的值作为参数传递。
有人可以指出我正确的方向吗?
根据您所说的: “寻找一种将参数(事件/上下文)传递给几个后续任务中的每一个的方法”,我假设您想将非静态值传递给 lambda。
有两种方法可以通过状态机传递参数。 通过InputPath
和Parameters
。 有关差异,请查看此处。
如果您没有要传递给 lambda 的任何静态值,我将执行以下操作。 将所有参数以 json 格式传递给 step 函数。
状态机的输入 JSON
{
"foo": 123,
"bar": ["a", "b", "c"],
"car": {
"cdr": true
}
"TableName": "table_example"
}
在 step 函数中,您使用"InputPath": "$"
将整个 JSON 显式传递给 lambda,除了隐式传递的第一步。 有关$
path 语法的更多信息,请查看此处。 您还需要通过使用ResultPath
的多种方法之一来处理任务结果。 对于大多数情况,最安全的解决方案是将任务结果保存在特殊变量"ResultPath": "$.taskresult"
{
"Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync",
"Next": "HelloWorld2"
},
"HelloWorld2": {
"Type": "Task",
"InputPath": "$",
"ResultPath": "$.taskresult"
"Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync_2",
"End": true
}
}
}
在 lambda 中哪个变成了事件变量,可以作为 python 字典访问
def lambda_handler(event, context):
table_example = event["TableName"]
a = event["bar"][0]
cdr_value = event["car"]["cdr"]
# taskresult will not exist as event key
# only on lambda triggered by first state
# in the rest of subsequent states
# it will hold a task result of last executed state
taskresult = event["taskresult"]
通过这种方法,您可以使用多个 step 函数和不同的 lambdas,并通过移动 lambdas 中的所有逻辑来保持它们的干净和小。 此外,调试更容易,因为所有 lambda 表达式中的所有事件变量都相同,因此通过简单的print(event)
您可以查看整个状态机所需的所有参数以及可能出错的地方。
显然,当Resource
设置为 lambda ARN(例如"arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync"
)时,我遇到了这个问题,您不能使用Parameters
来指定输入和而是传递阶跃函数的状态(可能是状态函数的输入,如果它之前没有阶跃)。
要通过参数传递函数输入,您可以将 Resource 指定为"arn:aws:states:::lambda:invoke"
并在参数部分提供您的FunctionName
:
{
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "YOUR_FUNCTION_NAME",
"Payload": {
"SOMEPAYLOAD": "YOUR PAYLOAD"
}
},
"End": true
}
}
}
您可以在此处找到调用 Lambda 函数的文档: https : //docs.aws.amazon.com/step-functions/latest/dg/connect-lambda.html
您还可以潜在地使用 inputPath,或者也可以使用来自您的 step 函数状态函数的元素: https ://docs.aws.amazon.com/step-functions/latest/dg/input-output-inputpath-params.html
就像 Milan 在他的评论中提到的那样,您可以将数据从 Step Function State 传递给 Lambda 函数。
在 Lambda 函数中,您需要读取event
内容。
import json
def lambda_handler(event, context):
TableName = event['TableName']
出于某种原因,在Resource
中直接指定lambda function ARN
不起作用。
以下解决方法纯粹是使用 ASL 定义,您只需在带参数之前创建一个Pass
步骤,其输出将用作下一步的输入(您的步骤HelloWorld
与 lambda 调用):
{
"Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
"StartAt": "HelloParams",
"States": {
"HelloParams": {
"Type": "Pass",
"Parameters": {
"TableName": "table_example"
},
"Next": "HelloWorld"
},
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync",
"End": true
}
}
}
另一个响应中有一个解决方法,它告诉您使用 lambda 函数执行上一步,但对于简单情况不需要它。 还可以映射上下文值,例如当前时间戳:
"HelloParams": {
"Type": "Pass",
"Parameters": {
"TableName": "table_example",
"Now": "$$.State.EnteredTime"
},
"Next": "HelloWorld"
},
此外, InputPath
和ResultPath
可用于防止覆盖先前步骤中的值。 例如:
{
"Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
"StartAt": "HelloParams",
"States": {
"HelloParams": {
"Type": "Pass",
"Parameters": {
"TableName": "table_example"
},
"ResultPath": "$.hello_prms",
"Next": "HelloWorld"
},
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync",
"InputPath": "$.hello_prms",
"ResultPath": "$.hello_result",
"End": true
}
}
}
这会将参数保存在hello_prms
(以便您可以在其他步骤中重用它们),并将执行结果保存在hello_result
而无需来自先前步骤的值(以防您添加它们)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.