Using an SSH client to connect to an EC2 Instance
AWS — Amazon-Linux 2
I was happy to learn about deploying apps onto servers, but the most difficult part for me was using SSH to get into the virtual environment. Mainly because I did not understand how key-pairs work and how important it is to store them some somewhere safe.
Virtual environments, aka instances, are virtual servers that are rented on an hourly base for users to deploy their applications on them. This saves them the effort of dealing with hardware.
In this tutorial, we’ll be launching an instance and signing-into it using SSH.
AWS Console
We’ll begin by launching an instance. Please create an AWS account if you don’t have one yet.
Login into your console & from the top menu, select Services, then select EC2.
Click on Running Instances and create one with Amazon Linux.
You’ll have some options of instance types to choose from — varying mainly by CPU, memory and network performance. We’ll use the free tier eligible, t2.micro option which is usually highlighted in green.
We’ll stick with the default setup, and select Review & Launch. You’ll be presented a page with all the settings and specifications of your instance.
Click Launch and you’ll be prompted to choose or create a new key-pair which consists of a public key and a private key file that you can use to connect to your instance securely.
Please note: You cannot download this key-pair file more than once. Please Make sure to keep it safe!
Using SSH
- Once you’ve launched your instance, right-click on it to view options, and select Connect.
- Select the SSH client tab, and copy the command example. (Part of the command should contain the name of your key-pair file)
- Open terminal or command-line, and navigate to your key pair (.pem)file.
- First we’ll assign the key pair file appropriate permissions, run:
chmod 400 keyPairName.pem
- Finally, you’ll paste example command and run it to SSH into your instance:
ssh -i "keyPairName.pem" ec2-user@ec2-54-345-678-90.us-west-1.compute.amazonaws.com
- When you are connecting to a server for the first time, you’ll be asked if you want to continue connecting. Make sure to type yes and press Enter (This will add an ECDSA key fingerprint)
And Voila! Congrats for getting this far, feel free to ask any questions or share your thoughts.
Happy Coding! 🌟