Chương 15: Infrastructure as Code (IaC)
Tổng Quan
Thay vì click trên AWS Console, IaC cho phép bạn viết code để tạo và quản lý hạ tầng. Kỳ thi SAA tập trung vào CloudFormation và Elastic Beanstalk.
1. AWS CloudFormation
1.1 Đặc điểm
- Viết template (YAML/JSON) mô tả tất cả tài nguyên AWS cần tạo.
- Deploy 1 lần → CloudFormation tạo toàn bộ resources theo đúng thứ tự dependencies.
- Xóa stack → tất cả resources bị xóa theo (clean up dễ dàng).
- Miễn phí (chỉ trả tiền resources được tạo).
1.2 Template Structure
AWSTemplateFormatVersion: "2010-09-09"
Description: "My infrastructure"
Parameters: # Input values (VD: instance type)
InstanceType:
Type: String
Default: t3.micro
Mappings: # Lookup table (VD: AMI per region)
Conditions: # Tạo resource có điều kiện
Resources: # ⚡ BẮT BUỘC - Khai báo AWS resources
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
Outputs: # Export values (VD: ALB DNS Name)
1.3 Intrinsic Functions thường gặp
| Function | Mô tả |
|---|---|
!Ref | Tham chiếu parameter hoặc resource (trả về ID/value) |
!GetAtt | Lấy attribute của resource (VD: !GetAtt MyALB.DNSName) |
!Join | Nối strings |
!Sub | Substitute variables trong string |
!Select | Chọn phần tử từ list |
Fn::ImportValue | Import output từ stack khác |
1.4 Các khái niệm quan trọng
| Khái niệm | Mô tả |
|---|---|
| Stack | Tập hợp resources tạo từ 1 template |
| StackSet | Deploy stack vào nhiều accounts/regions cùng lúc |
| Change Set | Preview changes trước khi update stack |
| Drift Detection | Phát hiện resources bị thay đổi thủ công (ngoài CloudFormation) |
| Nested Stacks | Stack gọi stack khác (tái sử dụng template) |
| Cross-Stack Ref | Stack A export output → Stack B import |
2. AWS CDK (Cloud Development Kit)
- Viết IaC bằng ngôn ngữ lập trình (TypeScript, Python, Java, C#).
- CDK code → compile → CloudFormation template → deploy.
- Ưu điểm: Dùng logic lập trình (loops, conditions), IDE support, testing.
- CDK có L1 (low-level), L2 (high-level), L3 (patterns) constructs.
3. AWS Elastic Beanstalk
3.1 Đặc điểm
- PaaS (Platform as a Service) — Bạn chỉ cần upload code.
- Beanstalk tự động tạo: EC2, ASG, ALB, RDS, CloudWatch... (bạn vẫn kiểm soát được).
- Hỗ trợ: Java, Node.js, Python, Ruby, Go, Docker, .NET, PHP.
- Miễn phí (chỉ trả tiền underlying resources).
3.2 Deployment Options
| Option | Downtime | Rollback |
|---|---|---|
| All at once | ✅ Có | Manual redeploy |
| Rolling | Minimal | Manual redeploy |
| Rolling with additional batch | ❌ Không | Manual redeploy |
| Immutable | ❌ Không | Terminate new instances |
| Blue/Green | ❌ Không | Swap URLs |
3.3 Beanstalk Tiers
| Web Server Tier | Worker Tier | |
|---|---|---|
| Giao tiếp | HTTP requests | SQS messages |
| Components | ALB + EC2 ASG | SQS Queue + EC2 ASG |
| Use case | Web app / API | Background processing |
4. AWS Systems Manager (SSM)
- Quản lý fleet of EC2 instances và on-premises servers.
- Run Command: Thực thi commands trên nhiều instances cùng lúc (không cần SSH).
- Patch Manager: Tự động patching OS.
- Session Manager: SSH không cần mở port 22, không cần SSH key, audit logs.
- Parameter Store: Lưu config/secrets (đã học ở Ch.13).
💡 Exam Tip: "Quản lý EC2 mà không cần SSH" → SSM Session Manager.
Exam Tips 💡
- CloudFormation = IaC tiêu chuẩn AWS. Mọi thứ khai báo dưới dạng YAML/JSON.
- StackSets cho multi-account, multi-region deployment.
- Drift Detection khi bạn nghi resources bị sửa thủ công.
- Elastic Beanstalk khi đề nói "developer chỉ muốn deploy code, không muốn quản lý infra".
- CDK khi muốn dùng ngôn ngữ lập trình thay YAML.
- SSM Session Manager thay thế SSH (bảo mật hơn, không cần mở port 22).
Câu Hỏi Ôn Tập 📝
Câu 1: Cần deploy cùng 1 infrastructure vào 20 accounts trong AWS Organization. Dùng gì?
Xem đáp án
CloudFormation StackSets. Cho phép deploy stack vào nhiều accounts/regions từ 1 management account.
Câu 2: Developer muốn deploy ứng dụng Node.js mà không cần học CloudFormation hay quản lý EC2. Dịch vụ nào?
Xem đáp án
AWS Elastic Beanstalk. Upload code → Beanstalk tự tạo và quản lý EC2, ALB, ASG, monitoring.
Câu 3: Cần chạy shell command trên 500 EC2 instances mà không SSH vào từng cái. Dùng gì?
Xem đáp án
AWS Systems Manager Run Command. Thực thi commands trên fleet of instances, không cần SSH, có audit logs.
⬅️ Chương 14: Monitoring & Logging | Chương 16: Data & Analytics ➡️