I lost the pem file and deleted the key-pair under which an instance in currently running. I want to still use that server though. I created a new key pair but I don't know how to assign it to that instance.
You can go to your instance and say " launch instance like this". This time it will create clone of your EC2 instance. You can specify new key for this instance. Make sure you select the same security group.
This way, you will not loose your data and you will gain access to your ec2 instance with new key.
Note: If your old instance had EIP then you need to re-assign that EIP to new instance.
If your instance does not have EIP then you need to make sure you update your other instances with new private IP address or URL so that connectivity will not be broken.
If there are any EBS volumes attached to your previous instance then you need to attach to new instance.
Termination of old EC2 can be done as last step!!!.
I just went through this, and surprisingly, it was fairly "simple". I had the more difficult-to-recover S3 instance. EBS instances are easier.
For S3-backed instance:
You must have support through AWS. Well worth it, great support and all responses are answered withing 2 hours - I just bought the lowest cost tier of support.
Request AWS Back-Up. Per AWS: "AWS Support has a data recovery tool that will attempt to copy the instance store volume to an EBS backed volume. If the process is successful, you will be able to attach the created EBS volume to an instance and copy the needed data off to another volume. You must leave the instance running through out the process. The recovery tool will basically create an image of the current instance store data. There are some caveats you must be made aware of, though. The data recovery process can take days to complete. AWS support provides no guarantees regarding the state of the data returned."
Create a snapshot of the volume[s]
From the snapshot, create an Image
From the Image, launch an Instance
SSH into new instance and verify proper server files [e.g. /etc/postfix]
Set Elastic IP on new server
In Route 53, set A records to new server
I found the a solution to the problem here on aws site. The short version is to attach your EBS disk to another instance and replace the public key.
If you lose the private key for an EBS-backed instance, you can regain access to your instance. You must stop the instance, detach its root volume and attach it to another instance as a data volume, modify the authorized_keys file, move the volume back to the original instance, and restart the instance. For more information about launching, connecting to, and stopping instances, see Instance Lifecycle.
This procedure isn't supported for instance store-backed instances. To determine the root device type of your instance, open the Amazon EC2 console, choose Instances, select the instance, and check the value of Root device type in the details pane. The value is either ebs or instance store. If the root device is an instance store volume, you must have the private key in order to connect to the instance.
You can ONLY get access to instance using private key that was created specific for the instance. Thats because in launch wizard you agree that you will access only through that key (see the screenshot below). Be sure to save the private key and it should have 400 permissions.
But there is a way around. You can get access to instance, by cloning the image instance. see here: https://forums.aws.amazon.com/message.jspa?messageID=549256
It says that
An instance can only be associated with a key pair only at launch time (either to an existing key pair or by creating a new key pair). that means if you lose the key pair then you won't be able to generate another one for that already running instance or associate it with an already existing key pair. To urgently address your issue, you may try to do the following:-
- From your WAS console stop the instance in question
- Create a snapshot of the instance
- Create a duplicate instance from the resulting snapshot and create a new Key Pair.