CloudFormation 堆栈资源依赖性问题:API Gateway Deployment + UsagePlan

CloudFormation stack resource dependency issues: API Gateway Deployment + UsagePlan

我正在使用 CloudFormation 模板来部署 API 网关资源,但部署 (AWS::ApiGateway::Deployment) 和 UsagePlan 资源存在问题。这是一种 chicken/egg 的情况。

在 UsagePlan 中我指定了 ApiStage 的配置,这意味着我需要先创建 Deployment 资源。但是,当我尝试删除堆栈时,UsagePlan 失败,因为相应的阶段仍然连接到 UsagePlan。

我有一个针对 UsagePlan 的 DependsOn 语句(UsagePlan 取决于部署)。这使得堆栈可以毫无问题地创建。但是由于这个 DependsOn 语句,它强制 UsagePlan 在删除操作时先删除。这会导致错误。

请参阅下面的相关代码摘录。

谁有解决这个问题的方法?我不能是第一个偶然发现这个的人!

谢谢!

"UppRestApiDeployment": {
  "Type": "AWS::ApiGateway::Deployment",
  "Properties": {
    "Description": "Upp Rest API Deployment",
    "RestApiId": {
      "Ref": "UppRestApi"
    },
    "StageName": {
      "Ref": "StackIdentifier"
    },
    "StageDescription": {
      "CacheClusterEnabled": false,
      "CacheClusterSize": "0.5",
      "CacheDataEncrypted": false,
      "CacheTtlInSeconds": 10,
      "CachingEnabled": false,
      "DataTraceEnabled": false,
      "LoggingLevel": "ERROR",
      "MetricsEnabled": true,
      "StageName": {
        "Ref": "StackIdentifier"
      },
      "Description": {
        "Fn::Sub": "${StackIdentifier} Stage"
      },
      "ThrottlingBurstLimit": 2000,
      "ThrottlingRateLimit": 1000,
      "Variables": {
        "lambdaAlias": {
          "Ref": "StackIdentifier"
        }
      }
    }
  }
},
"UppApiUsagePlan": {
  "Type": "AWS::ApiGateway::UsagePlan",
  "Properties": {
    "ApiStages": [
      {
        "ApiId": {
          "Ref": "UppRestApi"
        },
        "Stage": {
          "Ref": "StackIdentifier"
        }
      }
    ],
    "Description": "Upp Rest API Usage Plan to Prevent Overage Charges",
    "Quota": {
      "Limit": 5000,
      "Period": "MONTH"
    },
    "Throttle": {
      "BurstLimit": 200,
      "RateLimit": 100
    },
    "UsagePlanName": {
        "Fn::Sub": "${StackIdentifier}_UppApiUsagePlan"
    }
  },
  "DependsOn": [
    "UppRestApiDeployment"
  ]
}

UsagePlan 可以 re-used 跨越多个 API。因此,理想情况下,我们建议您为每个 API 使用不同的 CloudFormation 堆栈,并为 UsagePlans 使用不同的 CloudFormation 堆栈。这样,您可以删除 API 而无需删除 UsagePlan,也不会陷入此依赖性问题。