Amazon EC2
Virtual servers you control. Pick specs, launch in minutes.
What is EC2?
Virtual servers in the cloud. You pick the CPU, RAM, OS, and software. Full root access. Think: Renting a computer by the hour. Scale from 1 to 1,000 servers instantly.
Think of it like renting a computer
Pick your specs (CPU, RAM), choose your OS (Linux/Windows), and you're running in minutes. Pay only for what you use.
Key Features
General Purpose (M, T)
Balanced CPU + RAM. Good default choice.
Compute Optimized (C)
High CPU power. For batch jobs, encoding, gaming servers.
Memory Optimized (R, X)
Lots of RAM. For databases and caching.
Storage Optimized (I, D)
Fast local disks. For data warehouses.
GPU Instances (P, G)
Graphics cards. For ML training and rendering.
Graviton (ARM)
40% cheaper. Works for most workloads.
When to Use
- Need full server control
- Running Windows or specific OS
- Custom database setup
- GPU/ML training workloads
- Legacy apps that can't containerize
- Long-running background jobs
When Not to Use
- Short tasks under 15 min → Lambda
- Containerized apps → ECS/EKS/Fargate
- Static websites → S3 + CloudFront
- Managed databases → RDS
- Simple web apps → Elastic Beanstalk
- NoSQL needs → DynamoDB
Prerequisites
- An AWS account (free tier available)
- Basic understanding of Linux/Windows
- AWS CLI installed (optional but recommended)
AWS Console Steps
Open EC2 Dashboard
Navigate to EC2 in the AWS Console and click 'Launch Instance'
Choose an AMI
Select Amazon Linux 2023 (free tier eligible) for this tutorial
Select Instance Type
Choose t2.micro (free tier) or t3.micro for production
Configure Security Group
Allow SSH (port 22) from your IP address only
Create Key Pair
Download the .pem file - you'll need this to SSH into your instance
Launch & Connect
Click Launch, then connect via SSH using your key pair
AWS CLI Quickstart
Launch EC2 instance with AWS CLI
Create a new EC2 instance using the AWS CLI
# Launch a new EC2 instance
aws ec2 run-instances \
--image-id ami-0c55b159cbfafe1f0 \
--instance-type t3.micro \
--key-name my-key-pair \
--security-group-ids sg-xxxxxxxx \
--subnet-id subnet-xxxxxxxx \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyWebServer}]'
# Check instance status
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=MyWebServer" \
--query 'Reservations[].Instances[].{ID:InstanceId,State:State.Name,IP:PublicIpAddress}'This creates a t3.micro instance with the specified AMI, attaches it to your VPC, and tags it for easy identification.
First Project Ideas
- Host a simple web server with Nginx or Apache
- Set up a development environment with your favorite stack
- Create a bastion host for secure VPC access
- Deploy a WordPress blog
- Run a game server for Minecraft or Valheim
Pro Tips8
Start with t3 instances
performanceUse t3.micro for dev, t3.small/medium for prod. They're cheap and handle burst traffic well.
Start small, scale up when neededDon't over-provision from day oneSpot saves 90%
costSpot Instances cost 90% less than On-Demand. Use them for batch jobs, CI/CD, and dev environments.
Use Spot for stateless, interruptible workloadsDon't use Spot for databasesReserved for 24/7 workloads
costRunning instances 24/7 for a year? Reserved Instances save 30-72%. Analyze usage first.
Commit only after analyzing usage patternsDon't buy RIs for changing workloadsSecurity Groups are stateful
securitySecurity Groups auto-allow return traffic. Only define inbound rules for most cases.
Use Security Groups as primary firewallDon't open ports to 0.0.0.0/0Enable IMDSv2
securityIMDSv2 blocks SSRF attacks on metadata. Require it in production - no exceptions.
Configure instances to require IMDSv2Don't leave IMDSv1 enabledUse gp3 volumes
costgp3 is 20% cheaper than gp2 with better performance. No reason to use gp2 anymore.
Default to gp3 for all new volumesDon't use gp2 for new deploymentsUse IAM Roles, not keys
securityNever store credentials on instances. IAM Roles auto-rotate and are way more secure.
Attach IAM roles to instancesNever hardcode AWS credentialsAlways use Auto Scaling
reliabilityPut even single instances in an ASG with min=max=1. Auto-recovery if instance fails.
Use ASG for self-healingDon't run standalone instances in prodKey Facts8
Instance families: M (general), C (compute), R (memory), P/G (GPU), I/D (storage)
Higher numbers = newer generation.
defaultEBS max size: 64 TiB per volume, 28 volumes per Nitro instance
Use gp3 for best price/performance.
limitSecurity groups: Max 5 per interface, 60 rules each
Can increase via support request.
limitPlacement groups: Cluster (low latency), Spread (7/AZ max), Partition (distributed)
Cluster for HPC, Spread for HA.
behaviorSpot warning: 2-minute notice before termination
Check metadata or use CloudWatch Events.
behaviorInstance store: Data LOST on stop/terminate
Only for caches and temp data.
behaviorBilling: Per-second (60-second minimum)
Applies to On-Demand, Spot, and Reserved.
behaviorElastic IPs: Cost money when NOT attached
$0.005/hour (~$3.60/month) when unused.
limit