Terraform by HashiCorp is an open-source Infrastructure as Code (IaC) tool used to provision and manage cloud, on-prem, and SaaS infrastructure through configuration files written in HCL (HashiCorp Configuration Language).
With Terraform, you define infrastructure in a declarative format, allowing for versioning, reusability, automation, and consistency across environments.
| Term | Description |
|---|---|
| Providers | Plugin responsible for managing a specific cloud platform (e.g., AWS). |
| Resources | Infrastructure components like EC2, S3, etc. |
| Variables | Input values passed into configuration. |
| Outputs | Values that Terraform returns after execution. |
| State File | Keeps track of resources Terraform manages. |
🟢 Beginner Commands (Click to Expand)
terraform versionterraform initterraform validateterraform fmtterraform -help
terraform plan -help🟡 Intermediate Commands (Click to Expand)
terraform planterraform applyterraform destroyterraform output
terraform output my_variableterraform state list
terraform state show <resource>🔴 Advanced Commands (Click to Expand)
terraform apply -target=aws_instance.example
terraform destroy -target=module.vpcterraform get
terraform init -upgradeterraform init -backend-config="key=my-state.tfstate"terraform import aws_instance.example i-12345678terraform graph | dot -Tpng > graph.pngShows the installed version of Terraform.
terraform versionInitializes the working directory with provider plugins and backend config.
terraform init💡 Run this once per project after writing your .tf files.
Validates your configuration files for syntax errors.
terraform validateShows what actions Terraform will take without applying them.
terraform plan📌 Use before every apply to preview infrastructure changes.
Applies changes to reach the desired infrastructure state.
terraform apply- You can auto-approve with:
terraform apply -auto-approveRemoves infrastructure defined in the configuration files.
terraform destroy- Auto-confirm with:
terraform destroy -auto-approveAutomatically formats .tf files to canonical style.
terraform fmt- Format all recursively:
terraform fmt -recursiveDisplays human-readable output of the current or saved state.
terraform show
terraform show terraform.tfstatePrints the values of output variables after apply.
terraform output
terraform output instance_ipLists all resources tracked in the current state file.
terraform state listDisplays details about a specific resource in the state.
terraform state show aws_instance.exampleForces recreation of a resource on the next apply.
terraform taint aws_instance.exampleRemoves taint from a resource.
terraform untaint aws_instance.exampleBrings existing infrastructure into Terraform state.
terraform import aws_instance.example i-0abcd1234efgh5678Generates a dependency graph (in DOT format).
terraform graph | dot -Tpng > graph.pngLists all providers used in the current configuration.
terraform providersUsed to manage multiple workspaces (e.g., dev, staging, prod).
terraform workspace new dev
terraform workspace select dev
terraform workspace listSaves the execution plan to a file.
terraform plan -out=tfplanThen apply it later:
terraform apply tfplanApply only specific resources.
terraform apply -target=aws_instance.exampleMoves/renames resources in the state.
terraform state mv aws_instance.old aws_instance.newRemoves resource from state (does NOT destroy it in the cloud).
terraform state rm aws_instance.exampleOpens an interactive console to evaluate HCL expressions.
terraform console
> var.instance_typeAuthenticates to Terraform Cloud or Enterprise.
terraform loginLogs out from Terraform Cloud.
terraform logoutForce-unlocks a state file after a failed operation.
terraform force-unlock <LOCK_ID>terraform init
terraform plan
terraform applyterraform fmt
terraform validate
terraform plan
terraform applyterraform destroyGreat — here’s the full version of the Terraform.md cheat sheet with introductory info at the top and additional learning resources at the bottom, perfect for your repo:
- Keep
.tfstatefiles secure (use S3 + DynamoDB for remote locking) - Use
terraform.tfvarsor.auto.tfvarsfor sensitive input variables - Mark secrets using
sensitive = truein outputs - Use modules for reusable code
- Always run
terraform planbeforeapply - Version-lock providers in
required_providers
