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?"
Practice
nested stacks in AWS CloudFormation?Solution
Step 1: Understand the purpose of nested stacks
Nested stacks allow splitting a big CloudFormation template into smaller templates for easier management.Step 2: Compare options with this purpose
Only They break large templates into smaller, reusable parts. describes breaking large templates into reusable parts, which matches nested stacks' main benefit.Final Answer:
They break large templates into smaller, reusable parts. -> Option CQuick Check:
Nested stacks = modular templates [OK]
- Confusing nested stacks with scaling features
- Assuming nested stacks handle encryption automatically
- Mixing nested stacks with IAM role functions
Solution
Step 1: Identify nested stack resource type
The resource type for nested stacks isAWS::CloudFormation::Stack.Step 2: Check the properties for nested stack
Nested stacks require aTemplateURLproperty pointing to the child template location.Final Answer:
"Type": "AWS::CloudFormation::Stack", "Properties": {"TemplateURL": "https://s3.amazonaws.com/mybucket/mystack.yaml"} -> Option DQuick Check:
Nested stack resource = AWS::CloudFormation::Stack [OK]
- Using S3 bucket or EC2 instance types for nested stacks
- Omitting TemplateURL property
- Confusing Lambda function syntax with nested stacks
VpcId to a nested stack, what will happen if the nested stack template does NOT declare VpcId as a parameter?Solution
Step 1: Understand parameter passing in nested stacks
Parameters passed from parent to nested stack must be declared in the nested stack template.Step 2: Consequence of missing parameter declaration
If the nested stack does not declare the parameter, CloudFormation cannot map the value, causing deployment failure.Final Answer:
The nested stack deployment will fail with a parameter validation error. -> Option AQuick Check:
Missing nested stack parameter = deployment error [OK]
- Assuming parameters are optional in nested stacks
- Thinking parent stack auto-creates missing parameters
- Believing nested stacks use default values without declaration
Solution
Step 1: Identify cause of failure
The error is due to an incorrect TemplateURL in the parent stack referencing the nested stack template.Step 2: Correct the TemplateURL
Fixing the TemplateURL in the parent stack template ensures CloudFormation can find and deploy the nested stack correctly.Final Answer:
Update the parent stack template to use the correct TemplateURL for the nested stack. -> Option BQuick Check:
Fix TemplateURL in parent stack to fix nested stack update [OK]
- Deleting entire parent stack unnecessarily
- Changing resource type incorrectly
- Removing nested stack resource instead of fixing URL
VpcId and a subnet stack needs this VpcId as input. How should you connect these stacks to ensure modularity and correct parameter passing?Solution
Step 1: Use Outputs to share data from VPC stack
The VPC nested stack should declare an Output forVpcIdto share it externally.Step 2: Pass Output as Parameter to subnet stack
The parent stack template passes the VPC stack'sVpcIdOutput as a Parameter to the subnet nested stack, maintaining modularity.Final Answer:
Use Outputs in the VPC stack and pass them as Parameters to the subnet nested stack in the parent template. -> Option AQuick Check:
Outputs to Parameters connect nested stacks modularly [OK]
- Hardcoding values breaking modularity
- Deploying stacks separately without parameter passing
- Combining all resources in one big template losing modularity
Ready to prove it?
Pass the test and this pattern moves unknown
