Amazon EC2 - Elastic Compute Cloud
Tổng quan
Amazon Elastic Compute Cloud (EC2) là dịch vụ web cung cấp khả năng compute có thể thay đổi quy mô trong cloud. EC2 giảm thời gian khởi tạo và boot server xuống còn vài phút, cho phép bạn nhanh chóng scale capacity lên hoặc xuống theo yêu cầu.
Khái niệm cơ bản
Instance Types
General Purpose
- T4g, T3, T3a, T2: Burstable performance instances
- M6i, M5, M5a, M4: Balanced compute, memory, và networking
- A1: ARM-based processors với performance/cost tối ưu
Compute Optimized
- C6i, C5, C5n, C4: High-performance processors
- Ideal cho: CPU-intensive applications, scientific modeling, gaming servers
Memory Optimized
- R6i, R5, R5a, R4: Memory-intensive applications
- X1e, X1: High memory instances
- z1d: High frequency và NVMe SSD storage
Storage Optimized
- I3, I3en: NVMe SSD-backed instance storage
- D2, D3: Dense HDD storage
- H1: High disk throughput
Accelerated Computing
- P4, P3: GPU instances cho machine learning
- G4: GPU instances cho graphics workloads
- F1: FPGA instances
Instance Lifecycle
Pending → Running → Stopping → Stopped → Terminating → Terminated
↓ ↓ ↓ ↓
Rebooting Shutting-down Starting
States chi tiết:
- Pending: Instance đang được khởi tạo
- Running: Instance đang chạy và có thể sử dụng
- Stopping: Instance đang shutdown
- Stopped: Instance đã shutdown, EBS volumes vẫn attached
- Terminating: Instance đang bị xóa
- Terminated: Instance đã bị xóa hoàn toàn
Pricing Models
On-Demand Instances
- Đặc điểm: Pay per hour/second, không commitment
- Use cases: Development, testing, unpredictable workloads
- Ưu điểm: Flexibility cao
- Nhược điểm: Chi phí cao nhất
Reserved Instances (RI)
- Standard RI: Discount lên đến 75%, không thể modify
- Convertible RI: Discount lên đến 54%, có thể change instance family
- Scheduled RI: Reserve cho specific time windows
- Payment options: All upfront, Partial upfront, No upfront
Spot Instances
- Đặc điểm: Bid cho unused EC2 capacity
- Discount: Lên đến 90% so với On-Demand
- Risk: Có thể bị interrupt với 2-minute notice
- Use cases: Fault-tolerant, flexible start/end time
Dedicated Hosts
- Đặc điểm: Physical server dedicated cho customer
- Use cases: Compliance requirements, server-bound licenses
- Pricing: Per-host billing
Dedicated Instances
- Đặc điểm: Instances chạy trên hardware dedicated
- Khác với Dedicated Hosts: Không control placement
- Use cases: Compliance cần physical isolation
Storage Options
Instance Store
- Đặc điểm: Temporary storage, high IOPS
- Persistence: Data mất khi instance stop/terminate
- Use cases: Caching, temporary processing
Amazon EBS (Elastic Block Store)
- Persistent: Data persist độc lập với instance lifecycle
- Types:
- gp3/gp2: General Purpose SSD
- io2/io1: Provisioned IOPS SSD
- st1: Throughput Optimized HDD
- sc1: Cold HDD
EFS (Elastic File System)
- Đặc điểm: Fully managed NFS
- Scaling: Automatic scaling
- Access: Multiple instances concurrently
Networking
VPC Integration
- Subnets: Public, Private, Database subnets
- Security Groups: Stateful firewall
- NACLs: Stateless subnet-level firewall
- Route Tables: Control traffic routing
IP Addressing
- Private IP: Assigned from subnet CIDR
- Public IP: Dynamic, changes on stop/start
- Elastic IP: Static public IP address
Enhanced Networking
- SR-IOV: Single Root I/O Virtualization
- Placement Groups:
- Cluster: High network performance
- Partition: Large distributed applications
- Spread: Distinct underlying hardware
Security
Key Pairs
- Function: SSH access cho Linux instances
- Components: Public key (AWS), Private key (user)
- Best Practice: Unique key pairs cho different environments
Security Groups
Type Protocol Port Range Source
SSH TCP 22 My IP
HTTP TCP 80 0.0.0.0/0
HTTPS TCP 443 0.0.0.0/0
MySQL TCP 3306 sg-database
IAM Roles
- Instance Profiles: Attach IAM roles to EC2
- Benefits: No hardcoded credentials
- Rotation: Automatic credential rotation
Systems Manager
- Session Manager: Browser-based shell access
- Parameter Store: Secure storage cho configuration
- Patch Manager: Automated patching
Monitoring và Management
CloudWatch Metrics
Basic Monitoring (5-minute intervals)
- CPUUtilization
- NetworkIn/NetworkOut
- DiskReadOps/DiskWriteOps
Detailed Monitoring (1-minute intervals)
- Additional cost
- More granular data
CloudWatch Logs
- Agent: Collect logs from instances
- Log Groups: Organize logs
- Retention: Configurable retention periods
AWS Config
- Configuration tracking: Monitor instance changes
- Compliance: Check against rules
- History: Configuration change history
Auto Scaling
Auto Scaling Groups (ASG)
Auto Scaling Group:
MinSize: 2
MaxSize: 10
DesiredCapacity: 4
LaunchTemplate: my-template
AvailabilityZones: [us-west-2a, us-west-2b]
Launch Templates
- Next generation: Replace Launch Configurations
- Features: Multiple versions, mixed instance types
- Benefits: More configuration options
Scaling Policies
- Target Tracking: Maintain specific metric value
- Step Scaling: Scale based on CloudWatch alarms
- Simple Scaling: Basic scaling actions
High Availability và Disaster Recovery
Multi-AZ Deployment
Region: us-west-2
├── AZ-2a: Web Servers
├── AZ-2b: Web Servers
└── AZ-2c: Web Servers
Elastic Load Balancer Integration
- Application Load Balancer: Layer 7 routing
- Network Load Balancer: Layer 4, high performance
- Classic Load Balancer: Legacy, basic load balancing
Backup Strategies
- EBS Snapshots: Point-in-time backups
- AMI Creation: Instance-level backups
- Cross-Region: Copy AMIs/snapshots across regions
Best Practices
Security
- Regular Updates: Keep OS và applications updated
- Least Privilege: Minimal required permissions
- Network Segmentation: Use security groups effectively
- Encryption: Encrypt data at rest và in transit
Performance
- Right Sizing: Monitor usage và adjust instance types
- Placement Groups: Use cho high-performance computing
- Enhanced Networking: Enable cho supported instances
- Storage Optimization: Choose appropriate EBS types
Cost Optimization
- Reserved Instances: Plan cho predictable workloads
- Spot Instances: Use cho fault-tolerant workloads
- Instance Scheduling: Stop instances when not needed
- Resource Tagging: Track costs effectively
Management
- Automation: Use Systems Manager, CloudFormation
- Monitoring: Set up CloudWatch alarms
- Documentation: Maintain instance inventory
- Change Management: Track configuration changes
Common Use Cases
Web Applications
Architecture:
Internet Gateway → ALB → EC2 Instances (Multi-AZ) → RDS
Batch Processing
Architecture:
S3 → Lambda → EC2 Spot Instances → Results to S3
High Performance Computing
Architecture:
Cluster Placement Group → Compute Optimized Instances → EFS
Troubleshooting
Common Issues
- Instance won't start
- Check security group rules
- Verify subnet configuration
-
Check instance limits
-
Performance issues
- Monitor CloudWatch metrics
- Check network bandwidth
-
Verify storage performance
-
Connection problems
- Verify security groups
- Check NACLs
- Confirm route tables
Diagnostic Tools
- Instance Connect: Browser-based SSH
- Serial Console: Direct console access
- VPC Flow Logs: Network traffic analysis
- CloudTrail: API call logging
Exam Tips cho AWS SAA
Key Points
- Instance Types: Understand use cases cho each family
- Pricing Models: Know when to use each model
- Storage: Differences between instance store và EBS
- Networking: Security groups vs NACLs
- Auto Scaling: Components và strategies
Common Questions
- Which instance type cho specific workload?
- How to achieve high availability?
- Cost optimization strategies
- Security best practices
- Monitoring và troubleshooting
Best Practices for Exam
- Practice: Hands-on experience với EC2
- Scenarios: Understand real-world use cases
- Integration: Know how EC2 works với other services
- Troubleshooting: Common problems và solutions