EC2 Basics
Table of Contents
Amazon EC2 (Elastic Compute Cloud) is AWS’s core compute service. It lets you run virtual servers — called instances — in the cloud. Instead of buying and maintaining physical hardware, you can launch a server in minutes, use it for as long as you need, and shut it down when you’re done.
If you’ve been through the AWS Getting Started guide, you already have an AWS account. In this tutorial, we’ll launch an EC2 instance, connect to it via SSH, run a simple web server, and then clean everything up so you don’t get charged.
t2.micro or t3.micro instances for the first 12 months. We’ll use a Free Tier eligible instance in this tutorial, but always stop or terminate instances when you’re done.
Key Concepts
Before launching anything, let’s cover the terminology:
- Instance — a virtual server running in AWS
- AMI (Amazon Machine Image) — a template that defines the operating system and pre-installed software for your instance
- Instance Type — the hardware configuration (CPU, memory, network).
t2.microis the Free Tier option - Key Pair — an SSH key used to securely connect to your instance
- Security Group — a virtual firewall that controls inbound and outbound traffic
- EBS (Elastic Block Store) — the disk storage attached to your instance
Launching an Instance
Step 1: Open the EC2 Console
Sign in to the AWS Management Console and navigate to EC2 (search for it in the top search bar). Click Launch instance.
Step 2: Configure the Instance
Give your instance a name (e.g., “my-first-instance”) and configure these settings:
AMI: Select Amazon Linux 2023 (Free Tier eligible). This is a lightweight Linux distribution maintained by AWS.
Instance type: Choose t2.micro (or t3.micro depending on your region). Both are Free Tier eligible and provide 1 vCPU and 1 GB of memory.
Key pair: Click Create new key pair. Give it a name like my-key, select RSA and .pem format, and download the file. You’ll need this to SSH into the instance.
.pem) somewhere safe. You can’t download it again. If you lose it, you’ll need to create a new key pair and launch a new instance.
Network settings: Click Edit and configure the security group:
- Allow SSH (port 22) from “My IP” — this lets you connect from your current location
- Allow HTTP (port 80) from “Anywhere” — we’ll use this to test a web server
Storage: The default 8 GB gp3 volume is fine for this tutorial.
Step 3: Launch
Click Launch instance. AWS will provision the virtual server, which typically takes under a minute. Click the instance ID to go to the instance details page and wait for the Instance state to show Running.
Connecting via SSH
Once your instance is running, find its Public IPv4 address on the instance details page.
If the connection is successful, you’ll see a prompt like:
, #_
~\_ ####_ Amazon Linux 2023
~~ \_#####\
~~ \###|
~~ \#/ ___
~~ V~' '->
~~~ /
~~._. _/
_/ _/
_/m/'
[ec2-user@ip-172-31-xx-xx ~]$
You’re now logged into your EC2 instance.
Running a Web Server
Let’s run a simple web server to verify everything works end-to-end. We’ll use Python’s built-in HTTP server since Python comes pre-installed on Amazon Linux:
# Create a simple HTML page
echo '<h1>Hello from EC2!</h1><p>This page is served from an AWS instance.</p>' > index.html
# Start a web server on port 80
sudo python3 -m http.server 80
Now open your browser and go to http://<your-public-ip>. You should see “Hello from EC2!” displayed on the page.
Press Ctrl+C in the terminal to stop the server.
sudo because port 80 is a privileged port on Linux (ports below 1024 require root access). For a production setup, you’d use a proper web server like Nginx or Apache, or run your application behind a reverse proxy.
Instance Lifecycle
EC2 instances have several states:
| State | Description | Billing |
|---|---|---|
| Running | Instance is active and accessible | Charged |
| Stopped | Instance is shut down but preserved | Not charged for compute (EBS storage still charged) |
| Terminated | Instance is permanently deleted | Not charged |
Stopping vs Terminating
- Stop an instance when you want to pause it and come back later. Your data on the EBS volume is preserved, and you can restart it anytime. You won’t be charged for compute while it’s stopped, but you’ll still pay for the attached storage.
- Terminate an instance when you’re done with it. This deletes the instance and (by default) its storage. It can’t be undone.
To stop or terminate from the console: select your instance → Instance state → Stop instance or Terminate instance.
From the CLI:
# Stop
aws ec2 stop-instances --instance-ids i-0123456789abcdef0
# Terminate
aws ec2 terminate-instances --instance-ids i-0123456789abcdef0
Security Groups
Security groups act as a firewall for your instance. Each rule specifies a protocol, port range, and source/destination.
The security group we created allows:
- Inbound SSH (port 22) from your IP — so you can connect
- Inbound HTTP (port 80) from anywhere — so the web server is publicly accessible
- All outbound traffic — the default, so your instance can reach the internet
0.0.0.0/0 (anywhere) in a real environment. Restrict it to your IP address or use AWS Systems Manager Session Manager for a more secure alternative that doesn’t require opening any inbound ports.
Cleaning Up
To avoid unexpected charges, terminate the instance and delete the security group:
- Go to EC2 → Instances, select your instance, and choose Terminate instance
- Go to EC2 → Security Groups, select the security group you created, and delete it (you can’t delete the default security group, and that’s fine)
Verify the instance state shows Terminated. AWS will automatically clean it up after a short time.
What’s Next
You’ve launched an EC2 instance, connected to it, and served a web page. From here, you can explore S3 for static hosting if you haven’t already, or look into AWS Lambda for running code without managing servers at all.