Regaining access to your aws ubuntu ec2 instance

Today I was asked to get access to an ec2 instance back, My client had lost access to key file that was necessary to access the instance. It was Ubuntu instance and my client did not have access to any users in sudoer list. 

Below is what I had to deal with to get access to instance back using new key file.

  1. Logged in to aws account and stopped the main instance (downtime was not the issue here, could have snapshot the volume)
  2. Detached the root volume from the Main instance
  3. Started new micro instance (Named it Recovery instance) and attached the root volume of the main instance to it. 
  4. Mounted it and looked in to the volume. (:( there was no user directory or files in home)
  5. Detached other volume that was attached to the main instance and attached it to the new Recovery instance that I created. 
  6. Mounted it and looked in to the volume. (User’s home directory files were in this volume so concluded this volume was mounted to the /home directory in Main instance that I was to recover access to)
  7. looked in to the users .ssh folder and replaced the public key content in /home/ubuntu/.ssh/authorized_keys file with new key details
  8. Also It was now not possible to access the Main instance as volume mounted to the  /home directory was detached. So I used this trick. I created new /home/ubuntu directory in root volume of main instance attached to this new recovery instance and added my public key to the authorized_keys file in /home/ubuntu/.ssh/ directory.
  9. Then I Stopped the Recovery instance and Detached the volumes of the main instance. 
  10. Attached those volume back to the main Instance respectively. 
  11. I was then able to access the main instance with ubuntu user. I logged in to the instance with ubuntu user. 
  12. Then I deleted contents inside /home directory and mounted the Attached volume to the /home directory in main instance and I was then able to access the instance with ubuntu user. 

It really feels good to contribute to OpenSource projects

I wrote a post few months back about Backing up ec2 volume with ec2-consistent-snapshot, This was my first pull request to any Github/OpenSource project. Since I was using this program for my use, and I had made few changes done on the program, I felt it might be good idea to contribute the changes back, as there might be other users of the program who might be in need for it too.

Today it got merged and I even got credits on the project page. And it really feels great to contribute. :) 

Project link: https://github.com/alestic/ec2-consistent-snapshot

image