AWS Step Functions - 如何等待 cloudformation 完成?

问题描述 投票:0回答:1

在 AWS Step function create 上工作,我有以下步骤:

  1. ...
  2. 调用 Lambda 来运行 AWS CloudFormation 堆栈创建
  3. 调用第二个 Lambda 来运行 AWS CloudFormation 堆栈创建
  4. ...

我的问题是 - 我如何才能等到 CloudFormation 堆栈创建完成后,才能在步骤 3 中的 Lambda 执行另一个 Lambda 之前?

aws-step-functions
1个回答
0
投票

有几种方法可以做到这一点,但最好的解决方案可能是在Step Functions状态机中使用轮询循环。使用 AWS SDK 服务集成,您也许能够避免仅仅为了调用 CloudFormation API 而维护 Lambda 函数。请参阅下面的示例,其中我生成一个超级简单的模板(仅创建一个 SQS 队列),然后使用轮询循环来监视完成情况。

您可以将模板存储在 S3 中并使用

arn:aws:states:::aws-sdk:s3:getObject
在运行时加载它,而不是将模板直接烘焙到状态机定义中。

此处需要记住的一件事是 Step Functions 的 256 kB 有效负载大小限制。如果您的模板大小预计接近该大小,那么这将是使用 Lambda 函数来调用 CreateStack 的原因(尽管您可以让它返回 stack_id 并继续执行其余部分)。

An example of creating a CloudFormation stack and waiting for completion.

{
  "Comment": "An example of creating a CloudFormation stack and waiting for completion.",
  "StartAt": "Generate Template",
  "States": {
    "Generate Template": {
      "Comment": "Placeholder for a task state which starts a job. Replace with an API action.",
      "Type": "Pass",
      "Next": "Create Stack",
      "Result": {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Resources": {
          "SQSQueue": {
            "Type": "AWS::SQS::Queue"
          }
        }
      },
      "ResultPath": "$.template_body"
    },
    "Create Stack": {
      "Type": "Task",
      "Parameters": {
        "StackName.$": "States.Format('TestStack-{}',States.UUID())",
        "TemplateBody.$": "States.JsonToString($.template_body)"
      },
      "Resource": "arn:aws:states:::aws-sdk:cloudformation:createStack",
      "Next": "Wait 15 Seconds",
      "ResultPath": "$.stack_name",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "BackoffRate": 2,
          "IntervalSeconds": 2,
          "MaxAttempts": 3,
          "JitterStrategy": "FULL"
        }
      ]
    },
    "Wait 15 Seconds": {
      "Type": "Wait",
      "Next": "Describe Stack",
      "Seconds": 15
    },
    "Describe Stack": {
      "Type": "Task",
      "Parameters": {
        "StackName.$": "$.stack_name.StackId"
      },
      "Resource": "arn:aws:states:::aws-sdk:cloudformation:describeStacks",
      "Next": "Stack Creation Complete?",
      "ResultSelector": {
        "Status.$": "$.Stacks[0].StackStatus"
      },
      "ResultPath": "$.stack_status",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "BackoffRate": 4,
          "IntervalSeconds": 2,
          "MaxAttempts": 5,
          "JitterStrategy": "FULL"
        }
      ]
    },
    "Stack Creation Complete?": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.stack_status.Status",
          "StringEquals": "CREATE_FAILED",
          "Next": "Stack Creation Failed"
        },
        {
          "Variable": "$.stack_status.Status",
          "StringEquals": "CREATE_COMPLETE",
          "Next": "Stack Creation Successful"
        },
        {
          "Variable": "$.stack_status.Status",
          "StringEquals": "CREATE_IN_PROGRESS",
          "Next": "Wait 15 Seconds"
        }
      ],
      "Default": "Stack Creation Result Unknown"
    },
    "Stack Creation Result Unknown": {
      "Type": "Fail",
      "Error": "Unexpected Completion",
      "CausePath": "$.stack_status.Status"
    },
    "Stack Creation Successful": {
      "Type": "Succeed"
    },
    "Stack Creation Failed": {
      "Type": "Fail",
      "Error": "Stack Creation Failed"
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.