Basic CLI commands (s3, ec2) in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using AWS CLI commands for S3 and EC2, it's important to understand how the number of commands or resources affects the total time taken.
We want to know how the time grows as we work with more buckets, objects, or instances.
Analyze the time complexity of the following AWS CLI commands sequence.
aws s3 ls
aws s3 cp file.txt s3://mybucket/
aws ec2 describe-instances
aws ec2 start-instances --instance-ids i-1234567890abcdef0
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
This sequence lists S3 buckets, uploads a file, lists EC2 instances, and starts then stops one instance.
Look at the commands that repeat or scale with input size.
- Primary operation: Listing or managing multiple resources (buckets, objects, instances).
- How many times: Each command runs once here, but listing commands scale with number of resources.
As the number of buckets, objects, or instances grows, listing commands take longer because they process more items.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 items processed in listing |
| 100 | About 100 items processed in listing |
| 1000 | About 1000 items processed in listing |
Pattern observation: Time grows roughly in direct proportion to the number of items listed.
Time Complexity: O(n)
This means the time to complete listing commands grows linearly with the number of resources.
[X] Wrong: "Running a single AWS CLI command always takes the same time regardless of resource count."
[OK] Correct: Some commands like listing depend on how many resources exist, so more resources mean more time.
Understanding how command time grows with resource count helps you plan and explain cloud operations clearly and confidently.
"What if we changed from listing all instances to filtering by tag? How would the time complexity change?"
Practice
Solution
Step 1: Understand the service for buckets
Amazon S3 stores buckets, so the command must useaws s3.Step 2: Identify the correct action to list buckets
Thelsaction lists buckets in S3. Soaws s3 lslists all buckets.Final Answer:
aws s3 ls -> Option DQuick Check:
List buckets = aws s3 ls [OK]
- Confusing EC2 commands with S3 commands
- Using 'list-buckets' which is not a valid CLI action
- Trying to list buckets with 'aws ec2' commands
i-1234567890abcdef0?Solution
Step 1: Identify the correct EC2 start command
The correct command to start instances isstart-instances(plural).Step 2: Use the correct option name for instance IDs
The option is--instance-ids(plural), not--instance-id.Final Answer:
aws ec2 start-instances --instance-ids i-1234567890abcdef0 -> Option AQuick Check:
Start instances = start-instances + --instance-ids [OK]
- Using singular 'start-instance' instead of 'start-instances'
- Using '--instance-id' instead of '--instance-ids'
- Mixing up EC2 and S3 commands
my-bucket contains two files file1.txt and file2.txt?
aws s3 ls s3://my-bucket/
Solution
Step 1: Understand the command purpose
aws s3 ls s3://my-bucket/lists objects inside the bucketmy-bucket.Step 2: Predict the output for files in the bucket
The command will show the file names and their sizes forfile1.txtandfile2.txt.Final Answer:
Lists the names and sizes of file1.txt and file2.txt -> Option AQuick Check:
List bucket contents = file names + sizes [OK]
- Thinking it lists buckets instead of files
- Expecting deletion or error output
- Confusing bucket name listing with object listing
aws ec2 describe-instances --instance-ids i-1234567890abcdef0. What is the likely cause?Solution
Step 1: Check command correctness
aws ec2 describe-instances --instance-idsis a valid command to get instance details.Step 2: Identify common error reasons
An error usually means the instance ID is wrong or the instance does not exist in your account or region.Final Answer:
The instance ID is incorrect or does not exist -> Option BQuick Check:
Invalid instance ID causes describe-instances error [OK]
- Using S3 commands for EC2 tasks
- Confusing describe with start commands
- Adding irrelevant options like --bucket
photo.jpg to an S3 bucket my-photos in a folder 2024/. Which command correctly does this?Solution
Step 1: Choose the correct AWS service and action
Copying files to S3 usesaws s3 cp. EC2 commands do not handle S3 files.Step 2: Use the correct source and destination syntax
Source is local filephoto.jpg, destination is S3 paths3://my-photos/2024/.Final Answer:
aws s3 cp photo.jpg s3://my-photos/2024/ -> Option CQuick Check:
Copy local file to S3 = aws s3 cp [OK]
- Using 'mv' which moves instead of copies
- Using 'sync' for single file copy
- Using EC2 commands for S3 file operations
