terraform init初始化工作目录:下载 provider、设置 backend、安装模块。在新检出目录或添加新 provider 后必须先运行。
terraform init
cd infra/prod && terraform init
Terraform 速查表,90+ 命令涵盖 init、plan、apply、state、workspace、import 和调试,配真实示例。
56 条命令
terraform init初始化工作目录:下载 provider、设置 backend、安装模块。在新检出目录或添加新 provider 后必须先运行。
terraform init
cd infra/prod && terraform init
terraform init -upgrade将所有 provider 和模块升级到版本约束允许的最新版本。不加 -upgrade 时,init 会复用缓存的下载。
⚠ 常见坑: 在生产环境直接运行 -upgrade 而不先在 staging 测试,可能引入破坏性的 provider 变更。
terraform init -upgrade
terraform init -backend-config=backend.hcl从外部文件加载 backend 配置,而不是在 main.tf 中硬编码凭证。适合将密钥排除在版本控制之外。
terraform init -backend-config=backend.hcl
terraform init -backend-config="bucket=my-tfstate" -backend-config="region=us-east-1"
terraform init -migrate-state重新配置 backend 并将现有状态迁移到新位置。在从本地状态切换到 S3,或在 S3 桶之间切换时使用。
⚠ 常见坑: 迁移前备份状态文件,错误的迁移可能导致状态丢失。
terraform init -migrate-state
terraform init -reconfigure重新配置 backend 而不迁移现有状态。在指向全新 backend(如新 workspace)并从头开始时使用。
terraform init -reconfigure
terraform plan预览下次 apply 时 Terraform 将做的变更。比较之前会先从 provider 刷新状态。退出码 0 = 无变更,2 = 有变更。
terraform plan
terraform plan 2>&1 | tee plan.log
terraform plan -out=plan.tfplan将计划保存到文件,使后续 apply 精确执行已审查的内容。CI/CD 的黄金标准:在一个 job 中 plan,在另一个 job 中 apply。
⚠ 常见坑: 在自动化流水线中永远不要不加 -out 就 apply,plan 和 apply 之间基础设施的变化会带来意外。
terraform plan -out=plan.tfplan
terraform apply plan.tfplan
terraform plan -target=aws_instance.web将规划范围限制到特定资源(及其依赖项)。适合在不影响其余配置的情况下迭代单个资源。
⚠ 常见坑: -target 可能导致状态不一致。只在迭代开发时使用,不要作为标准操作。
terraform plan -target=aws_instance.web
terraform plan -target=module.vpc
terraform plan -var="key=value"在命令行内联传递变量。覆盖默认值和 tfvars 文件。适合开发时的临时覆盖。
terraform plan -var="env=prod"
terraform plan -var="region=eu-west-1" -var="instance_type=t3.medium"
terraform plan -var-file=prod.tfvars从 .tfvars 文件加载变量值。以 .auto.tfvars 结尾或名为 terraform.tfvars 的文件会自动加载,其他文件需要 -var-file。
terraform plan -var-file=prod.tfvars
terraform plan -var-file=common.tfvars -var-file=prod.tfvars
terraform plan -destroy预览销毁计划而不实际销毁任何东西。等同于 terraform destroy,但在应用前停止。
terraform plan -destroy
terraform plan -destroy -target=aws_rds_cluster.main
terraform plan -refresh-only将 Terraform 状态与真实基础设施同步,而不提议任何配置变更。在手动修改云资源后接受漂移时使用。
terraform plan -refresh-only
terraform apply -refresh-only -auto-approve
terraform apply将变更应用到真实基础设施。在做任何变更前会显示计划并提示确认。
terraform apply
terraform apply -var-file=prod.tfvars
terraform apply -auto-approve不经过交互式确认直接 apply。在 stdin 不可用的 CI/CD 流水线中必须使用。只在应用预先审查过的 plan 文件时才安全。
⚠ 常见坑: 在共享环境中永远不要直接对 terraform apply 使用 -auto-approve 而不指定 plan 文件,这会跳过审查步骤。
terraform apply -auto-approve plan.tfplan
terraform apply -auto-approve -var-file=staging.tfvars
terraform apply plan.tfplan精确应用之前保存的 plan 文件,按审查时的内容执行。这是推荐的 CI/CD 工作流:plan → 审查 → apply plan 文件。
terraform apply plan.tfplan
terraform apply -auto-approve plan.tfplan
terraform apply -replace=aws_instance.web强制在同一次 apply 中销毁并重建特定资源。替代已废弃的 `terraform taint` 命令。
terraform apply -replace=aws_instance.web
terraform apply -replace=aws_launch_template.app -auto-approve
terraform apply -target=module.vpc只将变更应用到特定资源或模块及其依赖项。适合分阶段发布或修复损坏的资源。
⚠ 常见坑: 反复使用 -target apply 可能导致状态漂移。谨慎使用,之后始终跟一次完整 apply。
terraform apply -target=module.vpc
terraform apply -target=aws_security_group.allow_tls
terraform apply -parallelism=20设置 Terraform 并发操作的资源数量。默认为 10。在大型部署且不担心 provider 限速时可以增大。
terraform apply -parallelism=20
terraform apply -parallelism=5 -var-file=prod.tfvars
terraform destroy销毁当前配置管理的所有资源。会显示计划并提示确认。在生产环境中极度谨慎使用。
⚠ 常见坑: terraform destroy 会删除 workspace 中的所有资源。选择性销毁请用 -target。
terraform destroy
terraform destroy -var-file=staging.tfvars
terraform destroy -target=aws_instance.web只销毁特定资源(及依赖它的资源)。当你只需要拆除栈的一部分时,比完整 destroy 安全得多。
terraform destroy -target=aws_instance.web
terraform destroy -target=module.rds -auto-approve
terraform destroy -auto-approve不经交互确认直接销毁。只在有意拆除 workspace 的临时 CI 环境中才安全。
terraform destroy -auto-approve
terraform destroy -auto-approve -target=module.test_env
terraform state list列出状态文件中当前追踪的所有资源。可以加过滤条件:`terraform state list aws_instance.*`。
terraform state list
terraform state list aws_s3_bucket.*
terraform state list module.vpc.*
terraform state show aws_instance.web显示状态文件中特定资源的所有属性。适合调试意外差异或验证导入资源的属性。
terraform state show aws_instance.web
terraform state show module.vpc.aws_vpc.main
terraform state mv aws_instance.old aws_instance.new在状态文件中重命名资源而不销毁重建。更新 .tf 配置以匹配,然后运行 terraform plan 确认零变更。
⚠ 常见坑: 在 state mv 之后始终运行 `terraform plan`,在 apply 前确认没有意外变更。
terraform state mv aws_instance.old aws_instance.new
terraform state mv module.app module.web
terraform state mv aws_instance.web "module.ec2.aws_instance.web"
terraform state rm aws_instance.legacy从状态文件中移除资源,但不在云端删除它。在你希望 Terraform 停止管理某资源但保持其运行时使用。
⚠ 常见坑: `state rm` 不会删除云端资源。移除后,该资源只是不被追踪,不会出现在 plan 中。
terraform state rm aws_instance.legacy
terraform state rm module.old_module.aws_s3_bucket.data
terraform state pull拉取远程状态并以 JSON 形式输出到 stdout。用于检查或备份远程 backend 中的当前状态。
terraform state pull
terraform state pull > backup-$(date +%F).tfstate
terraform state push terraform.tfstate将本地状态文件推送到远程 backend。极度谨慎使用,推送旧状态会覆盖当前状态,可能导致数据丢失。
⚠ 常见坑: state push 无条件覆盖远程状态。始终先 pull 并验证状态正确再 push。
terraform state push terraform.tfstate
terraform force-unlock LOCK_ID释放卡死的状态锁。在上一次运行崩溃未释放锁时使用。锁 ID 会在错误消息中打印出来。
⚠ 常见坑: 只在确认没有其他 apply 在运行时才强制解锁。在活动运行中解锁可能损坏状态。
terraform force-unlock 7ef08c27-5b32-4e9a-af67-a72f5fdca6a8
terraform workspace list列出当前 backend 中的所有 workspace。活动 workspace 以 * 为前缀。
terraform workspace list
terraform workspace show打印当前活动 workspace 的名称。在脚本中按环境分支行为时很有用。
terraform workspace show
ENV=$(terraform workspace show)
terraform workspace new staging创建新 workspace 并切换到它。每个 workspace 有独立的状态,允许同一套配置管理多个环境。
terraform workspace new staging
terraform workspace new feature-vpc
terraform workspace select prod切换到已有的 workspace。之后所有命令都针对该 workspace 的状态执行。
⚠ 常见坑: apply 前忘记检查当前活动 workspace 是一个常见错误,会导致部署到错误的环境。
terraform workspace select prod
terraform workspace select default
terraform workspace delete staging删除 workspace。该 workspace 必须为空(状态中无资源)。如需先用 `terraform destroy`。
terraform workspace delete staging
terraform workspace delete -force old_env
terraform import aws_s3_bucket.logs my-logs-bucket将现有云资源导入 Terraform 状态。先写好资源块,再用 provider 特定的 ID 运行 import。import 不生成配置,需自己编写。
⚠ 常见坑: 导入后始终运行 `terraform plan` 找出配置中缺失的属性。配置错误会立即导致漂移。
terraform import aws_s3_bucket.logs my-logs-bucket
terraform import aws_instance.web i-1234567890abcdef0
terraform import aws_route53_record.www Z1PA6795UKMFR9_www_A
terraform import -var="region=us-east-1" aws_vpc.main vpc-12345678在 import 时传递变量。当 provider 配置本身依赖变量(如 region、账号 ID)时必需。
terraform import -var="region=eu-west-1" aws_vpc.main vpc-12345678
terraform import -var-file=prod.tfvars aws_db_instance.main db-identifier
terraform output打印当前状态中所有根模块的输出值。从状态读取,不调用 provider,不变更基础设施。
terraform output
terraform output | grep instance_ip
terraform output instance_ip按名称打印单个输出值。默认输出带引号。
terraform output instance_ip
terraform output db_endpoint
terraform output -json将所有输出以 JSON 对象打印。适合通过管道传给 jq 或在脚本和 CI 系统中消费。
terraform output -json
terraform output -json | jq .instance_ip.value
IP=$(terraform output -json | jq -r .instance_ip.value)
terraform output -raw instance_ip打印单个输出值,不加引号。在需要原始字符串而不是 JSON 引号值的 Shell 脚本中使用。
IP=$(terraform output -raw instance_ip)
ssh ubuntu@$(terraform output -raw instance_ip)
terraform fmt将当前目录中所有 .tf 文件重新格式化为 Terraform 规范样式。任何时候都可以安全运行,只改变空白和缩进。
terraform fmt
terraform fmt main.tf
terraform fmt -recursive递归格式化当前目录及所有子目录中的 .tf 文件。在 monorepo 中提交前使用。
terraform fmt -recursive
terraform fmt -recursive .
terraform fmt -check如果有文件需要格式化则以非零退出码退出,不修改文件。在 CI 中用来强制代码风格。
terraform fmt -check
terraform fmt -check -recursive || (echo "Run terraform fmt" && exit 1)
terraform fmt -diff显示格式化变更的 diff 而不应用它们。审查时用来查看 fmt 会做什么变更。
terraform fmt -diff
terraform fmt -diff -recursive
terraform validate检查配置的语法错误和语义问题(无效引用、缺少必需参数)。不访问 provider 或远程状态。
⚠ 常见坑: validate 只检查静态语法,无法捕获依赖 provider schema 或运行时值的错误。
terraform validate
terraform validate && echo "OK"
terraform providers打印当前配置所需 provider 的树状视图,包含版本约束和来源。
terraform providers
terraform providers lock创建或更新 .terraform.lock.hcl 依赖锁文件。应提交到版本控制,以便所有团队成员使用完全相同的 provider 版本。
terraform providers lock
terraform providers lock -platform=linux_amd64 -platform=darwin_amd64
terraform providers mirror /local/mirror将所有所需 provider 下载到本地目录。用于设置离网环境,其中 CI runner 无法访问公共 Terraform Registry。
terraform providers mirror /tmp/tf-providers
terraform providers mirror /mnt/shared/providers
terraform get下载并安装配置中引用的模块。terraform init 会自动调用,在不重新 init 的情况下添加新模块后显式使用。
terraform get
terraform get -update
terraform get -update检查已安装模块的新版本并下载更新。在不完全重新 init 的情况下拉取模块 bugfix。
terraform get -update
TF_LOG=DEBUG terraform plan为单次命令启用详细调试日志。日志级别:TRACE、DEBUG、INFO、WARN、ERROR。DEBUG 显示每次 provider API 调用。
⚠ 常见坑: DEBUG 日志包含凭证值和敏感数据。分享或提交日志文件前务必脱敏。
TF_LOG=DEBUG terraform plan
TF_LOG=INFO terraform apply
TF_LOG=TRACE terraform init 2>&1 | head -200
TF_LOG_PATH=./terraform.log terraform apply将调试日志重定向到文件而不是 stderr。与 TF_LOG 结合控制详细程度。当日志太长无法在终端查看时必不可少。
TF_LOG=DEBUG TF_LOG_PATH=./tf-debug.log terraform plan
TF_LOG=INFO TF_LOG_PATH=/var/log/terraform.log terraform apply
TF_LOG_CORE=DEBUG TF_LOG_PROVIDER=OFF terraform plan为 Terraform 核心引擎和 provider 插件分别设置日志级别。在调试核心问题时用来屏蔽嘈杂的 provider 日志。
TF_LOG_CORE=DEBUG TF_LOG_PROVIDER=OFF terraform plan
TF_LOG_CORE=OFF TF_LOG_PROVIDER=TRACE terraform apply
terraform show以人类可读格式显示当前状态文件或已保存 plan 文件的内容。
terraform show
terraform show plan.tfplan
terraform show -json plan.tfplan | jq .
terraform console针对当前状态打开交互式 HCL 表达式 REPL。无需编写 plan 就能求值表达式、测试函数、检查资源属性。
terraform console
# Inside console:
aws_instance.web.public_ip
jsonencode({ key = "value" })cidrsubnet("10.0.0.0/16", 8, 2)terraform graph以 DOT 格式输出资源依赖图。通过管道传给 Graphviz 生成可视化图表。
terraform graph
terraform graph | dot -Tpng > graph.png
terraform graph | dot -Tsvg > graph.svg && open graph.svg
terraform version打印 Terraform 版本和所有已安装 provider 的版本。在 bug 报告中使用,或在 CI 中验证环境。
terraform version
terraform version -json
可搜索的 Terraform 速查表,90+ 条命令按 11 类组织。Init: terraform init、-upgrade 升级 provider、-backend-config 传配置 文件、-migrate-state 切换 backend、-reconfigure 重新配置。Plan: terraform plan、-out 保存 plan 文件、-target 只规划单个资源、 -var 和 -var-file 注入变量、-destroy 预览销毁、-refresh-only 只同步状态。Apply:terraform apply、-auto-approve 用于 CI、应用 已保存的 plan 文件、-replace 替代旧版 taint、-parallelism 提速。 Destroy:terraform destroy、-target 选择性销毁。State:state list、 state show、state mv 无需重建重命名、state rm 取消追踪、state pull/push 手动操作状态、force-unlock 解锁。Workspace:workspace list/new/select/show/delete 隔离环境。Import:terraform import 通过资源地址和 provider ID 将现有基础设施纳入管理。Output: terraform output、-json 供脚本使用、-raw 给 Shell 赋值。格式化 与校验:terraform fmt -recursive、-check 用于 CI 门控、-diff 审查差异;terraform validate 语法检查。Provider:providers lock 和 providers mirror 用于离网环境;terraform get -update 刷新 模块。调试:TF_LOG 级别、TF_LOG_PATH 写文件、terraform console 作为 HCL REPL、terraform graph 导出依赖图。每条都有双语说明、 可复制例子和坑提醒。搜索跨命令、说明、坑和例子。一键复制。 100% 浏览器内运行。
把内容粘贴或拖入工具面板。
点击按钮,在浏览器内本地处理,文件不上传。
一键复制结果或下载到本地。
适合穿插在写代码、查问题、做 Review、上线前的小任务里。
这些入口会把当前任务接到更完整的工具链里。
同事的 apply 中途崩溃,在 S3 backend 上留了一个 `.lock`。你从 错误消息中找到锁 ID,运行 `terraform force-unlock <id>`,确认后 下一次 apply 就能正常进行。没有这条命令,你就得手动编辑 DynamoDB。
你把 `module.app` 重构成了 `module.web`,`terraform plan` 却要 销毁再重建 14 个资源。于是你改用 `terraform state mv module.app module.web`,再次 plan 得到零变更。基础设施从未重启。
六个月前手动创建了一个桶,现在要用代码管理。你写好 `aws_s3_bucket` 资源块,运行 `terraform import aws_s3_bucket.assets my-assets-bucket`,再用 `terraform plan` 找出配置中的空缺。
在新增 CI runner 之前,运行 `terraform providers lock -platform=linux_amd64 -platform=darwin_amd64`,让锁文件同时覆盖 macOS 开发者和 Linux CI。再也不会在第一次流水线运行时遇到 "provider not found"。
在 CI 中不加 `-out` 运行 `terraform apply`,plan 和 apply 之间基础设施若发生变化,两步的内容可能不同。始终 `plan -out=plan.tfplan` 再 `apply plan.tfplan`。
`terraform state rm` 只从状态中移除资源,并不在云端删除它。只在你希望 Terraform 忘记某个资源时使用,而不是在你想删除它时。
用 `terraform destroy` 而不加 `-target` 会删除 workspace 中的所有资源。精确销毁请优先用 `terraform destroy -target=<address>`。
全部在你的浏览器里跑。命令列表是内存中的静态数组。搜索框、分类胶囊、 复制按钮从不发任何网络请求。你输入的内容不会被记录或发送,搜索词也 不会写入 URL。这张表在离线、离网 CI 机器上或公司代理后面都能用,而 这些恰恰是 DevOps 团队最需要 Terraform 参考的场景。
做你这行的人, 还会一起用这些。