Generating a New Private Key for Pre existing EC2 Instances on AWS - sudo-arshia/tips_and_tricks GitHub Wiki

To generate a new private key for a pre-existing EC2 instance on AWS, follow these steps:

  1. Create a new key pair in AWS:

    • Open the AWS Management Console and navigate to the EC2 service.
    • In the EC2 Dashboard, click on "Key Pairs" in the left-hand navigation menu.
    • Click on the "Create Key Pair" button.
    • Provide a name for the new key pair and select the desired key pair format (such as RSA).
    • Click on "Create Key Pair" to generate the new key pair.
    • Save the private key file in a secure location.
  2. Stop the EC2 instance:

    • In the EC2 Dashboard, select the pre-existing instance for which you want to generate a new private key.
    • Click on the "Actions" button and choose "Instance State" > "Stop" to stop the instance.
    • Wait for the instance to stop before proceeding to the next step.
  3. Detach the existing root volume:

    • With the instance still selected, click on the "Actions" button and choose "Instance Settings" > "Detach Volumes".
    • In the Detach Volumes dialog, select the root volume and click on "Yes, Detach" to detach it from the instance.
    • Note down the device name of the root volume (e.g., /dev/xvda).
  4. Create a snapshot of the root volume:

    • In the EC2 Dashboard, navigate to the "Volumes" section.
    • Find the root volume with the device name noted in the previous step.
    • Right-click on the root volume and choose "Create Snapshot".
    • Give the snapshot a meaningful name and create it.
  5. Create a new volume from the snapshot:

    • After the snapshot is created, right-click on it and choose "Create Volume".
    • Specify the desired volume size and any other configuration options.
    • Select the availability zone and click on "Create Volume".
  6. Attach the new volume to the instance:

    • After the new volume is created, right-click on it and choose "Attach Volume".
    • In the Attach Volume dialog, select the pre-existing instance from the Instance ID dropdown.
    • Enter the device name (e.g., /dev/xvda) noted earlier.
    • Click on "Attach" to attach the new volume to the instance.
  7. Start the EC2 instance:

    • With the new volume attached, start the EC2 instance by selecting it and clicking on the "Actions" button, then choosing "Instance State" > "Start".
  8. Connect to the instance and update SSH configuration:

    • Once the instance is running, use the new private key file you downloaded earlier to connect to the instance via SSH.
    • Update the SSH configuration on the instance to allow the new private key for future connections. The exact steps for this will depend on the operating system running on the instance.

By following these steps, you can generate a new private key for a pre-existing EC2 instance on AWS. Remember to exercise caution and ensure you have proper backups of any important data.