EC2 pricing models (on-demand, reserved, spot) in AWS - Time & Space Complexity
We want to understand how the cost and usage of EC2 instances grow when using different pricing models.
How does the number of instances and their usage affect the overall cost and resource allocation?
Analyze the time complexity of launching EC2 instances with different pricing models.
# Launch EC2 instances with spot and on-demand pricing models; purchase Reserved Instance
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t3.micro --instance-market-options MarketType=spot
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t3.micro
aws ec2 purchase-reserved-instances-offering --reserved-instances-offering-id abcdef12-3456-7890-abcd-ef1234567890 --instance-count 1
This sequence launches one spot instance, one on-demand instance, and purchases one Reserved Instance.
Look at the main actions that happen repeatedly when scaling instances.
- Primary operation: Launching EC2 instances (run-instances API call)
- How many times: Once per instance requested
- Other operations: Purchasing reserved instances (one-time per reservation)
When you increase the number of instances, the number of API calls to launch them grows directly with that number.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 run-instances calls + reserved purchases if any |
| 100 | 100 run-instances calls + reserved purchases if any |
| 1000 | 1000 run-instances calls + reserved purchases if any |
Pattern observation: The number of instance launches grows linearly with the number of instances requested.
Time Complexity: O(n)
This means the effort to launch instances grows directly in proportion to how many you want to run.
[X] Wrong: "Launching more spot instances costs less time and effort because they are cheaper."
[OK] Correct: The pricing affects cost, but the number of API calls and provisioning steps still grows with the number of instances regardless of pricing model.
Understanding how resource requests scale helps you design systems that handle growth smoothly and predict costs clearly.
"What if we switched from on-demand to spot instances for all launches? How would the time complexity of launching instances change?"