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
| Part | What it is |
|---|---|
| AMI (Amazon Machine Image) | The template with the operating system, such as Amazon Linux or Ubuntu |
| Instance type | The size: number of CPUs and amount of memory, e.g. t3.micro |
| EBS volume | The virtual hard disk attached to the instance |
| Key pair | The key file you use to log in securely with SSH |
| Security group | A firewall that decides which traffic may reach the instance |
Instance Types
Instance type names follow a pattern. In t3.micro:
tis the family (t = general purpose, burstable)3is the generationmicrois 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
- Open the EC2 service and click Launch instance
- Give it a name, for example
karachi-shop-web - Choose an AMI such as Amazon Linux 2023
- Choose the instance type
t3.micro(ort2.micro) - Create a new key pair and download the
.pemfile - In network settings, allow SSH from My IP and allow HTTP from anywhere
- 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
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
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" \
--query "Reservations[].Instances[].[InstanceId,PublicIpAddress]" --output table---------------------------------------- | DescribeInstances | +----------------------+---------------+ | i-0a1b2c3d4e5f67890 | 13.233.10.25 | +----------------------+---------------+
Connecting with SSH
On Linux, macOS or Windows (PowerShell), use your key file to log in:
Example
chmod 400 ali-key.pem
ssh -i ali-key.pem ec2-user@13.233.10.25For 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
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<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.
| Type | Port | Source | Why |
|---|---|---|---|
| SSH | 22 | My IP | So only you can log in |
| HTTP | 80 | 0.0.0.0/0 | So anyone can see the website |
| HTTPS | 443 | 0.0.0.0/0 | For 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
aws ec2 terminate-instances --instance-ids i-0a1b2c3d4e5f67890Exercise
Exercise
Which EC2 feature works like a firewall and controls which ports are open?
