Servisphere
Biểu tượng AWS CloudFormation (AWS Architecture Icons)

AWS CloudFormation

Hạ tầng bằng code

Mô tả hạ tầng bằng template YAML/JSON; CloudFormation tạo, cập nhật, xoá toàn bộ tài nguyên theo template (stack).

  • Chi phí$ Rẻ
  • Độ khó●●●●●
  • Để không có tốn tiền?Không
  • AWS quản lý
  • Có trong đềSAA · DVA · SOA

Phạm vi: Regional

Mở CloudFormation trên bản đồ →
Hình dungNhư bản vẽ thi công: đưa bản vẽ là xây ra đúng toà nhà, xây lại bao nhiêu lần cũng giống.

CloudFormation đứng ở đâu trong hệ thống?

CloudFormation

Xem toàn bộ trên bản đồ →

Một ví dụ thật, từng bước

  1. Mỗi lần dựng môi trường test, đội phải bấm tay hàng chục bước trên console và lần nào cũng thiếu một thứ.
  2. Bạn viết template YAML mô tả VPC, ALB, Auto Scaling group và database.
  3. Bạn tạo stack từ template; CloudFormation tự tạo tài nguyên theo đúng thứ tự phụ thuộc.
  4. Muốn đổi cỡ máy, bạn sửa template, xem change set để biết cái gì sẽ thay đổi rồi mới áp dụng.
  5. Có lỗi giữa chừng thì stack tự rollback; xoá stack là dọn sạch mọi thứ, riêng database có DeletionPolicy Retain thì giữ lại.

Khi nào nên dùng

✓ Nên dùng khi

Tạo môi trường lặp lại được (dev/staging/prod), review hạ tầng qua code.

✗ Không nên dùng khi

Thích viết bằng ngôn ngữ lập trình → CDK (vẫn sinh ra CloudFormation).

Ví dụ hay gặp

  • Tạo môi trường dev / staging / prod giống hệt nhau
  • Hạ tầng bằng code, review qua pull request
  • Triển khai serverless bằng SAM hoặc CDK
  • StackSets cho nhiều tài khoản và Region

CloudFormation, Systems Manager hay Organizations?

CloudFormationSystems ManagerOrganizations
Làm gìTạo và cập nhật hạ tầng từ templateVận hành máy: chạy lệnh, vá lỗi, truy cậpQuản lý nhiều tài khoản AWS
Đối tượngTài nguyên AWS (VPC, EC2, RDS…)Máy EC2 và máy on-premisesTài khoản và chính sách
Lặp lại đượcCó, cùng template ra cùng hạ tầngCó, qua document và runbookCó, qua OU và SCP
PhíMiễn phí với tài nguyên AWSPhần lớn miễn phíMiễn phí

Tốn bao nhiêu tiền?

Ví dụ$0 (bạn trả cho tài nguyên mà stack tạo ra).
  • Cách tính: Miễn phí với tài nguyên AWS; chỉ tính phí khi dùng extension của bên thứ ba (~$0,0009/thao tác).
  • Miễn phí: Miễn phí.
⚠️ Bẫy chi phí

Không có bẫy về giá; rủi ro là xoá stack làm xoá luôn tài nguyên (dùng DeletionPolicy để giữ lại).

Giá tham khảo tại us-east-1, cập nhật ngày 14/10/2026, có thể đã thay đổi. Luôn kiểm tra lại trên bảng giá của AWS. Bảng giá chính thức của AWS ↗

💡 Mẹo thi

Triển khai cùng hạ tầng cho nhiều tài khoản/region → StackSets. Xem trước thay đổi → change set. Giữ database khi xoá stack → DeletionPolicy: Retain. Phát hiện sửa tay ngoài template → drift detection. Phần bắt buộc duy nhất của template là Resources.

Thử sức với câu hỏi thi thật

Đề thi chứng chỉ AWS bằng tiếng Anh nên câu hỏi giữ nguyên tiếng Anh.

1An administrator is using Amazon CloudFormation to deploy a three tier web application that consists of a web tier and application tier that will utilize Amazon DynamoDB for storage. When creating the CloudFormation template, which of the following would allow the application instance access to the DynamoDB tables without exposing API credentials?

  1. ACreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and associate the Role to the application instances by referencing an instance profile.
  2. BUse the Parameter section in the Cloud Formation template to have the user input Access and Secret Keys from an already created IAM user that has the permissions required to read and write from the required DynamoDB table.
  3. CCreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and reference the Role in the instance profile property of the application instance.
  4. DCreate an identity and Access Management user in the CloudFormation template that has permissions to read and write from the required DynamoDB table, use the GetAtt function to retrieve the Access and secret keys and pass them to the application instance through user-data.
Xem đáp án

Đáp án đúng: C

Tạo IAM role có quyền đọc ghi bảng DynamoDB và tham chiếu qua thuộc tính instance profile của instance trong template. Không đưa access key vào tham số hay sinh access key trong template.

2You need to create a JSON-formatted text file for AWS CloudFormation. This is your first template and the only thing you know is that the templates include several major sections but there is only one that is required for it to work. What is the only section required?

  1. AMappings.
  2. BOutputs.
  3. CResources.
  4. DConditions.
Xem đáp án

Đáp án đúng: C

Resources là phần bắt buộc duy nhất của template CloudFormation, khai báo ít nhất một tài nguyên. Mappings, Outputs, Conditions đều là phần tuỳ chọn.

Luyện thêm hơn 700 câu trên Servisphere →

Câu hỏi thường gặp

CloudFormation khác gì CDK và Terraform?

CloudFormation mô tả hạ tầng bằng template YAML hoặc JSON. CDK cho viết bằng TypeScript, Python hay Java rồi sinh ra template CloudFormation. Terraform là công cụ của HashiCorp, dùng ngôn ngữ HCL và hỗ trợ nhiều nhà cung cấp cloud.

Change set và drift detection dùng để làm gì?

Change set cho xem trước những tài nguyên sẽ bị tạo, sửa hay thay thế trước khi cập nhật stack. Drift detection phát hiện tài nguyên đã bị sửa tay ngoài CloudFormation, lệch so với template.

CloudFormation có tính phí không?

Không tính phí với tài nguyên của AWS; bạn chỉ trả cho tài nguyên mà stack tạo ra. Chỉ tính phí khi dùng tài nguyên của bên thứ ba hay hook vượt hạn mức miễn phí.

Đề thi AWS hay hỏi gì về CloudFormation?

StackSets để triển khai nhiều tài khoản và region, change set, DeletionPolicy giữ lại database khi xoá stack, rollback khi lỗi, tham số và output giữa các stack, và cfn-init hay user data để cấu hình EC2.

Dịch vụ liên quan

Mở CloudFormation trên bản đồ →