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
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 gọi tới
Một ví dụ thật, từng bước
- 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ứ.
- Bạn viết template YAML mô tả VPC, ALB, Auto Scaling group và database.
- Bạn tạo stack từ template; CloudFormation tự tạo tài nguyên theo đúng thứ tự phụ thuộc.
- 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.
- 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?
| Làm gì | Tạo và cập nhật hạ tầng từ template | Vận hành máy: chạy lệnh, vá lỗi, truy cập | Quản lý nhiều tài khoản AWS |
|---|---|---|---|
| Đối tượng | Tài nguyên AWS (VPC, EC2, RDS…) | Máy EC2 và máy on-premises | Tài khoản và chính sách |
| Lặp lại được | Có, cùng template ra cùng hạ tầng | Có, qua document và runbook | Có, qua OU và SCP |
| Phí | Miễn phí với tài nguyên AWS | Phần lớn miễn phí | Miễn phí |
Tốn bao nhiêu tiền?
- 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í.
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).
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
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?
- 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.
- 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.
- 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.
- 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?
- AMappings.
- BOutputs.
- CResources.
- 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.
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.