How to recover and access AWS EC2 instances if we lost pem file?
I want to connect to my Amazon EC2 instance but have lost the SSH key pair. How can I connect to the instance?
Important: This method require a stop and start of the instance.
Step 1. Create a new key pair (From ec2 dashboard inside Network & security).

Step 2. Retrieve the public key for the key pair from private key (Do in local system that will used in step 6).

Step 3. Open the Amazon EC2 console.
Step 4. Stop your instance.
Step 5. Choose Actions, Instance settings, Edit user data.

Step 6. Copy the following script into the Edit user data dialog box:
Content-Type: multipart/mixed; boundary=”//”
MIME-Version: 1.0
— //
Content-Type: text/cloud-config; charset=”us-ascii”
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=”cloud-config.txt”
#cloud-config
cloud_final_modules:
— [users-groups, once]
users:
— name: username
ssh-authorized-keys:
— PublicKeypair

Step 7. Choose save.
Step 8. Start your instance.
Step 9. Login with new pem file. now you able to login with new pem file.
Step 10. Now again stop instance.
Step 11. Clear script from Edit user data dialog box. because each time we restart instance it will execute every time.
Step 12. Again start EC2 instance.
✌!