CLI scripting basics in AWS - Time & Space Complexity
When using AWS CLI scripts, it is important to understand how the time to complete tasks grows as you add more commands or resources.
We want to know how the number of AWS commands affects the total time taken.
Analyze the time complexity of this AWS CLI script sequence.
aws s3 mb s3://my-bucket
for i in $(seq 1 100); do
aws s3 cp file${i}.txt s3://my-bucket/
aws ec2 start-instances --instance-ids i-1234567890abcdef0
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
done
This script creates a bucket, then uploads 100 files and starts and stops an EC2 instance 100 times.
Look at the repeated AWS CLI commands in the script.
- Primary operation: Uploading files and starting/stopping instances.
- How many times: Each upload and start/stop command runs 100 times.
As the number of files or instance operations increases, the total commands grow proportionally.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | ~30 (10 uploads + 10 starts + 10 stops) |
| 100 | ~300 (100 uploads + 100 starts + 100 stops) |
| 1000 | ~3000 (1000 uploads + 1000 starts + 1000 stops) |
Pattern observation: The total commands increase directly with the number of files or operations.
Time Complexity: O(n)
This means the total time grows in direct proportion to the number of repeated commands.
[X] Wrong: "Adding more files won't affect total time much because commands run fast."
[OK] Correct: Each command takes time, so more commands add up linearly, increasing total time noticeably.
Understanding how script length affects execution time helps you write efficient automation and shows you can think about scaling tasks.
"What if we changed the script to upload files in parallel instead of sequentially? How would the time complexity change?"