Amazon Elastic Compute Cloud (Amazon EC2) is a web service offered by Amazon Web Services (AWS) that provides flexible computing power in the cloud. It allows users to rent virtual servers, called instances, and run applications or workloads on them. Amazon EC2 is a versatile and robust service that lets users run almost any workload in the cloud, with the capability to easily and effectively adjust resources to meet evolving business requirements. In this article, we are going to see how to launch an EC2 instance and SSH into it.
First of all, you need an AWS account. If you don't have one, go to AWS and create a new account. Once this is done, log into your account home console. This will look similar to the image shown below.
On the top left, you can search for the AWS services you want to use, while the most recently used services are visible on the home console. If the EC2 instance is visible, click on it or search for it and click on it to open the EC2 dashboard. The EC2 dashboard will have a summary of any instances you have running, plus other services associated with EC2 like elastic IPs, auto-scaling groups, and security groups. It should look similar to the image shown below. Click on the orange button written Launch Instance.
This will open a page where we will be prompted to configure a few choices depending on our needs, including the instance name and the type of OS application we want, called an Amazon Machine Image (AMI), which AWS describes as "a template that contains the software configuration (operating system, application server, and applications) required to launch your instance." For this post, we will name our instance hashnode-test-server and use the Ubuntu AMI.
We will then select the instance type. Note that AWS has many instance types depending on your needs. You can read about them here. For this tutorial, we will use the t2.micro since it is eligible for the free tier account we are using. Next, we create a key pair that is necessary for securely accessing your instance via SSH and working within our instance. We will name this key pair hashnode-test-key and leave the other options as default. Remember that this key will be downloaded to your local computer after creation. Keep it in a secure location.
Next, we configure the network settings for the instance. This is where we configure a VPC and Security Group that determine what traffic is allowed into the instance (inbound) and what traffic is allowed to leave the instance (outbound). For test purposes for this tutorial, we will create a security group that allows any inbound and outbound traffic and also SSH from anywhere on the internet. Note that this is a security risk, and it is recommended to allow only known IPs such as your computer's public IP address to access the instance. However, this is outside the scope of this tutorial, and you can read more about it here.
Finally, we configure the volume settings for the instance, and we will pick a general-purpose SSD (gp3) of 8GB for this tutorial. Click on launch instance and wait for a few minutes for your instance to be created.
Navigate back to the EC2 dashboard and click on instances. A page will open up listing all your instances (running and stopped). In our case, you will see the instance we created named hashnode-test-server in a running state. Select it and click connect from the top right.
Select the SSH client option, copy the command provided which should look similar to this:
ssh -i "hashnode-test-key.pem" ubuntu@ec2-3-254-185-56.eu-west-1.compute.amazonaws.com
The values after ubuntu will be different. On your local computer, navigate to the location where you stored the key pair named hashnode-test-key and open a terminal there. First, we need to change the permissions on the key to read-only. Execute the command below on a Linux-based machine (for Windows or any other OS, search how to convert a file to read-only).
chmod 400 hashnode-test-key.pem
For a Linux-based machine, an SSH client is already installed, and you can simply paste the command we copied and run it. If prompted to permanently add the key, type yes and press enter.
Note how brion@xbrion
changes to ubuntu@ip-172-31-5-32
. This means you are logged into your instance, and you can perform any workload. Do not forget to terminate or stop your instance before logging out of AWS to save on costs.
Happy hacking❤️!