amazon-web-services - AWS Step Function - 根据条件停止状态机执行

标签 amazon-web-services aws-step-functions

我已附上我的状态机的外观,并共享定义以供引用。 Service Status Retry Wait 状态在调用 Service Status Retry 状态之前等待 60 秒,该状态调用 lambda 来检查服务是否已在 Service Created? 中创建? 状态。

{
  "Comment": "Step Function for Deployment",
  "StartAt": "Start",
  "States": {
    "Start": {
      "Type": "Pass",
      "Next": "Service Status"
    },
    "Service Status": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "Service Status Check",
          "States": {
            "Service Status Check": {
              "Type": "Task",
              "Resource": "arn:aws:states:::lambda:invoke",
              "Parameters": {
                "FunctionName": "arn:aws:lambda:us-west-2:<acc-id>:function:<fn-name>",
                "Payload": {
                  "stepFunctionState.$": "$$.State.Name",
                  "requestContext": {
                    "stepFunction": true,
                    "accountId": "<acc-id>"
                  },
                  "resource.$": "States.Format('{}/srvc', $$.Execution.Input.resource)",
                  "path.$": "States.Format('{}/srvc', $$.Execution.Input.path)",
                  "httpMethod": "POST",
                  "pathParameters.$": "$$.Execution.Input.pathParameters",
                  "body.$": "$$.Execution.Input.body",
                  "isBase64Encoded": false,
                  "headers": {},
                  "queryStringParameters": null
                }
              },
              "ResultPath": "$.taskresult",
              "Next": "Service Created?"
            },
            "Service Created?": {
              "Type": "Choice",
              "Choices": [
                {
                  "Variable": "$.taskresult.Payload.body",
                  "StringMatches": "*\"isCreated\": \"false\"*",
                  "Next": "Service Status Retry Wait"
                },
                {
                  "Variable": "$.taskresult.Payload.body",
                  "StringMatches": "*\"isCreated\": \"true\"*",
                  "Next": "Service Status Check End"
                }
              ],
              "Default": "Service Status Retry Wait"
            },
            "Service Status Retry Wait": {
              "Type": "Wait",
              "Seconds": 60,
              "Next": "Service Status Retry"
            },
            "Service Status Retry": {
              "Type": "Task",
              "Resource": "arn:aws:states:::lambda:invoke",
              "Parameters": {
                "FunctionName": "arn:aws:lambda:us-west-2:<acc-id>:function:<func-name>",
                "Payload": {
                  "stepFunctionState.$": "$$.State.Name",
                  "requestContext": {
                    "stepFunction": true,
                    "accountId": "<acc-id>"
                  },
                  "resource.$": "States.Format('{}/srvc', $$.Execution.Input.resource)",
                  "path.$": "States.Format('{}/srvc', $$.Execution.Input.path)",
                  "httpMethod": "POST",
                  "pathParameters.$": "$$.Execution.Input.pathParameters",
                  "body.$": "$$.Execution.Input.body",
                  "isBase64Encoded": false,
                  "headers": {},
                  "queryStringParameters": null
                }
              },
              "ResultPath": "$.taskresult",
              "Next": "Service Created?"
            },
            "Service Status Check End": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ],
      "Next": "End"
    },
    "End": {
      "Type": "Pass",
      "End": true
    }
  }
}

直到并且除非创建服务,否则此状态机将继续循环运行。我想知道是否有办法打破循环并在重试 5 次后退出。它可能是在服务状态重试等待服务状态重试被调用5次之后发生。

或者是否有办法知道它现在已经循环了 1 小时并据此退出。上述两个选项中的任何一个都应该适合我。

关于如何实现这一目标有什么想法/想法吗? enter image description here

最佳答案

您有 2 个选择:

  1. 通过保留重试次数来自行处理重试,并在您选择的状态下检查它: enter image description here

  2. 使用内置错误/重试。为此,检查服务是否已创建,并抛出错误并将其包装为并行状态,重试次数为 5:

{
  "Comment": "Step Function for Deployment",
  "StartAt": "Start",
  "States": {
    "Start": {
      "Type": "Pass",
      "Next": "Service Status"
    },
    "Service Status": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "Service Status Check",
          "States": {
            "Service Status Check": {
              "Type": "Task",
              "Resource": "arn:aws:states:::lambda:invoke",
              "Parameters": {
                "FunctionName": "arn:aws:lambda:us-west-2:<acc-id>:function:<fn-name>",
                "Payload": {
                  "stepFunctionState.$": "$$.State.Name",
                  "requestContext": {
                    "stepFunction": true,
                    "accountId": "<acc-id>"
                  },
                  "resource.$": "States.Format('{}/srvc', $$.Execution.Input.resource)",
                  "path.$": "States.Format('{}/srvc', $$.Execution.Input.path)",
                  "httpMethod": "POST",
                  "pathParameters.$": "$$.Execution.Input.pathParameters",
                  "body.$": "$$.Execution.Input.body",
                  "isBase64Encoded": false,
                  "headers": {},
                  "queryStringParameters": null
                }
              },
              "ResultPath": "$.taskresult",
              "Next": "Service Created?"
            },
            "Service Created?": {
              "Type": "Choice",
              "Choices": [
                {
                  "Variable": "$.taskresult.Payload.body",
                  "StringMatches": "*\"isCreated\": \"false\"*",
                  "Next": "Throw Error"
                },
                {
                  "Variable": "$.taskresult.Payload.body",
                  "StringMatches": "*\"isCreated\": \"true\"*",
                  "Next": "Succeed"
                }
              ],
              "Default": "Throw Error"
            },
            "Throw Error": {
              "Type": "Fail"
            },
            "Succeed": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ],
      "Next": "End",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "IntervalSeconds": 3,
          "MaxAttempts": 5,
          "BackoffRate": 1.5
        }
      ]
    },
    "End": {
      "Type": "Pass",
      "End": true
    }
  }
}

enter image description here

关于amazon-web-services - AWS Step Function - 根据条件停止状态机执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66874953/

相关文章:

node.js - AWS Lambda 函数执行多次(无服务器)

amazon-web-services - AWS Stepfunctions - 在存储桶之间复制文件

aws-cloudformation - 检查cloudformation中step函数的状态

amazon-web-services - AWS 步骤函数 - 重用上一步的输出

amazon-web-services - AWS 步骤函数 : check for null

hadoop - 从 Internet 直接下载文件到我的 S3 存储桶

amazon-web-services - AWS Cognito不发送验证短信

mongodb - AWS 将数据从 MongoDB 迁移到 DynamoDB/S3/Redshift

amazon-web-services - AWS IAM 和 KMS 策略 'muddlement'

amazon-web-services - 在Lambda失败的情况下,AWS Fail Step Function仍会发送错误对象作为响应