DEV Community

Cover image for How to recover your EC2 instance key pair
Mukesh Singh
Mukesh Singh

Posted on

How to recover your EC2 instance key pair

Hi there! If you are looking for exact way to regenerate or redownload the already generated key-pair of your AWS EC2 instance then, I will apologize and would like to let you know in very beginning of this article that, I haven't found any such way.

However, if you want to have a new EC2 key-pair without losing any data of your EC2 instance, then this article will definitely help you.

πŸ‘‰πŸ» Time Saver >>> In case you already have good experience with AWS. Then you can create an AMI, and then you can launch an instance from that AMI. Here will not lose anything and get a new key-pair as well.

Here, I'm going to mention the process in a step-wise manner by putting an example. So, in case someone is referring this for learning purpose only, even then he can get most out of this reading.

Old EC2 instance (marvel) - I will treat it like its key-pair has been lost

EC2 Instance Console

image 1

You can see in the above image, I have a directory named test which contains two files named test.py and test.sh

Create an AMI (Amazon Machine Images) of existing EC2 instance

  1. Open EC2 services then, go to instances section
  2. Select required instance
  3. Click on Actions dropdown button
  4. Go to Image and templates
  5. Select Create image and it will take you to the AMI creation flow, like image 3 below

Create EC2 AMI

image 2

AMI creation flow

  1. Provide Image name then,
  2. Provide Image description (optional)
  3. You can go with default options for rest of the steps, or you can include/exclude your additional volumes.

AMI creation wizard

image 3

You can add tags also to your image. Now, move ahead by clicking on Create Image button and it will take you back to the instances page. You will get a message for your AMI creation status, like - image 5

AMI creation

image 4

Add tags to AMI

image 5

If I check my AMI's status then, it will show as pending because my instance is running.

Stop EC2 instance

image 6

Now, I have to stop my instance. So, that AMI will get created

  1. Go to instances
  2. Select required instance
  3. Open Instance State dropdown
  4. Select Stop instance

Stoping EC2 instance

image 7

Now, on the AMI page you will find your AMI's updated status

Once your AMI's status will become available then it can be used for launching new instances. Until AMI's status becomes available, please do not make any modification to your EC2 instance.
(Obviously you have to select the required one in case you have multiple AMIs)

AMI status

image 8

You can launch an instance by clicking on the "Launch instance from AMI" button in the top right corner, or you can go to EC2 >>> instances >>> Launch an instance. Here you will find "My AMIs" tab alongside "Quick Start" tab under "Application and OS images" section. simply select the respective image as mentioned in image 9

select respective AMI

image 9

Under "Key pair" section, you have to create a new Key Pair

Create new instance from AMI

image 10

Simply put a name for your key pair

(Generally I go with "Key pair type" = "RSA" and "Private key file format" = ".pem". However, you can proceed as per your wish)
Now, click on "Create key pair" and it will download the "private key file".

Create new EC2 Key Pair

image 11

Now launch the instance

Launch new EC2 instance

image 12

You can see in below image 13 a new instance has been created and initializing.
(If only 2 instances confusing you then let me tell you, in the beginning of this article I had 2 instances one of them was terminated so by the time that one gone. That's why even after creating a new instance you are seeing only two instead of 3)

list of EC2 instances

image 13

Now let me show you that my data from older instance (marvel) has been carried to this instance or not

EC2 Instance Console

image 14

All the data has been carried successfully.

Congrats πŸ‘πŸ», We have successfully completed the key pair recovery process!

Top comments (0)