Amazon EC2 Basics
Virtual servers in the cloud: instances, AMIs, instance types, and how the hypervisor shares hardware.
What is it?
Amazon EC2 (Elastic Compute Cloud) rents you virtual servers called instances. You choose the operating system, the size, and the network placement; you manage everything from the OS upward.
- AMI (Amazon Machine Image): a template containing an operating system and optionally software. Launching an instance means 'start a server from this image'.
- Instance type: the hardware shape, written like
m7g.large- family and generation (m7g) plus size (large). - Key pair / Systems Manager: ways to log in securely.
- User data: a script that runs on first boot to configure the server.
Instance families group types by what they are good at:
- General purpose (M, T): balanced CPU/memory, the usual starting point. T types are burstable.
- Compute optimized (C): batch processing, game servers, high-performance web.
- Memory optimized (R, X): in-memory databases and large caches.
- Storage optimized (I, D): high local disk throughput, data warehouses.
- Accelerated computing (P, G, Inf, Trn): GPUs and ML chips for training, inference, graphics.
Many instances run on the same physical host - this is multi-tenancy. A hypervisor (AWS uses the Nitro system) divides the host's CPU, memory, and storage and keeps each tenant isolated.
Explain like I'm 10
A physical server is a large apartment building. The hypervisor is the building manager who divides it into separate flats with locked doors: each tenant gets their own space and cannot peek into the neighbor's. An AMI is the furnished floor plan you can copy into any new flat.
Examples
Launch an instance
aws ec2 run-instances \
--image-id ami-0123456789abcdef0 \
--instance-type t3.micro \
--count 1 \
--key-name my-key \
--security-group-ids sg-0abc1234 \
--subnet-id subnet-0abc1234 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'
aws ec2 describe-instances \
--filters Name=tag:Name,Values=web-1 \
--query "Reservations[].Instances[].[InstanceId,State.Name]" --output table
aws ec2 terminate-instances --instance-ids i-0123456789abcdef0Replace the placeholder IDs with ones from your account. Terminate when done so billing stops.
User data bootstrap script
#!/bin/bash
dnf install -y nginx
echo "hello from $(hostname)" > /usr/share/nginx/html/index.html
systemctl enable --now nginxRuns once at first boot, so a fresh instance configures itself.
How it works
When you launch, EC2 picks a physical host in the AZ that fits the instance type, attaches a root volume built from the AMI, and starts the VM. Instances go through states: pending, running, stopping/stopped, and terminated. You pay for compute while running; stopped instances stop compute billing, but attached EBS volumes still cost money.
Storage can be an EBS network volume (persists independently) or an instance store (physically attached, lost when the instance stops or terminates). Networking is placed inside a VPC subnet and filtered by security groups.
AMI (OS + software)
|
v launch
+--------------- Physical host (one AZ) ---------------+
| Hypervisor (Nitro) |
| +-----------+ +-----------+ +-----------+ |
| | Instance A| | Instance B| | Instance C| <-- separate customers
| +-----------+ +-----------+ +-----------+ |
+-------------------------------------------------------+Why does it exist?
Plenty of software expects a normal server: a particular OS, installed packages, long-running processes. EC2 gives you that control with the speed and billing model of the cloud, which is useful when you cannot or do not want to rewrite for more managed services.
When to use it
Use EC2 for lifted-and-shifted applications, software needing OS-level control, custom networking or licensing, long-running servers, and specialized hardware like GPUs.
When not to use it
If you only need to run short event-driven code, Lambda is simpler. If you run containers, ECS/EKS with Fargate removes server management. If a managed database fits, use RDS rather than running a database yourself on EC2.
Common mistakes
Leaving instances running after experiments.
Opening SSH to the entire internet rather than restricting the source.
Storing important data on instance store volumes.
Oversizing 'just in case' instead of right-sizing from metrics.
Treating servers as pets that cannot be rebuilt; bake an AMI or use user data so you can replace them.
Practice exercises
- Easy:
Decode the instance type
c7g.xlarge: what do the letter, number, extra letter and size tell you? - Easy:
Pick an instance family for (a) a large in-memory cache, (b) video encoding, (c) a small blog.
- Medium:
Write user data that installs a web server and serves a page showing the instance's hostname.
- Hard:
Explain what is lost, kept, and billed when you stop vs terminate an instance with one EBS root volume and one instance-store volume.
Interview questions
What is an AMI?
A template with an operating system and optional software used to launch EC2 instances.
What does multi-tenancy mean for EC2?
Multiple customers' instances share one physical host, kept isolated by the hypervisor.
Which instance family suits memory-intensive workloads?
Memory optimized (R and X families).
Exam-style: A company needs full control of the OS for a legacy application. Which compute service fits best?
Amazon EC2.
Exam-style: Who is responsible for patching the operating system on an EC2 instance?
The customer; AWS manages the underlying host and hypervisor.