Stack drift detection in AWS - Time & Space Complexity
Stack drift detection checks if your cloud setup has changed outside your control.
We want to know how the time to detect drift grows as your stack gets bigger.
Analyze the time complexity of the following operation sequence.
aws cloudformation detect-stack-drift --stack-name MyStack
aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id detectionId
aws cloudformation describe-stack-resource-drifts --stack-name MyStack
This sequence starts drift detection, checks its status, and then lists resource drifts in the stack.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Checking drift status and listing resource drifts for each resource in the stack.
- How many times: Once to start detection, then once to list all resource drifts (not once per resource).
As the number of resources in the stack grows, the number of drift checks grows too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 resource drift checks plus detection start and status calls |
| 100 | About 100 resource drift checks plus detection start and status calls |
| 1000 | About 1000 resource drift checks plus detection start and status calls |
Pattern observation: The number of operations grows roughly in direct proportion to the number of resources.
Time Complexity: O(n)
This means the time to detect drift grows linearly with the number of resources in your stack.
[X] Wrong: "Drift detection time stays the same no matter how many resources are in the stack."
[OK] Correct: Each resource must be checked, so more resources mean more work and longer time.
Understanding how drift detection scales helps you design stacks that stay manageable and predictable.
"What if drift detection only checked changed resources instead of all resources? How would the time complexity change?"
Practice
What is the main purpose of stack drift detection in AWS CloudFormation?
Solution
Step 1: Understand stack drift detection concept
Stack drift detection checks if resources in a stack have been changed manually or outside CloudFormation management.Step 2: Identify the main purpose
The main goal is to find these manual or unexpected changes to keep infrastructure consistent.Final Answer:
To find manual changes made outside of CloudFormation templates -> Option AQuick Check:
Stack drift detection = find manual changes [OK]
- Thinking it fixes errors automatically
- Confusing it with stack creation
- Assuming it monitors network traffic
Which AWS CLI command starts drift detection for a CloudFormation stack named MyAppStack?
Solution
Step 1: Recall AWS CLI syntax for drift detection
The correct command to start drift detection isdetect-stack-driftwith the--stack-nameoption.Step 2: Match the command with the stack name
aws cloudformation detect-stack-drift --stack-name MyAppStack uses the exact syntax and correct parameter for the stack name.Final Answer:
aws cloudformation detect-stack-drift --stack-name MyAppStack -> Option BQuick Check:
Detect drift command = detect-stack-drift [OK]
- Using incorrect command verbs like start or begin
- Wrong parameter names like --stack or --name
- Mixing up command order or syntax
After running aws cloudformation detect-stack-drift --stack-name MyStack, which command shows the drift detection status and results?
Solution
Step 1: Identify the command to check drift detection status
The AWS CLI command to check drift detection status isdescribe-stack-drift-detection-status.Step 2: Confirm the correct parameter usage
It requires the--stack-drift-detection-idparameter to specify the detection ID, not--stack-name.Final Answer:
aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id <detection-id> -> Option DQuick Check:
Check drift status = describe-stack-drift-detection-status [OK]
- Using non-existent commands like get-stack-drift-status
- Confusing list commands with status commands
- Wrong parameter names
You ran aws cloudformation detect-stack-drift --stack-name TestStack but got an error: Stack with id TestStack does not exist. What is the most likely cause?
Solution
Step 1: Analyze the error message
The error says the stack with the given name does not exist, indicating a wrong name or missing stack.Step 2: Consider other causes
While region or CLI version issues can cause errors, this specific message points to a missing or misspelled stack name.Final Answer:
The stack name is misspelled or the stack does not exist -> Option AQuick Check:
Error about stack id means wrong or missing stack [OK]
- Assuming region parameter is always required
- Blaming CLI version without checking stack
- Ignoring the exact error message
You want to ensure your CloudFormation stack stays consistent with its template. Which approach best uses stack drift detection to maintain this?
A) Run drift detection regularly and update stack if drift is detected.B) Disable drift detection and manually check resources.C) Use drift detection only after stack deletion.D) Ignore drift detection and rely on CloudWatch alarms.
Solution
Step 1: Understand drift detection best practice
Regularly running drift detection helps find manual changes early and keeps the stack consistent.Step 2: Evaluate options
Run drift detection regularly and update stack if drift is detected uses drift detection proactively. Options B, C, and D either ignore drift detection or use it incorrectly.Final Answer:
Run drift detection regularly and update stack if drift is detected -> Option CQuick Check:
Regular drift detection = best practice [OK]
- Ignoring drift detection completely
- Checking drift only after deletion
- Relying solely on CloudWatch for drift
Ready to prove it?
Pass the test and this pattern moves unknown
