To create a key pair using AWS CLI, you can use the aws ec2 create-key-pair
command. Here’s how to do it:
Step 1: Open the AWS CLI
First, open the AWS CLI on your local machine or a remote server. You can do this by opening a terminal window and typing aws
in the command prompt.
Step 2: Create a key pair
To create a new key pair, use the following command:
aws ec2 create-key-pair --key-name <key_name> --query 'KeyMaterial' --output text > <key_name>.pem
Replace <key_name>
with a unique name for your key pair. This will be the name that you use to identify the key pair when launching EC2 instances.
This command will create a new key pair with the specified name and output the private key to a file with the same name as the key pair, but with a .pem
extension. The private key file will be saved in the current working directory.
Step 3: Set permissions on the private key file
Before you can use the private key file to connect to an EC2 instance, you need to set the correct permissions on the file. Use the following command to set the permissions:
chmod 400 <key_name>.pem
Replace <key_name>
with the name of your key pair.
Step 4: Verify the key pair
You can verify that the key pair was created successfully by using the following command:
aws ec2 describe-key-pairs --key-names <key_name>
This command will display information about the key pair, including the fingerprint of the public key.
That’s it! You have successfully created a key pair using AWS CLI. You can now use this key pair to launch EC2 instances and connect to them using SSH.