Key pairs for SSH access in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When creating key pairs for SSH access in AWS, it is important to understand how the time to create and manage these keys grows as you add more keys.
We want to know how the number of operations changes when we create many key pairs.
Analyze the time complexity of the following operation sequence.
// Create multiple key pairs for SSH access
for (let i = 0; i < n; i++) {
aws ec2 create-key-pair --key-name "my-key-" + i
}
This sequence creates n separate SSH key pairs in AWS, each with a unique name.
- Primary operation: The
create-key-pairAPI call to AWS EC2. - How many times: This call is made once for each key pair, so n times.
Each new key pair requires one API call, so the total number of calls grows directly with the number of keys.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of API calls increases one-for-one with the number of key pairs created.
Time Complexity: O(n)
This means the time to create key pairs grows linearly as you add more keys.
[X] Wrong: "Creating multiple key pairs can be done with a single API call, so time stays the same no matter how many keys."
[OK] Correct: Each key pair requires its own API call; AWS does not support batch creation of key pairs in one request.
Understanding how operations scale with input size helps you design efficient cloud workflows and shows you can think about resource management clearly.
"What if AWS allowed batch creation of key pairs in one API call? How would the time complexity change?"
Practice
Solution
Step 1: Understand SSH access
SSH uses keys to allow secure login without passwords.Step 2: Role of key pairs in AWS
A key pair provides a private key for the user and a public key for the server to verify identity.Final Answer:
To securely connect to a server without using a password -> Option BQuick Check:
Key pairs enable passwordless secure login [OK]
- Thinking key pairs store server data
- Confusing key pairs with backups
- Assuming key pairs monitor performance
MyKey and saves the private key to a file?Solution
Step 1: Identify correct AWS CLI syntax
The correct command usescreate-key-pairwith--key-nameand outputs the private key material.Step 2: Confirm output redirection
The private key is saved by redirecting the output to a file with> MyKey.pem.Final Answer:
aws ec2 create-key-pair --key-name MyKey --query 'KeyMaterial' --output text > MyKey.pem -> Option AQuick Check:
Correct AWS CLI syntax for key pair creation [OK]
- Using wrong command like generate-key-pair
- Omitting --query to extract key material
- Not redirecting output to save private key
MyKey. Which command will you use to connect to it if the instance's public IP is 54.12.34.56 and your private key file is MyKey.pem?Solution
Step 1: Understand SSH command syntax for key usage
The-ioption specifies the private key file for authentication.Step 2: Confirm correct order of arguments
The correct syntax isssh -i private_key user@host. ssh -i MyKey.pem ec2-user@54.12.34.56 matches this exactly.Final Answer:
ssh -i MyKey.pem ec2-user@54.12.34.56 -> Option DQuick Check:
SSH uses -i to specify private key file [OK]
- Using -key or -p instead of -i
- Placing -i after user@host
- Omitting the private key option
Solution
Step 1: Check SSH private key file permissions
SSH requires private key files to have strict permissions (e.g., 400). Too open permissions cause denial.Step 2: Understand other options
While stopped instances or no public IP prevent connection, the error message differs. Deleted key pairs do not affect existing instances.Final Answer:
The private key file has incorrect permissions (too open) -> Option AQuick Check:
Private key file permissions cause SSH denial [OK]
- Ignoring file permission errors
- Assuming instance state causes permission denied
- Confusing deleted key pairs with connection errors
OldKey. What is the best way to regain SSH access without stopping the instance?Solution
Step 1: Understand private key loss impact
Private keys cannot be recovered or downloaded again from AWS once lost.Step 2: Regain access without stopping instance
Use AWS Systems Manager or another user with access to add a new public key from a new key pair to the instance's authorized keys.Final Answer:
Create a new key pair, then update the instance's authorized keys by connecting through Systems Manager or another user -> Option CQuick Check:
Lost private key requires new key and authorized keys update [OK]
- Trying to download lost private key again
- Assuming new key pair with same name works
- Deleting instance unnecessarily
