How to deploy a Linux EC2 with Key Pair from a SSH Client
What is Amazon EC2 ?
Amazon Elastic Compute Cloud (Amazon EC2) provides on-demand, scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 reduces hardware costs so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. You can add capacity (scale up) to handle compute-heavy tasks, such as monthly or yearly processes, or spikes in website traffic. When usage decreases, you can reduce capacity (scale down) again.
An EC2 instance is simply a virtual server. With an EC2 instance, AWS subscribers can request and provision a computer server within the AWS Cloud.
OBJECTIVE:
In this article, I will be showing you a step-by-step guide on how to do the following:
- Deploy a Linux EC2 instance with keypair
- Create a security Group to allow SSH
- SSH into the deployed EC2
- Run the “whoami” command to verify you are in your EC2 instance
PREREQUISTES:
- Amazon Free Tier Account
- Internet Connection
STEP 1: Log into your AWS Free Tier account using the IAM user role.
STEP 2: From the amazon S3 console search EC2 (Virtual Servers in the Cloud).
STEP 3: From the EC2 Dashboard select the launch instance tab.
STEP 4: From here, name your instance. I named mine “LevelUpCorpLinux”
STEP 5: From the Application and OS Images Tab, select the Amazon Linux 2023 AMI.
STEP 6: In the “Instance type” tab make sure the t2.micro instance type is chosen.
STEP 7: Now we’re going to create a key pair to use to log into our instance. A key pair is a combination of a public key that is used to encrypt data and a private key that is used to decrypt data.
From the key pair (login) tab, select the Create new key pair link in the bottom right corner to create a key pair.
STEP 8: From here name your key pair, I named mine “levelupkeypair1” For the Key pair type and Private key file format leave them on the default settings RSA and .pem, then select create Key pair in the bottom right hand corner.
STEP 9: In this step we will configure our network settings. In the network settings tab click edit.
from here we will leave our VPC, Subnet, and public IP set at default settings.
*note* make sure your IP is public so that you can connect to your instance
STEP 10: For the firewall security group we will be creating a new security group. A security group acts as a virtual firewall that controls the traffic allowed to and from the resources in your virtual private cloud (VPC). You can choose the ports and protocols to allow for inbound traffic and for outbound traffic.
Lets name our security group, I named mine “Levelupacces,” then copy the name of your security group and paste it in the description box as well.
STEP 11: For our inbound security group rules we want our type set to SSH and our source type set to anywhere. You can use a Secure Shell (SSH) to connect to your Linux instance from a local machine that runs a Linux or macOS operating system, or you can use a platform independent connection tool, such as EC2 Instance Connect or AWS Systems Manager Session Manager. All other settings will stay the same then we will launch our instance.
STEP 12: Now that we created our instance, I’m going to show you how to SSH into your deployed EC2 instance. Go Back to the EC2 console by selecting instances.
Step 12: From here select the instance that we just created and click connect. Once connected, select SSH client from the “connect to instance” tab.
STEP 13: From here you will need to know the location of the key pair that you created earlier and a SSH client, since I’m using a MAC I will SSH with Terminal. My key pair is located in my downloads folder, so in the terminal I will use code “cd downloads” to open the downloads folder to access my key pair.
STEP 14: Next we will use the command in step 3 of our connect to instance tab to ensure that our key is not publicly viewable.
STEP 15: Now we will run our SSH command in the bottom of the connect to instance tab. To do this copy the code and paste it on the command line in your terminal.
After doing this I got this message and was puzzled on why it wasn’t a success. I realized that I didn’t run the file first to change the permission so I typed yes to allow permission and ran the command again from step 3 and the SSH Client command now it should be a successful connection.
STEP 16: Now we will run the “whoami” command, after running this command it should verify the username used to launch the instance.
This about sums it up on how you connect to a Linux Instance from a SSH Client.