Skip to content

CloudFormation Resource Limit Issue Despite Using Nested Stacks

0

We recently encountered an issue while deploying our serverless Lambda API Gateway—we were exceeding the CloudFormation resource limit of 500. To work around this, we implemented nested stacks to break up our resources. However, the issue still persists. For context the Backend then gets deployed as a stage via the pipeline.

Could someone please review the structure below and let me know if there’s anything wrong?

class Backend(cdk.Stack):
    def __init__(self, scope: cdk.App, construct_id: str, deploy_env, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        # Initialize shared resources like the REST API, S3 bucket, and Lambda layer.
        self.api = API(...) 
        self.shared = Shared(...) 
        self._lambda = Lambda(...)

        # Create nested stacks for Lambda endpoints.
        self.endpoints1_stack = Endpoints1NestedStack(self, "Endpoints1",
                                                      api=self.api,
                                                      shared=self.shared,
                                                      _lambda=self._lambda,
                                                      deploy_env=deploy_env,
                                                      **kwargs)
        self.endpoints2_stack = Endpoints2NestedStack(self, "Endpoints2",
                                                      api=self.api,
                                                      shared=self.shared,
                                                      _lambda=self._lambda,
                                                      deploy_env=deploy_env,
                                                      **kwargs)

class Endpoints1NestedStack(NestedStack):
    def __init__(self, scope: cdk.Stack, construct_id: str, api, shared, _lambda, deploy_env, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        # Define the first set of endpoints.
        self.endpoints = Endpoints(...)

class Endpoints2NestedStack(NestedStack):
    def __init__(self, scope: cdk.Stack, construct_id: str, api, shared, _lambda, deploy_env, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        # Define the second set of endpoints.
        self.endpoints = Endpoints2(...)
  • Try running 'cdk synth'. It will show you the generated cloudformation stack. With this, you could check how many resources are being created and whether the nested stacks are actually getting used.

  • Thanks @SHAJAM, while running cdk synth I do get the resource error. But this error hasn't been previously occurring.

  • In that case, I think it is creating 500+ resources. Maybe, it is adding more resources than you had previously. How many resources do you have in the previously deployed stack.

1 Answer
0

The structure you've provided for using nested stacks to break up your resources is generally correct, but there are a few considerations that might explain why you're still encountering the CloudFormation resource limit issue:

  1. Resource Distribution: Even though you're using nested stacks, it's important to ensure that the resources are actually distributed across these stacks. If the majority of your resources are still in the main stack (Backend), you might still hit the limit. Make sure you're moving a significant portion of your resources into the nested stacks.

  2. Nested Stack Limit: While nested stacks can help overcome the 500 resource limit per stack, there's still a limit of 2500 resources that can be created, updated, or deleted in a single nested stack operation. If your total resources across all stacks exceed this, you might still encounter issues.

  3. Shared Resources: Your main stack (Backend) is creating shared resources like the API, S3 bucket, and Lambda layer. Depending on how many resources these constructs create, you might be using up a significant portion of your resource quota in the main stack.

  4. Resource Creation in Nested Stacks: Ensure that your Endpoints1 and Endpoints2 classes are actually creating resources within the nested stacks, rather than just referencing resources from the main stack.

  5. Multiple Levels of Nesting: If you have a large number of resources, you might need to consider multiple levels of nesting or further breaking down your stacks.

  6. Cross-Stack References: Be cautious with cross-stack references. While they're useful, excessive use can lead to complex dependencies and potential deployment issues.

To further optimize your structure:

  1. Consider moving more resources into nested stacks, including potentially the shared resources.
  2. If possible, break down your endpoints into more granular nested stacks.
  3. Use the AWS CDK's escape hatch to access the underlying CloudFormation template and count the resources in each stack. This can help you identify which stack is approaching the limit.
  4. Monitor the size of your processed templates. If they're approaching the template size limit (51,200 bytes for direct requests or 1 MB for S3-stored templates), you may need to further split your stacks.

If after these optimizations you're still hitting limits, you might need to consider splitting your application into multiple independent stacks rather than using nested stacks within a single main stack.
Sources
aws-cdk-lib.aws_apigateway module · AWS CDK
Troubleshoot the processed template - AWS CloudFormation
Understand CloudFormation quotas - AWS CloudFormation

answered a year ago

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.