# Understanding and Using EC2 Key Pairs

Let's imagine you want to create a virtual server on AWS called an EC2 instance and need a secure way to connect to it. This is where key pairs come in. A key pair acts as a lock and key that ensures you can access only your EC2 instances.

This article will set up a key pair using the AWS Management Console. Setting up a key pair does not require any coding skills. By the end of this article, you will learn how to create a key pair and how to connect to your EC2 instance.

> Please ensure that you have a valid AWS account before proceeding with this article.

Go to the AWS Management Console, search for `EC2`, and select the first option in the results list.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735233548080/5dbb9b9a-a4c4-4b2c-ad36-19a10e24b2d6.png align="center")

On this page, select `Key Pairs` under `Network & Security`, then click `Create key pair`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735233748417/c1948d6c-fcb6-4a13-95e0-0120bd3379fe.png align="center")

Now, we will enter details for setting up a key pair.

The `Name` field is where you enter your key pair name.

There are two different types of key pairs:

1. RSA: A cryptographic system that uses public and private keys for secure communication and authentication in systems.
    
2. ED25519: A modern public-key cryptography algorithm that offers high performance and security. Its key size is smaller than RSA.
    

There are two different types of file formats:

1. .pem is for [SSH](https://en.wikipedia.org/wiki/Secure_Shell)
    
2. .ppk is for [PuTTY](https://en.wikipedia.org/wiki/PuTTY)
    

Enter your details as shown below and click `Create key pair.`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735238121427/d2455664-b97d-4bab-a6ed-49b63569a910.png align="center")

You should see the key pair in the list, and it will be downloaded automatically.

> Note that you must keep it safe.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735265158262/958bc902-827b-4519-b1e2-4917d879998f.png align="center")

Let's create an EC2 instance and verify that our key pair is working.

Go to EC2 on the AWS Management Console, click `Instances`, and then click `Launch instances`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735266248836/b660b86d-492b-4084-8245-7f8cb9429cc0.png align="center")

Since we'll use the Free tier in this article, select the `t2.micro` instance type. Then, select the key pair you just created and click `Launch instance`.

> We will not cover all EC2 details in this article.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735266894098/f2d6a70f-11e9-40ad-b311-32e5990ab0b6.png align="center")

After the process is complete, you should see your EC2 instance on the list.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735266963947/547709a9-b93c-49ce-b2f2-4c5a20847dc1.png align="center")

Now, we'll learn how to connect to our EC2 instance using the key pair. Let's open a `Terminal`.

We must set the correct permissions for our key pair file `AWSKeyPair.pem` by executing the `chmod 400` command.

```bash
chmod 400 ~/Downloads/AWSKeyPair.pem 
```

After setting permissions, we need to get our EC2's public IP. Go to the AWS Management Console and find the `Public IPv4 address` on the Instances page under Details.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735267818913/c50d3875-e93b-4f7b-9d4e-0100198d2b59.png align="center")

Execute the following command using the Public IP.

```bash
ssh -i ~/Downloads/AWSKeyPair.pem ec2-user@54.196.249.211
```

After executing the command, you'll receive a connection verification prompt. Type `yes`, and if successful, you'll see the EC2 console.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735268000898/bd02d841-9f03-432b-950e-be44ba158d7d.png align="center")

Following these steps, you can create a key pair and connect to your EC2 instance. A key pair ensures that only you can access the instance, so keep your .pem file secure and never share it. You can now use this knowledge for key pair management and EC2 creation in your projects.
