3
votes

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"
  ]
}
1
You contradict yourself a bit. If you include a code snippet of your Deployment and UsagePlan resources (Stage too if created as a separate resource), I'm sure we'll be able to sort it out for you.lebryant
Thanks for taking a look. I added the excerpt of the script.theoneandonly2

1 Answers

3
votes

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.