Terraform Cheatsheet - Infrastructure as Code Command Reference

Essential Terraform IaC commands for the complete lifecycle from init to destroy, organized by scenario. Copy directly for your infrastructure.

SysOps·53 commands·Last updated 2026-07-21
Back to SysOps

Core Workflow 7

terraform init
初始化工作Directory,下载 Provider 和module
terraform plan
预览变更,Display将Create/修改/销毁的资源
terraform apply
Execute变更,应用基础设施修改
terraform destroy
销毁所有由Configure管理的基础设施
terraform fmt
格式化 .tf File为标准格式
terraform validate
验证ConfigureFile语法和内部一致性
terraform apply -auto-approve
自动批准Execute(CI/CD 使用)

State Management 9

terraform state list
ListstateFile中所有资源
terraform state show <resource>
Show指定资源的verbosestate
terraform state mv <src> <dst>
Move资源在state中的地址
terraform state rm <resource>
从state中Remove资源(不销毁实际资源)
terraform state pull
拉取远程state到本地 stdout
terraform state push
推送本地state到远程后端
terraform import <address> <id>
将已有资源Import Terraform 管理
terraform taint <resource>
标记资源为"损坏",下次 apply 会重建
terraform untaint <resource>
取消资源的"损坏"标记

Variables & Outputs 8

variable "region" {\n type = string\n default = "us-east-1"\n}
定义variable(type、default)
var.region
引用已定义的variable
TF_VAR_region=us-west-2
通过env varSetvariable
terraform plan -var="region=us-west-2"
通过命令行OptionsSetvariable
output "ip" {\n value = aws_instance.web.public_ip\n}
定义Output值
terraform output
Show所有Output值
terraform output -json
以 JSON 格式ShowOutput值
locals {\n name = "${var.prefix}-web"\n}
本地局部variable,简化表达式

Modules 6

module "vpc" {\n source = "terraform-aws-modules/vpc/aws"\n version = "5.0.0"\n}
引用 Registry module,指定版本
module "vpc" {\n source = "./modules/vpc"\n}
引用本地module(相对路径)
module "vpc" {\n source = "git::https://example.com/vpc.git"\n}
引用 Git 仓库中的module
module.vpc.vpc_id
引用module的Output值
terraform init -upgrade
Upgrademodule至最新允许版本
terraform get
下载和Updatemodule

Remote Backends 6

terraform {\n backend "s3" {\n bucket = "my-tf-state"\n key = "prod/terraform.tfstate"\n region = "us-east-1"\n }\n}
S3 后端存储stateFile
terraform {\n backend "s3" {\n dynamodb_table = "tf-lock"\n }\n}
DynamoDB state锁防concurrency
data "terraform_remote_state" "db" {\n backend = "s3"\n config = {\n bucket = "shared-state"\n key = "db/terraform.tfstate"\n }\n}
跨ConfigureRead远程state
data.terraform_remote_state.db.outputs.db_endpoint
引用远程state的Output值
terraform init -reconfigure
重新Configure后端(migratestate)
terraform init -migrate-state
将stateFilemigrate到新后端

Advanced Features 9

terraform workspace new staging
Create新的工作空间
terraform workspace list
List所有工作空间
terraform workspace select staging
切换到指定工作空间
resource "aws_instance" "web" {\n for_each = var.subnets\n}
for_each 通过setbatchCreate资源
resource "aws_instance" "web" {\n count = var.instance_count\n}
count 通过计数batchCreate资源
depends_on = [aws_s3_bucket.data]
显式dependencies,确保Createorder
lifecycle {\n create_before_destroy = true\n}
生命periodic策略:先Create后销毁
lifecycle {\n prevent_destroy = true\n}
生命periodic策略:防止意外销毁
lifecycle {\n ignore_changes = [tags]\n}
生命periodic策略:忽略特定property变更

Debugging 8

terraform console
interactive控制台,Test表达式
terraform graph | dot -Tpng > graph.png
生成资源dependencies关系图
terraform show
Show当前state的可读Output
terraform show -json
JSON 格式Outputstate,方便 jq 处理
TF_LOG=DEBUG terraform apply
开启 DEBUG 级别日志Output
TF_LOG=TRACE terraform plan
TRACE 级别日志,最verbose信息
TF_LOG_PATH=./tf.log terraform apply
将日志Output到指定File
export TF_LOG=WARN
Setlog level(TRACE/DEBUG/INFO/WARN/ERROR)

💡 Tips

  • terraform plan never modifies resources, safe to run anytime
  • Store sensitive values in variables, never hardcode in .tf files
  • Use terraform fmt to standardize code format across your team
  • State files contain sensitive information, never commit to Git
  • Use remote backends (S3 + DynamoDB) for team collaboration and state locking

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it