AWS - How to connect an EC2 instance if you lost keypair (.pem) file

AWS - How to connect an EC2 instance if you lost keypair (.pem) file

Connect the AWS EC2 instance if you lost the key pair a few ways. Here we can show how to connect an EC2 instance if you lost the key, generate new AMI images from the old instance, create a new key pair, and connect the EC2 instance.

Steps are given below:

  1. Step 1: Create AMI images of the old running instances.

  2. Step 2: Stop the existing instance and copy and edit user data from the old instance.

  3. Step 3: Go to AMI, select and launch the new instance from AMI, and create a new key pair. Allow https, http, and, in advance, setting the user data script that is running in the old instance.

Apache server user data. This data is needed in advance to create a new instance from AMI images.

#!/bin/bash

# Update package repositories and install Apache HTTP server
yum -y update
yum -y install httpd

# Enable Apache service to start at boot
chkconfig httpd on

# Start Apache service
service httpd start

# Create a simple HTML index page
echo "<html><h1>This is a test data script</h1>
<h1>Hello from $(hostname -f)</h1></html>" > /var/www/html/index.html

Fiverr