You will perform two main phases:
The main idea is to use the EC2 Instance Connect (browser-based SSH) as a temporary “backdoor” to install a new, permanent key.
First, you need to create a new key pair on your own computer. This will generate a new private key (which you keep secret) and a new public key (which you will give to EC2).

ssh-keygen command to create a new key. ssh-keygen -t -f ~/Downloads/my-new-ec2-key
my-new-ec2-key.pem (The private key. Keep this safe!)my-new-ec2-key.pub (The public key. This is what you’ll add to the instance.)my-new-ec2-key.pub) with a text editor and copy its entire contents to your clipboard. It will look something like ssh-rsa AAAAB3NzaC1yc....
Next, you’ll use the AWS Console to get temporary access to the instance’s terminal.
ec2-user, ubuntu) and click Connect.Now you are inside the instance. You just need to “authorize” the new public key you created in Step 1.
authorized_keys file. This file lists all public keys that are allowed to log in.nano or vi to open the file. (Nano is easier if you’re not familiar with Vi).Bashnano ~/.ssh/authorized_keysmy-new-ec2-key.pub that you copied to your clipboard).nano: Press Ctrl+O to Write (Save), press Enter to confirm the filename, and press Ctrl+X to Exit.You’re done with the browser. The final step is to test your new key from your local computer’s terminal.
chmod 400 ~/Downloads/my-new-ec2-keyssh -i "~/Downloads/my-new-ec2-key" ec2-user@<your-instance-ip-or-dns>