# AWS CloudFormation

**AWS CloudFormation** 是[亚马逊云科技](https://www.edgepedia.org/zh/amazon-web-services)（Amazon Web Services）推出的一项基础设施即代码服务，它根据文本模板预置一组组 AWS 资源，并将每一组资源作为一个称为“堆栈”的单元来管理。该服务于 2011 年 2 月 25 日发布，让开发者可以在模板文件中描述应用所需的资源，再由服务按照正确的顺序预置这些资源，并处理资源之间的依赖关系。<sup>[1](https://aws.amazon.com/about-aws/whats-new/2011/02/25/introducing-aws-cloudformation/)</sup> CloudFormation 本身不额外收费；用户只需为堆栈所预置的 AWS 资源付费。<sup>[1](https://aws.amazon.com/about-aws/whats-new/2011/02/25/introducing-aws-cloudformation/)</sup>

| 关键事实 | 详情 |
|---|---|
| 发布时间 | 2011 年 2 月 25 日，附带 WordPress、Tracks 和 Gollum 等应用的示例模板<sup>[1](https://aws.amazon.com/about-aws/whats-new/2011/02/25/introducing-aws-cloudformation/)</sup> |
| 模板格式 | YAML 或 JSON 文本文件，可使用 `.yaml`、`.json`、`.template` 或 `.txt` 等任意扩展名保存<sup>[2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)</sup> |
| 核心单元 | 堆栈：由单个模板定义、一起创建、更新和删除的相关资源<sup>[2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)</sup> |
| 更改预览 | 更改集在应用之前汇总拟议的更改；更新失败会回滚到上一个已知可用状态<sup>[2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)</sup> |
| 定价 | 该服务不收费；仅对底层 AWS 资源计费<sup>[1](https://aws.amazon.com/about-aws/whats-new/2011/02/25/introducing-aws-cloudformation/)</sup> |
| 状态模型 | AWS 以托管服务的形式持有堆栈状态；无需管理、锁定或担心丢失外部状态文件<sup>[3](https://durrellgemuh.com/blog/cloudformation-vs-terraform-a-devops-engineers-guide/)</sup> |
| 采用要求 | 对于新的 AWS 服务，CloudFormation 支持是内部功能发布检查清单上的必备项，仅有极少数例外<sup>[4](https://aws.amazon.com.cdn.amazon.com/blogs/devops/cloudformation-coverage/)</sup> |

## 模板、堆栈与更改集

CloudFormation 模板是一种 YAML 或 JSON 格式的文本文件，充当构建 AWS 资源的蓝图。例如，模板可以描述一个 Amazon EC2 实例及其实例类型、AMI ID、块设备映射和密钥对名称。实例类型等值可以在创建堆栈时指定，而不必硬编码在模板中，这使模板可以在不同场景中重复使用。<sup>[2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)</sup> 模板可以存储在 [Amazon S3](https://www.edgepedia.org/zh/amazon-s3) 存储桶中；如果提交的是存储在本地计算机上的模板，CloudFormation 会自动将其上传到用户账户中的 S3 存储桶。<sup>[5](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html)</sup><sup> • </sup><sup>[2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)</sup>

**堆栈。** 相关资源作为一个称为堆栈的单一单元进行管理。创建、更新或删除堆栈，会对模板所定义的整个资源集合执行相同的操作。一个包含 Auto Scaling 组、Elastic Load Balancing 负载均衡器和 [Amazon RDS](https://www.edgepedia.org/zh/amazon-rds) 数据库实例的模板，在作为一个堆栈提交时会预置所有这些资源。<sup>[2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)</sup>

**更改集。** 在更新堆栈之前，可以生成更改集作为拟议更改的摘要，展示这些更改可能对运行中的资源产生何种影响。AWS 给出的示例是：重命名一个 Amazon RDS 数据库实例会导致 CloudFormation 创建新数据库并删除旧数据库，除非已备份，否则数据会丢失；更改集会在更新运行之前揭示这一替换操作。更改集并不指示更新是否会成功；它们不检查账户配额、资源是否支持更新，或权限是否充足，其中任何一项都可能导致更新失败。发起更新时，CloudFormation 仅更新被修改的资源；如果更新失败，它会将堆栈回滚到上一个已知可用状态。<sup>[2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)</sup>

## 漂移与漂移感知更改集

当堆栈的资源在 CloudFormation 之外被更改时——通过 AWS 管理控制台、AWS CLI 或底层服务的 SDK——堆栈就会偏离其预期配置，即发生漂移。漂移感知更改集将模板与堆栈资源的实际状态进行比较，使漂移的资源与模板定义保持一致；当模板被更新以匹配实际状态时，漂移状态会被重置，而不会修改资源。其宣称的优势包括：预览一次部署是否会还原事件响应期间所做的带外更改；以及通过用模板值覆盖漂移属性或将模板更新为与实际状态一致，进行系统性对账。漂移感知更改集可以通过在 AWS CLI 的 `create-change-set` 命令中添加 `--deployment-mode REVERT_DRIFT` 参数来创建；对于支持漂移检测的资源类型（有少量例外），它们会执行实际状态、先前部署状态和期望状态的三方比较。<sup>[6](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/drift-aware-change-sets.html)</sup>

## 导入与重组资源

CloudFormation 可以接纳在服务之外创建的资源，而无需删除并重新创建它们。文档中记录的选项包括：

- **IaC 生成器**，自动扫描现有资源并根据其当前状态生成模板。<sup>[7](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/import-resources.html)</sup>
- **资源导入**，一种手动过程，在模板中描述现有资源并将其导入堆栈。<sup>[7](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/import-resources.html)</sup>
- **自动导入**，在模板中描述具有匹配自定义名称的资源后，自动导入这些资源。<sup>[7](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/import-resources.html)</sup>
- **堆栈重构**，在堆栈之间移动资源、将单体堆栈拆分为更小的组件，或将多个堆栈合并为一个，同时保留资源属性和数据。<sup>[7](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/import-resources.html)</sup>

资源导入还支持在堆栈之间移动资源，以及将现有堆栈作为 `AWS::CloudFormation::Stack` 资源嵌套在另一个堆栈中，从而实现模块化、可复用的设计。资源导入仅支持一层嵌套：堆栈不能导入到子堆栈中，已有子堆栈的堆栈也不能被导入。<sup>[8](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-nested-stacks.html)</sup>

## 扩展模板与控制权限

CloudFormation 提供了一些资源类型，用于扩展普通模板的能力。`AWS::CloudFormation::CustomResource` 处理内置类型无法提供的预置任务或资源；`AWS::CloudFormation::Macro` 定义在创建或更新期间处理模板的可复用代码；`AWS::CloudFormation::Stack` 创建嵌套堆栈；`AWS::CloudFormation::StackSet` 跨多个 AWS 账户和区域部署堆栈；`AWS::CloudFormation::WaitCondition` 及其句柄会暂停堆栈操作，直到满足某个条件，并使用预签名 URL 接收信号。<sup>[9](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-supplied-resource-types.html)</sup>

权限通过服务角色进行管理，服务角色是 CloudFormation 代入以对堆栈中的资源进行调用的 IAM 角色。默认情况下，CloudFormation 使用根据调用用户凭证生成的临时会话；指定服务角色则可以让管理员限制 CloudFormation 能做什么，例如即使用户拥有管理员权限，也将其限制为仅能执行 Amazon EC2 操作。一旦服务角色附加到堆栈，就无法移除；对堆栈拥有权限的其他用户无论自身是否拥有 `iam:PassRole` 权限，都可以使用该角色，因此该角色应授予最小权限，以避免无意的权限提升。<sup>[10](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-servicerole.html)</sup> 另外，堆栈策略是一份 JSON 文档，可以定义用户可以对哪些资源执行哪些更新操作，例如一条 `Deny` 语句阻止替换或删除生产数据库。<sup>[11](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html)</sup>

## 历史与生态位

CloudFormation 于 2011 年 2 月发布，附带适用于博客和维基等常见开源应用的可直接运行的示例模板。<sup>[1](https://aws.amazon.com/about-aws/whats-new/2011/02/25/introducing-aws-cloudformation/)</sup><sup> • </sup><sup>[4](https://aws.amazon.com.cdn.amazon.com/blogs/devops/cloudformation-coverage/)</sup> 随后于 2011 年 9 月 29 日推出了堆栈更新、应用引导、基于模板的 IAM 以及 EC2 实例上的脚本钩子。<sup>[12](https://aws.amazon.com/about-aws/whats-new/2011/09/29/aws-cloudformation-new-features-update-stack-and-iam-support/)</sup> 根据 AWS DevOps 博客，该服务在头十年间的创新速度提高了近 40 倍：2011 年发布了 80 项重要的新功能，而 2021 年则超过 3,000 项；CloudFormation 支持也成为新 AWS 服务功能发布检查清单上的必备项。<sup>[4](https://aws.amazon.com.cdn.amazon.com/blogs/devops/cloudformation-coverage/)</sup>

**与其他工具的关系。** AWS 云开发工具包（CDK）并不取代 CloudFormation；它是一个抽象层，从 [TypeScript](https://www.edgepedia.org/zh/typescript)、Python、Java、.NET 或 Go 代码合成 CloudFormation 模板，并且仍然使用 CloudFormation 作为部署引擎。<sup>[13](https://www.techvendorindex.com/compare/terraform-vs-cloudformation/)</sup> 与 Terraform 相比，结构性差异在于状态：CloudFormation 将状态作为托管服务保存在 AWS 内部，而 Terraform 将状态保存在 AWS 外部的文件中，通常是带有 DynamoDB 锁定的 S3 存储桶。<sup>[14](https://perun.au/insights/cloudformation-vs-terraform)</sup><sup> • </sup><sup>[3](https://durrellgemuh.com/blog/cloudformation-vs-terraform-a-devops-engineers-guide/)</sup> 两种工具都能检测漂移：CloudFormation 的漂移检测是一项针对堆栈运行的托管服务功能，Terraform 则通过针对状态的 plan 操作进行漂移检测；CloudFormation 的漂移检测不会自动纠正，因此对账需要通过更新堆栈来完成。<sup>[13](https://www.techvendorindex.com/compare/terraform-vs-cloudformation/)</sup><sup> • </sup><sup>[15](https://www.cloudroutehq.com/devops/terraform-vs-cloudformation)</sup> 对于多账户和多区域部署，StackSet 通过 AWS Organizations 将堆栈分发到多个账户和区域，而 Terraform 则通过提供商别名和模块实现这一模式，除非使用协调平台，否则需要手动编排。<sup>[13](https://www.techvendorindex.com/compare/terraform-vs-cloudformation/)</sup> CloudFormation 在 AWS 上的覆盖优势在于，新的资源类型在发布当天即得到支持，因为新服务发布必须提供 CloudFormation 支持；而 Terraform 的 AWS 提供商可能会在新服务发布后滞后数天或数周，直到提供商版本发布。<sup>[4](https://aws.amazon.com.cdn.amazon.com/blogs/devops/cloudformation-coverage/)</sup><sup> • </sup><sup>[13](https://www.techvendorindex.com/compare/terraform-vs-cloudformation/)</sup>

## 配额

CloudFormation 施加了服务配额，其中一些可以通过 `DescribeAccountLimits` API 按区域查询。在固定限制中，`cfn-signal` 等待条件数据和自定义资源响应各自上限为 4,096 字节；更大的负载必须发送到 Amazon S3 存储桶并通过 URL 引用。<sup>[16](https://docs.aws.amazon.com/en_uw/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html)</sup>

## References

1. [Introducing AWS CloudFormation, AWS What's New (February 25, 2011)](https://aws.amazon.com/about-aws/whats-new/2011/02/25/introducing-aws-cloudformation/)
2. [How CloudFormation works, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-overview.html)
3. [CloudFormation vs Terraform: A DevOps Engineer's Guide, Durrell Gemuh](https://durrellgemuh.com/blog/cloudformation-vs-terraform-a-devops-engineers-guide/)
4. [The history and future roadmap of the AWS CloudFormation Registry, AWS DevOps Blog](https://aws.amazon.com.cdn.amazon.com/blogs/devops/cloudformation-coverage/)
5. [Working with CloudFormation templates, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html)
6. [Using drift-aware change sets, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/drift-aware-change-sets.html)
7. [Import AWS resources into a CloudFormation stack, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/import-resources.html)
8. [Nesting an existing stack, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-nested-stacks.html)
9. [Extend your template's capabilities with CloudFormation-supplied resource types, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-supplied-resource-types.html)
10. [CloudFormation service role, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-servicerole.html)
11. [Prevent updates to stack resources, AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html)
12. [AWS CloudFormation New Features: Update Stack and IAM Support, AWS What's New (September 29, 2011)](https://aws.amazon.com/about-aws/whats-new/2011/09/29/aws-cloudformation-new-features-update-stack-and-iam-support/)
13. [Terraform vs CloudFormation 2026: Independent Comparison, TechVendorIndex](https://www.techvendorindex.com/compare/terraform-vs-cloudformation/)
14. [CloudFormation vs Terraform: five failure patterns, Perun Engineering](https://perun.au/insights/cloudformation-vs-terraform)
15. [Terraform vs CloudFormation, the honest 2026 comparison, CloudRoute](https://www.cloudroutehq.com/devops/terraform-vs-cloudformation)
16. [Understand CloudFormation quotas, AWS CloudFormation User Guide](https://docs.aws.amazon.com/en_uw/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Named software products and platforms › Cloud, virtualization, and containers › CI/CD and deployment tools

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/aws-cloudformation

English original: https://www.edgepedia.org/aws-cloudformation
