Amazon EC2 in Depth: Instances, AMIs, EBS and Pricing Models
Amazon Elastic Compute Cloud (EC2) provides resizable virtual servers – instances – that you launch in minutes and pay for per second.
Launching an instance – the ingredients
| Choice | Meaning |
|---|---|
| AMI (Amazon Machine Image) | Template with OS and software (Amazon Linux 2023, Ubuntu, Windows, your own golden image) |
| Instance type | CPU, memory, network – e.g. t3.micro, m7g.large |
| Key pair | SSH key (Linux) / password decryption (Windows) |
| Network | VPC, subnet (→ AZ), public IP, security group |
| Storage | Root EBS volume (size, type), extra volumes |
| IAM role (instance profile) | Credentials for code running on the instance |
| User data | Script run at first boot (install packages, start services) |
Instance families
| Family | Optimised for | Examples |
|---|---|---|
| General purpose | Balanced | t3, t4g (burstable), m7i, m7g |
| Compute optimised | CPU-heavy | c7i, c7g |
| Memory optimised | Large in-memory data | r7i, x2idn |
| Storage optimised | High local IOPS | i4i |
| Accelerated | GPU / ML | g5, p5, inf2, trn1 |
Name pattern: m7g.large = family m, generation 7, g = Graviton (ARM), size large. Graviton (ARM) instances usually give better price-performance.
Storage
| EBS | Instance store | |
|---|---|---|
| Persistence | Survives stop/start; independent of instance | Lost when instance stops or fails |
| Types | gp3 (general SSD), io2 (provisioned IOPS), st1/sc1 (HDD) |
Physically attached NVMe |
| Backups | Snapshots to S3 (incremental) → create AMIs or new volumes in any AZ | – |
| Scope | One AZ | One host |
Bootstrapping and metadata
#!/bin/bash
dnf install -y httpd
systemctl enable --now httpd
echo "<h1>Hello from $(hostname)</h1>" > /var/www/html/index.html
Code on the instance can read the Instance Metadata Service (IMDSv2) at http://169.254.169.254/latest/meta-data/ – instance ID, AZ, public IP and the temporary role credentials.
Security groups
A security group is a stateful virtual firewall on the instance’s network interface:
- Inbound rules allow traffic (all else is denied); there are no deny rules.
- Return traffic is allowed automatically (stateful).
- Sources can be CIDRs (
203.0.113.4/32) or other security groups (e.g. only the load balancer’s SG may reach port 80).
Opening SSH (22) to 0.0.0.0/0 exposes the server to the whole internet. Restrict it to your IP or use EC2 Instance Connect / Session Manager instead.
Lifecycle
stateDiagram-v2
[*] --> pending
pending --> running
running --> stopping
stopping --> stopped
stopped --> pending: start
running --> rebooting
rebooting --> running
running --> shutting_down
shutting_down --> terminated
stopped --> shutting_down
- Stopped EBS-backed instances keep their volumes (you pay for storage, not compute); the public IP changes on start unless you use an Elastic IP.
- Changing the instance type requires a stop (vertical scaling).
- Terminated instances are gone; root volumes are deleted by default.
Purchasing options
On-Demand · Savings Plans · Reserved Instances · Spot (up to 90 % cheaper, may be interrupted with 2-minute notice) · Dedicated Hosts (licensing/compliance).
Practise it
Virtual lab · Amazon Web Services · 25 min
Deploy a web server on Amazon EC2
Launch a Linux virtual server in the AWS console, open HTTP in its firewall, bootstrap Apache with user data and connect over EC2 Instance Connect – the classic IaaS deployment.
Quick revision
- Instance = AMI + type + network + storage + key + role + user data.
- Choose the family for the workload; Graviton saves cost.
- EBS persists and snapshots; instance store is temporary.
- Security groups are stateful allow-lists.
- Stop to resize; Elastic IP for a fixed address; Spot for cheap interruptible work.
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.