How to set up SSH access to an EC2 instance
To set up ssh access to an EC2 instance, we need a publicly accessible hostname or the IP address of the instance, a username and a private key.

Creating the private key
When launching a new EC2 instance, you need to create a key pair that you will use to securely connect to the instance.
In the launch an Instance form, under Key pair(login), click “create a new key pair”
create new key pair In the modal that follows, add the name of the key pair and then click “create key pair” to create it.
create key pair modal - After clicking create key pair, the key will be downloaded on your computer. Store it securely as we will need it to connect to the instance later on.
Under the Network settings section. Be sure to use a security group that allows SSH traffic from anywhere.
firewall settings
Establishing a connection
To establish an SSH connection to the EC2 instance, locate the key that was used to launch the ec2 instance. Make sure that the key is not publicly viewable by running chmod 400 priv-key.pem.
If you don't know which key was used to launch the instance, you can go to the EC2 instance dashboard:
Click the connect button
instance summary connect Switch to the SSH client tab, then you will be able to see the name of the file used to launch the ec2 instance.
ssh client tab secure
Finally, connect to the ec2 instance by running ssh -i “priv-key.pem” {USERNAME}@{HOSTNAME}