Amazon EC2
What is Amazon EC2?
EC2 provides resizable virtual servers — called instances — in the AWS cloud. Instead of buying physical hardware, you rent compute capacity by the second.
- Buy physical servers weeks or months in advance
- Pay for capacity you may not use
- Fixed location — no geographic flexibility
- You manage all hardware and OS
- Launch a server in seconds
- Pay only for what you use (per second)
- Deploy in any AWS Region globally
- Stop, resize, or terminate at any time
Service model: EC2 is Infrastructure as a Service (IaaS). AWS manages the physical hardware and hypervisor; you manage everything from the OS upward.
Instance types and families
An instance type defines the CPU, memory, storage, and network capacity of a virtual server.
Balanced CPU/memory
- Web servers
- Dev/test environments
- Small databases
t3, m6i, m7g
High CPU-to-memory ratio
- Batch processing
- Media transcoding
- High-traffic web apps
c6i, c7g
Large RAM workloads
- In-memory databases
- Real-time big data
- SAP HANA
r6i, x2idn
Type names encode the family and generation: m6i.xlarge = general purpose (m), 6th gen (6), Intel (i), extra large. GPU families include p4 and g5.
Amazon Machine Images (AMIs)
An AMI is a pre-configured template that includes the OS, installed software, and configuration needed to launch an instance.
What an AMI contains
- Operating system (Amazon Linux, RHEL, Ubuntu, Windows…)
- Application server and software packages
- Data volumes to attach at launch
- Launch permissions (who can use it)
You can use AWS-provided AMIs, Marketplace AMIs, or create your own from a running instance — useful for baking in your configuration.
Launch sequence
- Choose an AMI
- Select an instance type
- Configure network (VPC, subnet)
- Add storage (EBS volumes)
- Configure security group (firewall rules)
- Add a key pair for SSH access
- Launch
EC2 storage options
- Persistent block storage — survives instance stop/start
- Attach/detach like a virtual hard drive
- SSD (gp3, io2) or HDD (st1, sc1)
- Snapshots to S3 for backup
Most common choice for OS and data volumes
- Physically attached to host — very fast
- Ephemeral — data lost when instance stops
- Good for temporary data, caches, scratch space
- Not available on all instance types
High IOPS, low latency use cases
- EFS — shared NFS file system, mount on multiple instances simultaneously
- S3 — object storage, accessed via API (not mounted as a file system)
Shared or archival data
The root volume is typically EBS-backed, which is why an EC2 instance can be stopped and restarted without losing the OS.
EC2 pricing models
| Model | How it works | Best for | Savings vs On-Demand |
|---|---|---|---|
| On-Demand | Pay by the second, no commitment | Short-term, unpredictable workloads | — |
| Reserved Instances | 1- or 3-year commitment | Steady-state production workloads | Up to 72% |
| Savings Plans | Commit to $/hr spend, flexible on instance type | Flexible workloads across instance families | Up to 66% |
| Spot Instances | Bid on spare AWS capacity — can be interrupted | Fault-tolerant batch jobs, CI/CD runners | Up to 90% |
| Dedicated Hosts | Physical server dedicated to you | Compliance, bring-your-own-license (BYOL) | Premium |
Exam tip: Spot Instances offer the deepest discount but can be reclaimed by AWS with 2 minutes' notice — never use them for stateful workloads without a checkpoint strategy.
Security groups and networking
Every EC2 instance lives inside a VPC (Virtual Private Cloud) and is protected by a security group.
Security groups
- Stateful virtual firewall at the instance level
- Rules are allow-only — no explicit deny
- Inbound and outbound rules defined by protocol, port, and source/destination
- Multiple security groups can be applied to one instance
Default: all inbound blocked, all outbound allowed. Open only what is needed — principle of least privilege.
Key networking concepts
- Public subnet — instances can have a public IP and reach the internet via Internet Gateway
- Private subnet — no direct internet access; use NAT Gateway for outbound
- Elastic IP — static public IPv4 address you can reassign between instances
- ENI — Elastic Network Interface; a virtual NIC
EC2 in the AWS compute landscape
EC2 is the foundational compute layer that many other AWS services build on top of.
Key takeaways
- EC2 = IaaS — you get a virtual server; AWS manages the physical infrastructure
- Instance types are selected to match workload requirements: CPU, memory, GPU, storage
- AMIs are the launch templates — pre-bake your environment to ensure consistency
- EBS provides persistent storage; instance store is ephemeral
- Security groups are stateful, allow-only firewalls at the instance level
EC2 is a virtual machine rental service. You choose the spec, pick an OS image, and AWS runs it on their hardware — billed by the second.
EC2 is the foundation of AWS compute. Understanding it unlocks ECS, EKS, Beanstalk, and Auto Scaling — all build on top of it.
Review questions
- What is the difference between an AMI and an instance type?
- Which storage type is lost when an EC2 instance is stopped?
- What pricing model offers the deepest discount but can be interrupted?
- What service model does EC2 represent — IaaS, PaaS, or SaaS?
- A startup needs to run a web server for 3 months during a product launch. Which pricing model is most appropriate?
- A team runs batch ML training jobs that can restart from a checkpoint if interrupted. Which pricing model would minimize cost?
- You need to run a containerized app without managing servers. Should you use EC2 directly?
Next: Amazon EC2 Auto Scaling — how to automatically adjust the number of instances based on demand.