Skip to content

Instantly share code, notes, and snippets.

@anmolnagpal
Created September 26, 2018 19:04
Show Gist options
  • Save anmolnagpal/59f3211c15f0032c52c7d094b6ecb992 to your computer and use it in GitHub Desktop.
Save anmolnagpal/59f3211c15f0032c52c7d094b6ecb992 to your computer and use it in GitHub Desktop.
lost your key_pair

Is there any way to recover your AWS insyance, if you lost your key_pair?

Hardly not! because aws provide the key_pair only once.Here is one tip for recovering your AWS instance with a new set of key_pair.

Step1:Create a new AWS instance with the same instance type as the old one and save the new keu_pair(New instance would be in the same availability zone as the old one)

Step2:Stop the old instance and detach the root volume from the old one.

Step3:Attach the old instance root disk to new one

Step4:Login to the new instance and run below command to list the attached devices

#lsblk (You can find the attached volume as a secondary volume in the new instance)

Now create a a mount point '/mnt/recovery' and mount the secondary volume to this mount point. For example, secondary volume is '/dev/xvdb1'

#mount /dev/xvdb1 /mnt/recovery

Now append the authorized key files to the mounted volume

#cat /home/ec2-user/.ssh/authorized_keys /mnt/recovery/home/ec2-user/.ssh/authorized_keys

Step5: Unmount the secondary volume and detach the old root disk after stopping the new instance

Step6: Attach the root disk volume back to old instance and start it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment