通过Lambda函数从特定步骤调用步骤功能

时间:2019-12-11 12:14:12

标签: python-3.x amazon-web-services aws-lambda aws-step-functions

以下是step函数,它在每个步骤中触发不同的lambda函数。步进功能从“ 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函数。

1 个答案:

答案 0 :(得分:1)

您将需要在函数的开头添加一个Choice步骤,以确定接下来要转到的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