Recover the AWS EC2 SSH key lost or Corrupted

If the AWS EC2 instance and your SSH key no longer work or are Lost or Corrupted. How am I going to regain access to the instance? There is a pretty simple workaround if you have access to the AWS console and the region where the EC2 instance is hosted. Let’s begin with a quick process to recover the AWS EC2 SSH key lost or corrupted where access to the low-level shell environment is lost with the key.

AWS keypairs are essential for access to AWS EC2 instances

Provisioning a new EC2 instance is assigning a key pair to the instance. Before finalizing the instance creation, you will see the dialogue box asking you to Select an existing key pair or create a new one. If you have a current key pair, you can re-use one you have currently, or you can choose to create a new key pair.

You can also create the key pair ahead of time by navigating to EC2 > Key pairs. You can select Create key pair.

Either creating the keypair ahead of time or creating/selecting a key pair on the fly is an important step that you want to pay attention to. After you generate a new keypair using either method or if you choose an existing key pair, you need to have the corresponding private key saved in a safe location. When you create a new keypair, your browser will download the private key file in the format you choose for use with the SSH command (.pem) or Putty (.ppk).

Recover the AWS EC2 SSH key lost or corrupted

What if you get the dreaded message below? You verified the key is what you think it is, and it is still not working.

Create an AMI image

We can create an Amazon AMI image of the EC2 instance using the AWS console. With this, you are creating an “image” you can deploy, just like the images in the Amazon catalog when you initially deploy an EC2 instance.

The image you create is an exact copy of the server you are imaging, including the programs and settings applied when you launch an EC2 instance. You can create an image from the configuration of an existing instance. The crucial characteristic here is that you can select a new key pair when you deploy a new EC2 instance using the image.

The image can be created on your server while it is up and running. Navigate to Actions > Image and templates > Create image.

On the Create image page, you name the new AMI image and assign a description, tags, etc. Once you have assigned everything for the image, click the Create image button.

Now that we have a new AMI image, we can select the AMI image and choose Launch instance from image.

The crucial step for what we are trying to accomplish is that we get to Select an existing key pair or create a new key pair. This allows you to spin up an exact image using the AMI image and choose a new or select an existing key pair, which allows restoring access to the EC2 instance.

As you can see below, after deploying the AMI image as a new EC2 instance, I now again have access using a new SSH key that was created.

AWS EC2 key pair FAQs

  • What is an AWS EC2 key pair? – The key pair is made up of a public and private key. You have possession of the private key, and AWS has control of the public key.
  • Which pair of EC2 key pair do you have? You have the private key, either in the .pem format or .ppk format
  • What is an Amazon AMI image? – It is an exact copy of the configuration and settings of an EC2 instance. You can create an AMI image from an existing EC2 instance.