AWS
EC2InvalidKeyPairNotFound - EC2 Invalid Key Pair Not Found
Getting an **EC2InvalidKeyPairNotFound** error means the EC2 key pair you specified doesn't exist in the current region—the key pair might be in a different region, was deleted, or the name is misspelled. This client-side error (4xx) happens when AWS validates EC2 key pair existence. Most common when key pair names don't exist, but also appears when key pairs are in different regions, key pairs have been deleted, incorrect key pair name formats are used, or key pair name typos occur.
#Common Causes
- →Identity: IAM policy allows EC2 launch but key pair doesn't exist. Service Control Policy (SCP) restricts key pair access.
- →Network: VPC endpoint EC2 key pair restrictions. Cross-region key pair access.
- →Limits: Key pair name does not exist. Key pair in different region. Key pair has been deleted. Incorrect key pair name format. Key pair name typo.
✓Solutions
- 1Step 1: Diagnose - List all key pairs in region: aws ec2 describe-key-pairs --region REGION --query 'KeyPairs[*].[KeyName,KeyPairId]' --output table. Check if key pair name is in the list.
- 2Step 2: Diagnose - Verify key pair in specific region: aws ec2 describe-key-pairs --key-names KEY_NAME --region REGION --query 'KeyPairs[0].[KeyName,KeyPairId]' --output table. Verify region is correct.
- 3Step 3: Diagnose - Search key pairs across regions: Loop through regions: for region in us-east-1 us-west-2; do aws ec2 describe-key-pairs --key-names KEY_NAME --region $region; done. Find which region has the key pair.
- 4Step 4: Fix - Use correct key pair name: Verify key pair name from list. Check for typos. Use exact key pair name (case-sensitive). Verify key pair exists in current region.
- 5Step 5: Fix - Create new key pair if needed: Create key pair: aws ec2 create-key-pair --key-name KEY_NAME --region REGION --query 'KeyMaterial' --output text > KEY_NAME.pem. Set permissions: chmod 400 KEY_NAME.pem. Or use existing key pair in correct region.
</>Code Examples
List All EC2 Key Pairs to Find Correct Name
1#!/bin/bash
2REGION="us-east-1"
3
4echo "=== All Key Pairs in ${REGION} ==="
5aws ec2 describe-key-pairs --region ${REGION} \
6 --query 'KeyPairs[*].[KeyName,KeyPairId]' \
7 --output table
8
9# Search for specific key pair
10KEY_NAME="my-key-pair"
11echo "\n=== Searching for Key Pair: ${KEY_NAME} ==="
12
13if aws ec2 describe-key-pairs --key-names ${KEY_NAME} --region ${REGION} &>/dev/null; then
14 echo "✓ Key pair exists in ${REGION}"
15
16 # Get key pair details
17 aws ec2 describe-key-pairs --key-names ${KEY_NAME} --region ${REGION} \
18 --query 'KeyPairs[0].[KeyName,KeyPairId]' \
19 --output table
20else
21 echo "✗ Key pair not found (EC2InvalidKeyPairNotFound)"
22
23 echo "\n=== Similar Key Pair Names ==="
24 aws ec2 describe-key-pairs --region ${REGION} \
25 --query "KeyPairs[?contains(KeyName, 'my')].KeyName" \
26 --output table
27fiCheck Key Pairs Across Regions
1#!/bin/bash
2KEY_NAME="my-key-pair"
3REGIONS=("us-east-1" "us-west-2" "eu-west-1" "ap-southeast-1")
4
5echo "=== Checking Key Pairs Across Regions ==="
6for REGION in "${REGIONS[@]}"; do
7 echo "\nChecking region: ${REGION}"
8
9 RESULT=$(aws ec2 describe-key-pairs \
10 --key-names ${KEY_NAME} \
11 --region ${REGION} \
12 --query 'KeyPairs[0].KeyName' \
13 --output text 2>/dev/null)
14
15 if [ ! -z "${RESULT}" ] && [ "${RESULT}" != "None" ]; then
16 echo "✓ Key pair found in ${REGION}: ${RESULT}"
17 break
18 else
19 echo "✗ Key pair not found in ${REGION}"
20 fi
21doneCreate New EC2 Key Pair
1#!/bin/bash
2KEY_NAME="my-key-pair"
3REGION="us-east-1"
4
5echo "=== Creating New Key Pair ==="
6echo "Key name: ${KEY_NAME}"
7echo "Region: ${REGION}"
8
9KEY_MATERIAL=$(aws ec2 create-key-pair \
10 --key-name ${KEY_NAME} \
11 --region ${REGION} \
12 --query 'KeyMaterial' \
13 --output text 2>&1)
14
15if [ $? -eq 0 ] && [ ! -z "${KEY_MATERIAL}" ]; then
16 echo "\n✓ Key pair created successfully"
17
18 # Save key material
19 echo "${KEY_MATERIAL}" > ${KEY_NAME}.pem
20 chmod 400 ${KEY_NAME}.pem
21
22 echo "\nKey saved to: ${KEY_NAME}.pem"
23 echo "Permissions set to 400 (read-only for owner)"
24else
25 echo "\n✗ Failed to create key pair"
26 echo "Error: ${KEY_MATERIAL}"
27 echo "Key pair may already exist (EC2InvalidKeyPairNotFound if trying to use)"
28fi↗Related Errors
Provider Information
This error code is specific to AWS services. For more information, refer to the official AWS documentation.