Infrastructure as Code
Manage cloud infrastructure 使用 code for reproducible, version-controlled 环境.
你将学到
- 什么 Infrastructure as Code (IaC) 是
- Benefits: reproducibility, versioning, review
- Declarative 对比 imperative 方法
- Popular IaC tools: Terraform, Pulumi, CDK
概念讲解
What Is IaC?
Infrastructure as Code (IaC) means defining your infrastructure — servers, networks, databases, load balancers — in configuration files rather than creating them manually through a cloud console. The configuration is version-controlled, reviewed, and applied with tools, just like application code. Instead of clicking through a web console to create a server, you write a file that describes the server and run a command to create it.
Benefits
IaC makes infrastructure reproducible — the same configuration always creates the same infrastructure, eliminating "它工作 in staging 但不 production" mysteries. It is version-controlled — every change is tracked in git, reviewable, and rollbackable. It is automatable — you can integrate it with CI/CD to test infrastructure changes before applying them. It is documentable — the code is the documentation of what exists. And it is shareable — new team members can understand the infrastructure by reading the code.
Declarative vs Imperative
Declarative IaC describes the desired end state: "I want 3 servers with a load balancer." The tool figures out how to get there. Terraform and CloudFormation are declarative. Imperative IaC describes the steps to take: "Create a server, then create a load balancer, then add the server to it." Declarative is generally preferred because it is simpler and handles drift — if someone manually changes the infrastructure, the next apply brings it back to the declared state.
Popular Tools
Terraform is the most popular IaC tool, using its own HCL language and supporting hundreds of providers. Pulumi lets you write infrastructure in real programming languages like TypeScript and Python. AWS CDK does the same for AWS specifically. Ansible focuses on configuration management — setting up servers after they exist. CloudFormation is AWS-native. Choose based on your cloud provider, team language preferences, and whether you need multi-cloud support.
示例代码
# Terraform: define infrastructure as code
# Save as main.tf:
# resource "aws_s3_bucket" "app" {
# bucket = "my-app-data-bucket"
# tags = { Environment = "production" }
# }
#
# resource "aws_instance" "web" {
# ami = "ami-0c02fb559f6fe2234"
# instance_type = "t3.micro"
# count = 2
# }
# Apply the infrastructure
terraform init # install providers
terraform plan # preview changes
terraform apply -auto-approve # create resources
terraform destroy # tear everything down
Terraform uses HCL 到描述 infrastructure declaratively. init 安装 providers, 计划 previews 更改, apply 创建 resources, and destroy 移除它们.
动手试一试
- YAML Validator
验证 IaC 配置 files.
- YAML 到 JSON
Convert IaC configs for inspection.
- Code Diff
Compare infrastructure 更改.
常见错误
错误写法
Manually 更改 infrastructure 那个 IaC manages
正确做法
从不制作 manual 更改到 IaC-managed resources. 下一个 terraform apply 将 overwrite 它们, causing confusion. 总是制作更改通过 code and apply 它们.
错误写法
不存储 Terraform 状态 securely
正确做法
存储状态 in remote backend (S3 + DynamoDB, Terraform Cloud) 使用 locking. 局部状态 files 能是 lost, cause conflicts, and 可能包含 secrets.
练习 Infrastructure as Code
2 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。