0
0
AWScloud~5 mins

Nested stacks for modularity in AWS - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Nested stacks for modularity
O(n)
Understanding Time Complexity

When using nested stacks in AWS CloudFormation, it's important to understand how the time to deploy grows as you add more nested stacks.

We want to know how the number of nested stacks affects the total deployment time.

Scenario Under Consideration

Analyze the time complexity of deploying a CloudFormation template with nested stacks.


Resources:
  NetworkStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: network-template.yaml
  AppStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: app-template.yaml
      Parameters:
        VpcId: !GetAtt NetworkStack.Outputs.VpcId
  DatabaseStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: db-template.yaml
      Parameters:
        SubnetIds: !GetAtt NetworkStack.Outputs.SubnetIds
    

This sequence defines a parent stack that deploys three nested stacks for network, application, and database resources.

Identify Repeating Operations

Look at the main repeated actions during deployment:

  • Primary operation: Creating and updating each nested stack.
  • How many times: Once per nested stack defined in the parent template.
How Execution Grows With Input

As you add more nested stacks, the deployment time grows roughly in direct proportion to the number of nested stacks.

Input Size (n)Approx. Api Calls/Operations
33 nested stack creations
1010 nested stack creations
100100 nested stack creations

Pattern observation: Each additional nested stack adds one more create/update operation, so the total grows linearly.

Final Time Complexity

Time Complexity: O(n)

This means the deployment time increases in a straight line as you add more nested stacks.

Common Mistake

[X] Wrong: "Adding nested stacks doesn't affect deployment time much because they run in parallel."

[OK] Correct: While some operations may run in parallel, AWS CloudFormation often waits for dependencies, so nested stacks deploy mostly one after another, increasing total time linearly.

Interview Connect

Understanding how nested stacks affect deployment time helps you design modular templates that balance clarity and speed, a valuable skill in cloud architecture.

Self-Check

"What if nested stacks were deployed fully in parallel without waiting for dependencies? How would the time complexity change?"