首页 > 解决方案 > 通过 Lambda 函数从特定步骤调用步骤函数

问题描述

以下是在每一步中触发不同 lambda 函数的 step 函数。step 函数从“first_step”开始。

{
"Comment": "Step function",
"StartAt": "first_step",
"States": {
"first_step": {
   "Type": "Task",
   "Resource": "lambda_function1",
   "Next": "second_step"
 },
"second_step": {
    "Type": "Task",
   "Resource": "lambda_function2",
   "Next": "third_step"
 },
 "third_step": {
   "Type": "Task",
   "Resource": "lambda_function3",
  "End" : true
 }
}
}

现在,我想通过 Lambda 函数从特定步骤 (second_step) 调用 step 函数。也就是说,一旦我触发另一个 Lambda 函数 (lambda_function4) ,step 函数应该从 second_step 开始执行(跳过 first_step)并一直持续到结束。

另外,我使用 Python 来创建 Lambda 函数。

标签: python-3.xamazon-web-servicesaws-lambdaaws-step-functions

解决方案


您需要在函数的开头添加一个选择步骤,以确定接下来要转到的 lambda 并传入一个参数来区分。因为您不能使用具体从哪个步骤开始的选项来调用步骤函数

所以它看起来像:

{
"Comment": "Step function",
"StartAt": "flowDirector",
"States": {
    "flowDirector": {
    "Type" : "Choice",
    "Choices": [
      {
        "Variable": "$.customVarName",
        "StringEquals": "Cancel",
        "Next": "first_step"
      },
      {
        "Variable": "$.customVarName",
        "StringEquals": "CameFromFunction4",
        "Next": "second_step"
      }
    ],
    "Default": "first_step"
    },
"first_step": {
   "Type": "Task",
   "Resource": "lambda_function1",
   "Next": "second_step"
 },
"second_step": {
    "Type": "Task",
   "Resource": "lambda_function2",
   "Next": "third_step"
 },
 "third_step": {
   "Type": "Task",
   "Resource": "lambda_function3",
  "End" : true
 }
}
}

然后更新你的 python 代码,将一个额外的参数发送到 step 函数中,这样它就可以计算从哪里开始。

response = client.start_execution(
    stateMachineArn='string',
    name='string',
    input='"{\"customVarName\" : \"CameFromFunction4\"}"'
)

来自:https ://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/stepfunctions.html#SFN.Client.start_execution


推荐阅读