How to recover your EC2 instance key pair
Tags: aws, ec2, devops, recover
Published: 2022-11-26 12:25:13

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xibgi3skv2y40yuyoocp.png)
<p style="width=100%;" align="center">image 1</p>

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k0m6n686belsahy0sg78.png)
<p style="width=100%;" align="center">image 2</p>

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31bxmh4adi3nj0hd2eqd.png)
<p style="width=100%;" align="center">image 3</p>
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zps5v2utnjrbzh5xh86.png)
<p style="width=100%;" align="center">image 4</p>

![Add tags to AMI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzvwxlyhdj9gf3sni539.png)
<p style="width=100%;" align="center">image 5</p>

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


![Stop EC2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrhmjfer5cgruofxibhy.png)
<p style="width=100%;" align="center">image 6</p>

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ock0y39bliac7xxiwofi.png)
<p style="width=100%;" align="center">image 7</p>

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7uqcqhg5kaawda1yj2dh.png)
<p style="width=100%;" align="center">image 8</p>

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9o4dwfwr78ot66jlkiu.png)
<p style="width=100%;" align="center">image 9</p>

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


![Create new instance from AMI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c44gv969n0jfr3qllmdj.png)
<p style="width=100%;" align="center">image 10</p>
## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmtyh6tf77rqqyuwlk9o.png)
<p style="width=100%;" align="center">image 11</p>
Now launch the instance


![Launch new EC2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6uy84ywevjai0y6yv1b.png)
<p style="width=100%;" align="center">image 12</p>

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6724ogeke3xa3e1q56cm.png)
<p style="width=100%;" align="center">image 13</p>
#### Now let me show you that my data from older instance (marvel) has been carried to this instance or not


![EC2 Instance Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xacqakxdgw2ei00n3c0.png)
<p style="width=100%;" align="center">image 14</p>

All the data has been carried successfully.

Congrats 👏🏻, We have successfully completed the key pair recovery process!