Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFormation stack resource dependency issues: API Gateway Deployment + UsagePlan

I'm working on a CloudFormation template to deploy API Gateway resources and having issues with the Deployment (AWS::ApiGateway::Deployment) and UsagePlan resources. It's sort of a chicken/egg situation.

In the UsagePlan I specify the ApiStage's configuration, which means that I need to have the Deployment resource created first. However, when I try to delete the stack the UsagePlan has a failure because the corresponding stage is still connected to the UsagePlan.

I have a DependsOn statement for the UsagePlan (UsagePlan depends on the deployment). This allows the stack to create without issue. But because of this DependsOn statement, it forces the UsagePlan to delete first on the delete action. This causes the error.

See pertinent code excerpt below.

Anyone have a solution for this issue? I can't be the first one to stumble on this!

Thanks!

"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"
  ]
}
like image 381
theoneandonly2 Avatar asked Sep 11 '26 08:09

theoneandonly2


1 Answers

The UsagePlan can be re-used across multiple APIs. So, ideally we would recommend you to have different CloudFormation stacks for each API and a different CloudFormation stack for UsagePlans. That way, you can delete an API without deleting the UsagePlan and without getting into this dependency issue.

like image 184
Balaji Avatar answered Sep 13 '26 07:09

Balaji