Importing a Custom EC2 Key Pair in AWS: A Step-by-Step Guide
Source: Dev.to
Prerequisites
- Custom SSH key pair – generate using
ssh-keygen, OpenSSL, or another tool.- Private key stays on your local machine for authentication.
- Public key will be imported into AWS.
- AWS CLI or AWS Management Console – this guide covers both methods.
- IAM permissions – ensure your user/role has:
ec2:ImportKeyPairec2:DescribeKeyPairs
Generate an SSH Key Pair (using ssh-keygen)
ssh-keygen -t rsa -b 2048 -m PEM -C "your-email@example.com" -f my-custom-key
-t rsa– RSA algorithm.-b 2048– 2048‑bit key size.-m PEM– PEM format.-f my-custom-key– output file name.-C "your-email@example.com"– comment for identification.
This creates two files:
my-custom-key– private key (keep secure).my-custom-key.pub– public key (to import into AWS).
Import the Key Pair via the AWS Management Console
- Log in to the AWS Management Console and navigate to EC2 Dashboard.
- In the left‑hand menu under Network & Security, click Key Pairs.
- Click Import key pair.
- Provide a name (e.g.,
my-custom-key). - Paste the contents of
my-custom-key.pubinto the Public key contents field. - Click Import key pair.
Import the Key Pair via AWS CLI
If the AWS CLI is not installed, follow the official installation guide to set it up.
aws ec2 import-key-pair \
--key-name "my-custom-key" \
--public-key-material fileb://my-custom-key.pub
--key-name– name of the key pair in AWS.--public-key-material– path to the public key file.
Verify the Imported Key Pair
aws ec2 describe-key-pairs --key-name "my-custom-key"
If the command returns details about the key pair, the import was successful.
Best Practices
- Keep private keys secure – store them in a protected location and never share.
- Key pair limits – default soft limit is 5,000 key pairs per region; contact AWS Support for higher limits.
- Backup – regularly back up private keys securely to avoid loss of access.
Importing a custom EC2 key pair gives you full control over SSH access to your instances while complying with organizational security policies.
For more details, refer to the official AWS Key Pair Documentation.