Updating and deleting stacks in AWS - Time & Space Complexity
When updating or deleting cloud stacks, it is important to understand how the time taken grows as the stack size changes.
We want to know how the number of resources affects the time to update or delete a stack.
Analyze the time complexity of updating and deleting a stack with multiple resources.
# Update stack with new template
aws cloudformation update-stack --stack-name MyStack --template-body file://template.yaml
# Delete stack
aws cloudformation delete-stack --stack-name MyStack
This sequence updates a stack by applying a new template and then deletes the stack.
Look at what happens repeatedly during update and delete.
- Primary operation: Processing each resource in the stack during update or delete.
- How many times: Once per resource in the stack.
As the number of resources grows, the time to update or delete grows roughly in direct proportion.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 resource operations |
| 100 | About 100 resource operations |
| 1000 | About 1000 resource operations |
Pattern observation: The number of operations grows linearly with the number of resources.
Time Complexity: O(n)
This means the time to update or delete a stack grows directly with the number of resources it contains.
[X] Wrong: "Updating or deleting a stack takes the same time no matter how many resources it has."
[OK] Correct: Each resource must be processed, so more resources mean more work and more time.
Understanding how stack operations scale helps you design efficient cloud deployments and shows you grasp real-world cloud management challenges.
"What if the stack uses nested stacks? How would that affect the time complexity of updating or deleting?"
Practice
update-stack command in AWS CloudFormation?Solution
Step 1: Understand the role of update-stack
Theupdate-stackcommand is used to modify resources that are already managed by an existing CloudFormation stack.Step 2: Differentiate from other commands
Unlikedelete-stackwhich removes resources,update-stackchanges them safely without deleting the stack.Final Answer:
To safely change existing cloud resources managed by a stack -> Option BQuick Check:
update-stack = change resources safely [OK]
- Confusing update-stack with delete-stack
- Thinking update-stack creates new stacks
- Assuming update-stack lists stacks
MyAppStack?Solution
Step 1: Identify the correct delete command
The correct AWS CLI command to delete a stack isdelete-stackwith the parameter--stack-name.Step 2: Verify syntax correctness
aws cloudformation delete-stack --stack-name MyAppStack uses the exact syntax:aws cloudformation delete-stack --stack-name MyAppStack, which is valid and deployable.Final Answer:
aws cloudformation delete-stack --stack-name MyAppStack -> Option AQuick Check:
delete-stack + --stack-name = correct syntax [OK]
- Using update-stack instead of delete-stack
- Wrong parameter names like --name or --stack
- Using non-existent commands like remove-stack or destroy-stack
aws cloudformation update-stack --stack-name TestStack --template-body file://template.json aws cloudformation describe-stacks --stack-name TestStack
What is the expected behavior after running these commands?
Solution
Step 1: Understand update-stack effect
Theupdate-stackcommand modifies the existing stack named TestStack using the provided template.Step 2: Describe-stacks shows current stack status
Thedescribe-stackscommand retrieves details about TestStack, including its updated status.Final Answer:
The stack TestStack is updated with the new template, and its status is shown -> Option AQuick Check:
update-stack changes stack + describe-stacks shows status [OK]
- Thinking update-stack deletes the stack
- Assuming describe-stacks creates a new stack
- Believing update-stack and describe-stacks cannot be used sequentially
aws cloudformation update-stack --stack-name ProdStack --template-body file://newtemplate.json but get an error saying the stack is in UPDATE_IN_PROGRESS state. What should you do to fix this?Solution
Step 1: Understand stack states during update
When a stack is inUPDATE_IN_PROGRESS, it means an update is already running and must complete before another update starts.Step 2: Choose the correct action
Waiting for the current update to finish avoids conflicts and errors. Deleting or renaming the stack is unnecessary and may cause data loss or confusion.Final Answer:
Wait for the current update to finish before running update-stack again -> Option DQuick Check:
UPDATE_IN_PROGRESS means wait before next update [OK]
- Trying to delete stack during update
- Renaming stack instead of waiting
- Ignoring error and retrying immediately
update-stack to achieve this?Solution
Step 1: Understand rollback behavior in update-stack
By default, CloudFormation rolls back stack updates on failure. The--disable-rollbackoption disables this behavior.Step 2: Identify the correct option to keep rollback enabled
To ensure rollback happens automatically on failure, you should NOT disable rollback. Therefore, do not use--disable-rollback. The default behavior is rollback enabled, so omit this option or explicitly avoid disabling rollback.Step 3: Analyze options
--rollback-configurationis for configuring alarms,--enable-rollbackand--no-rollbackare not valid AWS CLI options.Final Answer:
--disable-rollback disables rollback; omit it to keep rollback enabled -> Option CQuick Check:
Omit --disable-rollback to keep rollback on failure [OK]
- Using non-existent --enable-rollback option
- Confusing disable-rollback disables rollback
- Assuming rollback is off by default
Ready to prove it?
Pass the test and this pattern moves unknown
