Course Lessons
Lesson 2 of 4
EC2: Elastic Compute Cloud
Master EC2 virtual servers including instance types, security groups, key pairs, and how to launch and manage cloud servers for your applications.
35 minutes
EC2: Elastic Compute Cloud
Amazon EC2 provides resizable compute capacity in the cloud. It's like renting virtual servers that you can configure and manage yourself.
What is EC2?
- Virtual Servers: Run applications on virtual machines
- Flexible Configuration: Choose CPU, memory, storage, and networking
- Pay Per Use: Only pay for the compute time you use
- Quick Deployment: Launch instances in minutes
EC2 Instance Types
AWS offers different instance families optimized for various use cases:
General Purpose (T3, T4g, M5, M6i)
- Balanced compute, memory, and networking
- Great for web servers and small databases
- Example: t3.micro, t3.medium, m5.large
Compute Optimized (C5, C6i, C7g)
- High-performance processors
- Ideal for batch processing, web servers, gaming
- Example: c5.xlarge, c6i.2xlarge
Memory Optimized (R5, R6i, X2)
- High memory-to-CPU ratio
- Perfect for databases and in-memory caching
- Example: r5.large, r6i.xlarge
Storage Optimized (I3, D2, H1)
- High sequential read/write access
- Ideal for data warehousing and log processing
- Example: i3.large, d2.xlarge
Key EC2 Concepts
AMIs (Amazon Machine Images)
- Pre-configured templates for instances
- Include OS, applications, and configurations
- Can create custom AMIs from existing instances
Security Groups
- Virtual firewalls for instances
- Control inbound and outbound traffic
- Stateful - return traffic is automatically allowed
Key Pairs
- Public-key cryptography for SSH access
- Keep private key secure
- Used to connect to Linux instances
Instance States
- Running: Instance is active and billing
- Stopped: Instance is shut down, only storage billed
- Terminated: Instance is deleted permanently
Launching an EC2 Instance
- Choose an AMI (Amazon Linux, Ubuntu, etc.)
- Select instance type based on your needs
- Configure instance details (network, IAM role)
- Add storage (EBS volumes)
- Add tags for organization
- Configure security group
- Review and launch with key pair
Best Practices
- Use IAM roles instead of embedding credentials
- Enable detailed monitoring for production instances
- Use Elastic IPs for static IP addresses
- Regular backups with AMIs or snapshots
- Right-size instances based on actual usage
- Use Auto Scaling for high availability
Code Example
# Launch an EC2 instance using AWS CLI
# Create a security group
aws ec2 create-security-group \
--group-name web-server-sg \
--description "Security group for web server"
# Add inbound rules to security group
aws ec2 authorize-security-group-ingress \
--group-name web-server-sg \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0 # SSH access
aws ec2 authorize-security-group-ingress \
--group-name web-server-sg \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0 # HTTP access
# Create a key pair
aws ec2 create-key-pair \
--key-name my-key-pair \
--query 'KeyMaterial' \
--output text > my-key-pair.pem
chmod 400 my-key-pair.pem
# Launch an EC2 instance
aws ec2 run-instances \
--image-id ami-0c55b159cbfafe1f0 \
--instance-type t3.micro \
--key-name my-key-pair \
--security-groups web-server-sg \
--count 1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyWebServer}]'
# List running instances
aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[*].Instances[*].[InstanceId,InstanceType,PublicIpAddress,State.Name]' \
--output table
# Connect to instance via SSH
ssh -i my-key-pair.pem ec2-user@<PUBLIC_IP_ADDRESS>
# Stop an instance
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
# Start an instance
aws ec2 start-instances --instance-ids i-1234567890abcdef0
# Terminate an instance
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
# Create an AMI from a running instance
aws ec2 create-image \
--instance-id i-1234567890abcdef0 \
--name "MyWebServer-Backup" \
--description "Backup of web server"