SSH into EC2 Instance whose key pair is lost

Lab Details

  1. This lab walks you through the steps to SSH into EC2 Instance whose Key pair is lost.

  2. You will practice using Amazon Machine Images to launch Amazon EC2 Instances and use key pairs for SSH authentication to log into your instance. Then create a temporary instance with a new key pair and transfer the key pair data to the first instance and SSH into that instance using this instance's key pair.

  3. Duration: 90 minutes

  4. AWS Region: US East (N. Virginia) us-east-1


What is EC2?

  • AWS defines it as Elastic Compute Cloud.

  • It’s a virtual environment where “you rent” to have your environment created, without purchasing. 

  • Amazon refers to these virtual machines as Instances.

  • Preconfigured templates can be used to launch instances. These templates are referred to as images. Amazon provides these images in the form of AMIs (Amazon Machine Images).

  • Allows you to install custom applications and services.

  • Scaling of infrastructure i.e., up or down is easy based on the demand you face.

  • AWS provides multiple configurations of CPU, memory, storage etc., through which you can pick the flavor that's required for your environment.

Amazon EC2 Keypair

Key Pair consists of a public and private key, which is a set of security credentials that we use to prove our identity when connecting to an instance.  Amazon EC2 stores the public key, and you store the private key.  Instead of a password, you will be using a private key to securely access the instance.  Ownership of private key security is with the customer.  So, he/she has to be careful in handling it.

Case Study

ABC Corporation is a software company that is new to working with AWS. They had launched a group of PoC servers and had defined a Key pair to login into the servers. Since they are new, and due to some unknown reasons they had lost the key pair file. They have a customer visit scheduled and are required to demonstrate the PoC. Now, how do they get to the server and have the PoC presented? We have got multiple ways to get this issue resolved, And one of the ways you will see with this lab.

Architecture Diagram

Task Details

  1. Launching Lab Environment

  2. Launching an EC2 Instance

  3. Stop the Original_Instance

  4. Launch a temporary instance

  5. Detach the root volume of the first instance and attach it to the temporary instance

  6. Add the new public key information to the Original instance

  7. Unmount and detach the temporary volume and attach to the Original instance

  8. SSH into Original_instance using new key pair

  9. Delete the AWS Resources