To change the key pair, create an AMI of the existing instance, and then launch a new instance. You can then select a new key pair by following the instance launch wizard. Follow these steps:
Before starting this procedure, be aware of the following:
- Stopping and restarting the instance erases any data on instance store volumes. Be sure that you back up any data on the instance store volume that you want to keep.
- Stopping and restarting the instance changes the public IP address of your instance. It’s a best practice to use an Elastic IP address instead of a public IP address when routing external traffic to your instance.
MIME-Version: 1.0 --// Content-Type: text/cloud-config; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cloud-config.txt" #cloud-config cloud_final_modules: - [users-groups, once] users: - name: ```username``` ssh-authorized-keys: - ```PublicKeypair```
usernamewith your user name, such as
ec2-user. You can enter the default user name, or enter a custom username, if one was previously set up for the instance.
PublicKeypairwith the public key retrieved in Step 4. Be sure to enter the entire public key, starting with
cloud-initphase is complete, validate that the public key was replaced.
Important: Because the script contains a key pair, remove the script from the User Data field.
Note: If your instance is Amazon Linux 2 2.0.20190618 or later, you can use EC2 Instance Connect to connect to the instance.