Understanding and Using EC2 Key Pairs

Fırat TONAKFırat TONAK
3 min read

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.

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

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

  2. .ppk is for PuTTY

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

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

Note that you must keep it safe.

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.

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.

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

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.

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.

Execute the following command using the Public IP.

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.

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.

0
Subscribe to my newsletter

Read articles from Fırat TONAK directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Fırat TONAK
Fırat TONAK