使用 CloudFormation 实现基础设施即代码
学习如何使用 AWS CloudFormation 模板和堆栈,以声明方式定义、配置和版本化 AWS 基础设施。
使用 CloudFormation 实现基础设施即代码 是 CoddyKit 上的免费 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AWS for Backend Developers (EC2, S3, RDS, Lambda) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Infrastructure as Code?
Clicking through the console to create resources is slow, error-prone, and impossible to reproduce reliably.
Infrastructure as Code (IaC) defines your infrastructure in files you can version, review, and deploy repeatedly.
What Is CloudFormation?
AWS CloudFormation provisions AWS resources from a declarative template. You describe the desired end state and CloudFormation figures out how to create it.
Templates and Stacks
Key terms:
- A template is a YAML or JSON file describing resources
- A stack is a deployed instance of a template
- Updating the template updates the stack
A Minimal Template
This template creates a single S3 bucket.
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-iac-bucketParameters
Parameters make templates reusable by accepting input at deploy time, like an environment name or instance size.
Parameters:
EnvName:
Type: String
Default: devOutputs
Outputs export useful values from a stack, such as a bucket name or endpoint URL, so other stacks or operators can reference them.
Outputs:
BucketName:
Value: !Ref MyBucketDeploying a Stack
Create or update a stack from the CLI.
aws cloudformation deploy \
--template-file template.yaml \
--stack-name my-appChange Sets
A change set previews exactly what an update will add, modify, or delete before you apply it — a safety net against accidental data loss.
Automatic Rollback
If a stack update fails partway, CloudFormation rolls back to the last known good state, so you are never left with half-broken infrastructure.
Drift Detection
Drift detection tells you when someone changed a resource manually outside CloudFormation, so you can bring reality back in line with the template.
CloudFormation in CI/CD
In a pipeline, CodePipeline can deploy CloudFormation stacks automatically on every commit, making infrastructure changes follow the same review-and-deploy flow as your application code.
Quick Check
Test your IaC knowledge.
Recap
You learned Infrastructure as Code:
- Templates declare resources; stacks deploy them
- Parameters and outputs make templates reusable
- Change sets preview updates safely
- Drift detection and rollback keep infrastructure consistent
IaC makes your infrastructure repeatable, reviewable, and version-controlled.
常见问题解答
「使用 CloudFormation 实现基础设施即代码」课时是免费的吗?
是的 — 「使用 CloudFormation 实现基础设施即代码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程的其余内容,请升级到 CoddyKit PRO。 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程共包含 4 节课。
「使用 CloudFormation 实现基础设施即代码」这节课中我会学到什么?
学习如何使用 AWS CloudFormation 模板和堆栈,以声明方式定义、配置和版本化 AWS 基础设施。 你通过在浏览器中直接运行的动手代码来练习 AWS for Backend Developers (EC2, S3, RDS, Lambda),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AWS for Backend Developers (EC2, S3, RDS, Lambda) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 CloudFormation 实现基础设施即代码」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课中编写并运行代码吗?
能。每节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- CodeCommit 与 CodeBuild 基础
- 用于 EC2/Lambda 的 CodeDeploy
- 无服务器应用模型(SAM)
- 使用 CloudFormation 实现基础设施即代码