Nested stacks for modularity in AWS - Time & Space 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.
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.
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.
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 |
|---|---|
| 3 | 3 nested stack creations |
| 10 | 10 nested stack creations |
| 100 | 100 nested stack creations |
Pattern observation: Each additional nested stack adds one more create/update operation, so the total grows linearly.
Time Complexity: O(n)
This means the deployment time increases in a straight line as you add more nested stacks.
[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.
Understanding how nested stacks affect deployment time helps you design modular templates that balance clarity and speed, a valuable skill in cloud architecture.
"What if nested stacks were deployed fully in parallel without waiting for dependencies? How would the time complexity change?"