AWS Tutorial menu

AWS EC2 Virtual Servers

EC2 (Elastic Compute Cloud) lets you rent virtual servers, called instances, in the AWS cloud.

What is an EC2 Instance?

An instance is a virtual computer running in an AWS data centre. It has a CPU, memory, a disk and a network connection, just like a laptop, but you control it over the internet.

You can use an instance to:

  • Host a website or an API
  • Run a database or a game server
  • Process data or run scheduled jobs

Key Parts of EC2

PartWhat it is
AMI (Amazon Machine Image)The template with the operating system, such as Amazon Linux or Ubuntu
Instance typeThe size: number of CPUs and amount of memory, e.g. t3.micro
EBS volumeThe virtual hard disk attached to the instance
Key pairThe key file you use to log in securely with SSH
Security groupA firewall that decides which traffic may reach the instance

Instance Types

Instance type names follow a pattern. In t3.micro:

  • t is the family (t = general purpose, burstable)
  • 3 is the generation
  • micro is the size (nano, micro, small, medium, large, xlarge...)

Bigger sizes cost more per hour. For learning, use a free tier eligible size.

Launching an Instance in the Console

  1. Open the EC2 service and click Launch instance
  2. Give it a name, for example karachi-shop-web
  3. Choose an AMI such as Amazon Linux 2023
  4. Choose the instance type t3.micro (or t2.micro)
  5. Create a new key pair and download the .pem file
  6. In network settings, allow SSH from My IP and allow HTTP from anywhere
  7. Click Launch instance

After a minute the instance state changes to Running and it gets a public IP address.

Launching with the CLI

The same thing from the command line:

Example

bash
aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type t3.micro \
  --key-name ali-key \
  --security-group-ids sg-0123456789abcdef0 \
  --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=karachi-shop-web}]"

Then list your running instances:

Example

bash
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" \
  --query "Reservations[].Instances[].[InstanceId,PublicIpAddress]" --output table
Output
----------------------------------------
|           DescribeInstances          |
+----------------------+---------------+
|  i-0a1b2c3d4e5f67890 |  13.233.10.25 |
+----------------------+---------------+

Connecting with SSH

On Linux, macOS or Windows (PowerShell), use your key file to log in:

Example

bash
chmod 400 ali-key.pem
ssh -i ali-key.pem ec2-user@13.233.10.25

For Ubuntu AMIs, the user name is ubuntu instead of ec2-user.

Hosting a Simple Web Page

Once connected, install a web server and create a page:

Example

bash
sudo dnf install -y httpd
sudo systemctl start httpd
echo "<h1>Welcome to Ali's Shop</h1>" | sudo tee /var/www/html/index.html
curl http://localhost
Output
<h1>Welcome to Ali's Shop</h1>

Now open http://13.233.10.25 (your public IP) in a browser and you will see the page.

Security Groups

A security group is a list of inbound rules (traffic allowed in). By default, nothing is allowed in.

TypePortSourceWhy
SSH22My IPSo only you can log in
HTTP800.0.0.0/0So anyone can see the website
HTTPS4430.0.0.0/0For secure websites

Never open SSH (port 22) to 0.0.0.0/0, which means the whole internet. Allow it only from your own IP address.

Stop vs Terminate

  • Stop: the instance shuts down. You stop paying for compute, but still pay a little for the disk. You can start it again.
  • Terminate: the instance is deleted for ever, with its disk.

Example

bash
aws ec2 terminate-instances --instance-ids i-0a1b2c3d4e5f67890

Exercise

Exercise

Which EC2 feature works like a firewall and controls which ports are open?

Learn AWS with a teacher

Join the CIT (Computer Information Technology) course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp