Instance states (running, stopped, terminated) in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When managing cloud instances, it is important to understand how the time to check or change their states grows as you handle more instances.
We want to know how the number of operations changes when we work with many instances in different states.
Analyze the time complexity of the following operation sequence.
# List all instances
aws ec2 describe-instances
# For each instance, check its state
for instance in instances:
state = instance.state.name
if state == 'stopped':
aws ec2 start-instances --instance-ids instance.id
This sequence lists all instances, checks their states, and starts those that are stopped.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Checking each instance's state and starting stopped instances.
- How many times: Once per instance in the list.
As the number of instances grows, the number of state checks and possible start commands grows too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 state checks, up to 10 start commands |
| 100 | About 100 state checks, up to 100 start commands |
| 1000 | About 1000 state checks, up to 1000 start commands |
Pattern observation: The number of operations grows directly with the number of instances.
Time Complexity: O(n)
This means the time to check and start instances grows in a straight line as you add more instances.
[X] Wrong: "Checking all instances' states takes the same time no matter how many instances there are."
[OK] Correct: Each instance requires a separate check, so more instances mean more checks and more time.
Understanding how operations grow with the number of instances helps you design efficient cloud management scripts and shows you can think about scaling in real projects.
"What if we batch start stopped instances instead of starting them one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand EC2 instance states
Instances can be in states like running, stopped, or terminated, indicating their power and availability status.Step 2: Identify the state meaning 'powered on'
The 'running' state means the instance is powered on and ready to use.Final Answer:
Running -> Option AQuick Check:
Running = Powered On [OK]
- Confusing stopped with running
- Thinking terminated means running
- Mixing pending with running
i-1234567890abcdef0?Solution
Step 1: Identify command to stop instance
The command to stop an instance isstop-instances.Step 2: Match instance ID usage
The command must include--instance-ids i-1234567890abcdef0to specify the instance.Final Answer:
aws ec2 stop-instances --instance-ids i-1234567890abcdef0 -> Option DQuick Check:
Stop instance = stop-instances command [OK]
- Using terminate instead of stop
- Using start instead of stop
- Using reboot instead of stop
aws ec2 terminate-instances --instance-ids i-0abcdef1234567890
Solution
Step 1: Understand the terminate-instances command
This command deletes the instance permanently, changing its state to terminated.Step 2: Identify the resulting state
After termination, the instance cannot be started again and is marked as terminated.Final Answer:
Terminated -> Option CQuick Check:
Terminate command = Terminated state [OK]
- Thinking terminated means stopped
- Confusing terminate with stop
- Expecting instance to restart automatically
Solution
Step 1: Analyze error meaning
If AWS says the instance does not exist, it usually means it was deleted or terminated.Step 2: Match instance state to error
Terminated instances are removed and cannot be started again, causing this error.Final Answer:
The instance is terminated -> Option AQuick Check:
Instance not found = Terminated state [OK]
- Assuming stopped means instance missing
- Confusing running with missing
- Ignoring termination possibility
Solution
Step 1: Understand cost and data retention
Stopping an instance powers it off but keeps the attached storage intact, preserving data.Step 2: Compare with termination
Terminating deletes the instance and storage, losing data and cannot be restarted.Step 3: Choose correct state for saving costs and keeping data
Stopping is the correct choice to save costs while keeping data safe.Final Answer:
Stop, because it powers off the instance but keeps data on the disk -> Option BQuick Check:
Stop = Save cost + keep data [OK]
- Choosing terminate and losing data
- Thinking running saves cost
- Confusing pending with stop
