Importing a Custom EC2 Key Pair in AWS: A Step-by-Step Guide

Published: (January 15, 2026 at 04:01 AM EST)
2 min read
Source: Dev.to

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:ImportKeyPair
    • ec2: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-keyprivate key (keep secure).
  • my-custom-key.pubpublic key (to import into AWS).

Import the Key Pair via the AWS Management Console

  1. Log in to the AWS Management Console and navigate to EC2 Dashboard.
  2. In the left‑hand menu under Network & Security, click Key Pairs.
  3. Click Import key pair.
  4. Provide a name (e.g., my-custom-key).
  5. Paste the contents of my-custom-key.pub into the Public key contents field.
  6. 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.

Back to Blog

Related posts

Read more »

Understand AWS IAM Identifiers

When working with AWS Security, one thing that often confuses beginners is IAM identifiers. You may have seen terms like ARN, UserID, RoleID, and FriendlyName....