Trang chủ/Chương 15

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

FunctionMô tả
!RefTham chiếu parameter hoặc resource (trả về ID/value)
!GetAttLấy attribute của resource (VD: !GetAtt MyALB.DNSName)
!JoinNối strings
!SubSubstitute variables trong string
!SelectChọn phần tử từ list
Fn::ImportValueImport output từ stack khác

1.4 Các khái niệm quan trọng

Khái niệmMô tả
StackTập hợp resources tạo từ 1 template
StackSetDeploy stack vào nhiều accounts/regions cùng lúc
Change SetPreview changes trước khi update stack
Drift DetectionPhát hiện resources bị thay đổi thủ công (ngoài CloudFormation)
Nested StacksStack gọi stack khác (tái sử dụng template)
Cross-Stack RefStack 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

OptionDowntimeRollback
All at once✅ CóManual redeploy
RollingMinimalManual redeploy
Rolling with additional batch❌ KhôngManual redeploy
Immutable❌ KhôngTerminate new instances
Blue/Green❌ KhôngSwap URLs

3.3 Beanstalk Tiers

Web Server TierWorker Tier
Giao tiếpHTTP requestsSQS messages
ComponentsALB + EC2 ASGSQS Queue + EC2 ASG
Use caseWeb app / APIBackground 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 💡

  1. CloudFormation = IaC tiêu chuẩn AWS. Mọi thứ khai báo dưới dạng YAML/JSON.
  2. StackSets cho multi-account, multi-region deployment.
  3. Drift Detection khi bạn nghi resources bị sửa thủ công.
  4. Elastic Beanstalk khi đề nói "developer chỉ muốn deploy code, không muốn quản lý infra".
  5. CDK khi muốn dùng ngôn ngữ lập trình thay YAML.
  6. 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 ➡️