terraform
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTerraform
Terraform
Terraform is the world's most popular Infrastructure as Code (IaC) tool. It uses HCL to provision resources on any cloud. 2025 introduces Terraform Stacks for easier component management.
Terraform是全球最受欢迎的基础设施即代码(IaC)工具。它使用HCL语言在任意云平台上部署资源。2025年推出了Terraform Stacks功能,可更便捷地管理组件。
When to Use
适用场景
- Provisioning: Creating VPCs, Databases, K8s Clusters.
- Multi-Cloud: Learn one syntax (HCL), use it for AWS, Azure, GCP, Datadog, etc.
- State Management: It tracks resource state, allowing "Plan" (preview) and "Apply".
- 资源编排:创建VPC、数据库、K8s集群等资源。
- 多云环境:只需掌握一种语法(HCL),即可用于AWS、Azure、GCP、Datadog等平台。
- 状态管理:跟踪资源状态,支持“Plan(预览)”和“Apply(部署)”操作。
Quick Start
快速开始
hcl
undefinedhcl
undefinedmain.tf
main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "b" {
bucket = "my-tf-test-bucket"
tags = {
Name = "My bucket"
}
}
undefinedprovider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "b" {
bucket = "my-tf-test-bucket"
tags = {
Name = "My bucket"
}
}
undefinedCore Concepts
核心概念
Providers
Providers
Plugins that talk to APIs (AWS, Azure, Kubernetes).
与各类API(AWS、Azure、Kubernetes等)交互的插件。
State
State
terraform.tfstateterraform.tfstateStacks (2025)
Stacks(2025新功能)
A new layer above Modules. Allows defined dependencies between deployments (e.g., Deploy VPC, then Deploy K8s using VPC ID output).
位于Modules之上的新层级,可定义部署之间的依赖关系(例如:先部署VPC,再使用VPC ID输出部署K8s)。
Best Practices (2025)
2025最佳实践
Do:
- Use Remote State: S3 backend or Terraform Cloud. Never local state.
- Use Modules: DRY. Write a "Company Standard Bucket" module and reuse it.
- Use /
tfsec: Scan HCL for misconfigurations (open security groups) before deploy.trivy
Don't:
- Don't hardcode secrets: Use and pass it via
variable "db_password" {}or a secret manager.TF_VAR_
建议:
- 使用远程状态存储:采用S3后端或Terraform Cloud,切勿使用本地状态。
- 使用Modules:遵循DRY原则,编写“企业标准存储桶”模块并复用。
- 使用/
tfsec:部署前扫描HCL代码,检查配置错误(如开放的安全组)。trivy
禁止:
- 不要硬编码密钥:使用定义变量,并通过
variable "db_password" {}环境变量或密钥管理器传入。TF_VAR_