CDK - How to remove API gateway methods with deploy stacks ?

1

Hello,

As stated in the cdk documentation here (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway-readme.html#breaking-up-methods-and-resources-across-stacks), it is fairly common for REST APIs with a large number of Resources and Methods to hit the CloudFormation limit of 500 resources per stack.

We followed the documentation and implemented NestedStacks as well as a DeployStask. Everything seems to work as expected except that we did not find how to properly remove an API Gateway method.

The deployment.node.addDependency(method) pushes all new or updated methods but we don't know how to deal with deleted methods.

I expected that cdk would try to compute the diff between the methods in the new deployments compared with the last deployment but it does not seem to be the case.

The only workaround we found is to manually redeploy the whole stage from cli or from the console.

How can we remove a method with a deploy stack using cdk only ?

Thanks for your help, Best Regards, Bruno

1 Answer
0

As you already mentioned that for REST APIs with a large number of Resources and Methods which hits the CloudFormation limit of 500 resources per stack, it is suggested to implement nested stacks in order to divide the resources among the multiple child stacks

Now, in order to get the new or updated methods deployed in additon to the deletion of the older or deprecated methods, it is required to update the definition of the cdk application to allow the deletion of the older versions of method deleted during cleanup of the stack resources once the new resources have been deployed successfully.

However, in case these methods have been deleted manually as an out of band change to the cdk application, it is suggested to update the definition of the resource to avoid the drift among the resources. With this, we can ensure that cdk will try to compare the difference between the methods in the new deployments compared with the last deployment.

profile pictureAWS
SUPPORT ENGINEER
answered 2 years ago
  • Thank you for your reply.

    I am not sure to understand exactly what you mean by "it is required to update the definition of the cdk application" I understand the remove or deprecated methods can be removed during the cleanup phase.

    Let's take the exemple from the documentation with books and pets API. Let's say I want to remove the books API. I would remove the code regarding BooksStack and I would write new DeployStack(this, { restApiId: restApi.restApiId, methods: petsStack.methods, });

    Then I redeploy. The books method are removed from the resource tab in the aws gateway console but not from the stage tab.

    What do I need to do in addition to update the definition of the cdk application ?

  • +1 to Bruno's question - the proposed answer to this is not explicit enough. Please elaborate.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions