How I debugged through AWS ec2 instance creation when SSH failed to work
Tags: aws, ec2, ssh, webconsole
Published: 2023-07-12 11:05:03

## Introduction:
Creating and connecting to an Amazon Web Services (AWS) EC2 instance is a fundamental skill for anyone working with cloud infrastructure. However, even experienced users can encounter challenges, such as failed SSH connections, when attempting to connect to newly created instances. In this blog post, we will explore the steps I took to debug an SSH failure while connecting to an EC2 instance, specifically when I created an instance without a key pair. By following these troubleshooting techniques, you can quickly identify and resolve similar issues.

When I started to create an ec2 instance, I came to this step where I needed to select key-pair(login) option. I chose `without key-pair` to create further as shown below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lopmp7zza771cxihy93.png)
By the time I got to start the instance and connect it through `SSH` after finishing all the steps, I grabbed the command and ran it in the terminal.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vl7o7e6qyjhrnkw17let.png)
Next, I tried to setup as per the instruction given in the SSH connection without key-pair. I failed to do so.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p910edf3kfwtlvwlipr7.png)
After retrying several times, I realised that I am missing somewhere. Then I got to know I can't connect to the instance without key-pair.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otdtvtf7lx6okc57d2vh.png)
I chose other option to connect the instance created. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xufrenm11rzlvwq5uy2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d5i21cfma0o5jxavnis.png)

Finally, I was able to connect through EC2 instance connect which opens you up a new tab to use the web based console.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlls0oem5a9jc8yyyad1.png)

If we want to connect ec2 instance using SSH and ignore the failures like me, we can follow the steps as such below

> Step 1: Confirm SSH Configuration
`The first step in troubleshooting an SSH connection issue is to ensure that the SSH configuration on the EC2 instance is correctly set up.`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36kaa9nkrq8rhbynoh8z.png)

> Step 2: Create and Associate a Key Pair
`If you created the EC2 instance without a key pair, you won't be able to establish an SSH connection. In this case, you will need to create a new key pair and associate it with the instance:`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvr1e7xzobvptcld1tb4.png)
We can also follow as such given following ways:
Generate a New Key Pair: In the EC2 Dashboard, navigate to the "Key Pairs" section and create a new key pair. Save the private key (.pem file) securely on your local machine.

Associate Key Pair with the Instance: Right-click on the instance in the EC2 Dashboard and select "Actions" > "Instance Settings" > "Attach/Replace IAM Role." Choose the newly created key pair, and AWS will associate it with the instance.

## Conclusion:

Debugging SSH connection issues when creating an AWS EC2 instance can be a frustrating experience, but by following a systematic troubleshooting approach, you can identify and resolve the underlying problems. In this blog post, we explored the steps to confirm SSH configuration, create and associate a key pair, and establish an SSH connection. Remember to double-check security group rules, ensure the instance has a public IP, and verify the permissions and path of the private key. By mastering these techniques, you can effectively troubleshoot SSH connection failures and confidently manage your EC2 instances in AWS.