Skip to content

Lesson 2 of 5

Launch a virtual machine with EC2

Launch an EC2 instance (a virtual server) and connect to it with SSH; this instance will host your web page and is equivalent to Azure Virtual Machines and Google Compute Engine.

9 minCloud

After this lesson you will be able to launch a virtual server on AWS, connect to it from your computer, and run commands on it. You will also know the equivalent services on Azure and Google Cloud.

What is a virtual machine?

A virtual machine is a computer that runs inside another computer. It has its own operating system, memory, and disk, but it shares the physical hardware with other virtual machines. Cloud providers rent virtual machines by the hour or second. On AWS, a virtual machine is called an EC2 instance. EC2 stands for Elastic Compute Cloud. On Azure, the same thing is called a Virtual Machine. On Google Cloud, it is called a Compute Engine instance.

You will use an EC2 instance to host a simple web page. In later lessons, this instance will also run a small application that stores files.

Launch an EC2 instance

Sign in to the AWS Management Console with the IAM user you created in lesson 1. In the search bar at the top, type EC2 and choose EC2 from the list. This opens the EC2 dashboard.

Click the orange Launch instance button. You will see a form with several sections.

In the Name and tags section, type a name for your instance, for example my-web-server.

In the Application and OS Images section, keep the default Amazon Linux. This is a free operating system provided by AWS. The default version is fine for this course.

In the Instance type section, keep the default t2.micro. This is a small, low-cost instance that is free tier eligible. The free tier gives you a limited amount of usage each month at no charge.

In the Key pair (login) section, click Create new key pair. A key pair is a set of two files: a private key that stays on your computer, and a public key that AWS puts on the instance. You use the private key to connect securely. Type a name for the key pair, for example my-key-pair. Keep the default key pair type RSA and the default private key file format .pem. Click Create key pair. Your browser downloads a file named my-key-pair.pem. Save this file in a safe place on your computer. You will need it every time you connect to the instance. Do not share it with anyone.

In the Network settings section, keep the default VPC and subnet. A VPC is a private network for your AWS resources. The default VPC is already set up for you. Keep the default setting to allow SSH traffic from anywhere. SSH is the protocol you will use to connect to the instance. In the next lesson, you will restrict this to only your own computer.

In the Configure storage section, keep the default 8 GiB gp2 root volume. This is the disk space for the instance.

Leave all other settings as they are. Click the orange Launch instance button. AWS starts creating your instance. Click View all instances to see it in the list.

Wait a minute or two. The instance state changes from Pending to Running. When it is running, you can connect to it.

Connect to the instance with SSH

SSH stands for Secure Shell. It is a way to run commands on a remote computer over an encrypted connection. To connect, you need the public IP address of your instance and the private key file you downloaded.

In the EC2 instance list, select your instance. In the details pane below, find the Public IPv4 address. It looks like 54.123.45.67. Copy this address.

Open a terminal on your computer. On Windows, you can use PowerShell or the Windows Terminal. On macOS or Linux, use the built-in terminal.

First, change to the directory where you saved the key file. For example, if you saved it in your Downloads folder:

cd ~/Downloads

Then change the permissions on the key file so that only you can read it. SSH refuses to use a key file that is too open. On macOS or Linux, run:

chmod 400 my-key-pair.pem

On Windows PowerShell, you can skip this step because file permissions work differently.

Now connect to the instance. The default user name for Amazon Linux is ec2-user. Run the following command, replacing 54.123.45.67 with your instance's public IP address:

ssh -i my-key-pair.pem [email protected]

The first time you connect, SSH asks if you want to continue connecting. Type yes and press Enter. You are now logged in to your EC2 instance. The prompt changes to something like [ec2-user@ip-172-31-12-34 ~]$.

You are now running commands on a computer in AWS's data centre. This computer is yours for as long as you keep the instance running.

Run a command on the instance

Let's check that the instance is working. Run this command to see the operating system version:

cat /etc/os-release

You will see lines like NAME="Amazon Linux" and VERSION="2023". The exact version may differ, but it is Amazon Linux.

Run this command to see how much memory the instance has:

free -h

The output shows total, used, and free memory. The t2.micro instance has 1 GiB of memory.

Run this command to see the disk space:

df -h

The output shows the root volume with about 8 GiB of space.

To log out of the instance, type exit and press Enter. You return to your local terminal.

Stop or terminate the instance

An EC2 instance costs money while it is running. The free tier gives you 750 hours per month of t2.micro usage, which is enough to run one instance continuously. But if you are not using the instance, you can stop it to save your free tier hours. Stopping an instance is like shutting down a computer: the disk is kept, and you can start it again later. Terminating an instance deletes it completely, including the disk.

To stop the instance, go to the EC2 console, select the instance, click Instance state, and choose Stop instance. Confirm. The instance state changes to Stopped. You can start it again later with Instance state > Start instance. Note that the public IP address may change when you start it again. You will need to use the new IP address to connect.

To terminate the instance, select it, click Instance state, and choose Terminate instance. Confirm. The instance is deleted after a short time. You will not be charged for a terminated instance.

For this course, keep the instance running so you can use it in the next lessons. If you stop it, remember to start it again before the next lesson.

How this maps to Azure and Google Cloud

On Azure, a virtual machine is created in the Azure portal. You choose an image, a size, and a key pair in a similar way. You connect with SSH using the same ssh command, but the default user name is often azureuser and the key file format is the same.

On Google Cloud, a Compute Engine instance is created in the Google Cloud Console. You choose a machine type, a boot disk, and a key pair. You connect with SSH using the gcloud compute ssh command or the browser-based SSH button. The default user name is your local user name.

The concepts are the same: you launch a virtual server, connect to it securely, and run commands. The names and console layouts differ, but the skills transfer.

In the next lesson, you will create an S3 bucket to store files for your app.

Key takeaways

  • An EC2 instance is a virtual server on AWS, equivalent to an Azure Virtual Machine or a Google Compute Engine instance.
  • You launch an instance from the AWS console by choosing an image, instance type, key pair, and network settings.
  • You connect to the instance with SSH using the private key file and the public IP address.
  • You can stop an instance to save free tier hours, or terminate it to delete it completely.
  • The default user for Amazon Linux is ec2-user, and the key file must have restricted permissions.

Try it

Launch a new EC2 instance using the same steps, but name it test-instance. Connect to it with SSH using its own public IPv4 address from the console, replacing the address in the ssh command, and run the command uname -a to see the kernel version. Then stop the instance and verify in the console that its state is Stopped. Finally, terminate the instance and confirm it disappears from the instance list. A good result is that you can connect successfully, see the kernel information, and the instance is gone after termination.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.