CLI scripting basics in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
aws s3 ls do?Solution
Step 1: Understand the command structure
The command usesaws s3which relates to the S3 service, andlswhich means list.Step 2: Interpret the command action
Listing in S3 context means showing all buckets or objects. Without extra parameters, it lists all buckets.Final Answer:
Lists all S3 buckets in your AWS account -> Option CQuick Check:
Commandaws s3 lslists buckets [OK]
- Confusing 'ls' with 'rm' (delete)
- Thinking it creates resources
- Assuming it uploads files
Solution
Step 1: Identify the correct AWS CLI command for EC2 instance creation
The official command to create EC2 instances isrun-instances.Step 2: Check the syntax correctness
aws ec2 run-instances --image-id ami-12345678 --count 1 usesaws ec2 run-instances --image-id ami-12345678 --count 1, which is the correct syntax to launch one instance.Final Answer:
aws ec2 run-instances --image-id ami-12345678 --count 1 -> Option AQuick Check:
EC2 instance creation uses 'run-instances' command [OK]
- Using 'create-instance' which is invalid
- Confusing 'start-instance' with creation
- Using 'launch-instance' which is not a valid command
aws s3api list-buckets --query 'Buckets[].Name' --output json
Solution
Step 1: Understand the command components
The command useslist-bucketsto get all buckets, with a query to extract only the bucket names.Step 2: Analyze the output format
The--output jsonoption formats the result as JSON, so the output is a JSON array of bucket names.Final Answer:
A JSON array of bucket names -> Option AQuick Check:
Query filters names, output json formats as JSON array [OK]
- Expecting plain text instead of JSON
- Confusing bucket names with creation dates
- Assuming syntax error due to query
aws s3 rb s3://my-bucket
But it fails with an error. What is the most likely cause?
Solution
Step 1: Understand the command purpose
aws s3 rbremoves (deletes) an S3 bucket, but only if it is empty.Step 2: Identify the common error cause
If the bucket contains files, the command fails. You must delete all objects first.Final Answer:
The bucket is not empty -> Option BQuick Check:
Bucket must be empty before removal [OK]
- Trying to delete non-empty bucket directly
- Confusing 'rm' (remove objects) with 'rb' (remove bucket)
- Assuming AWS CLI is not installed without checking
Solution
Step 1: Identify the correct command to list EC2 instances
The correct command to list instances isdescribe-instances.Step 2: Identify the correct command to stop instances
To stop instances, usestop-instanceswith the instance IDs.Step 3: Combine commands in script logic
First list instances to get IDs, then stop them using those IDs.Final Answer:
Useaws ec2 describe-instancesto list, thenaws ec2 stop-instances --instance-idsto stop -> Option DQuick Check:
List with describe, stop with stop-instances [OK]
- Using non-existent commands like 'list-instances'
- Confusing 'terminate' with 'stop' (termination deletes)
- Using wrong commands that cause errors
