Terraform Commands Cheat Sheet is a must-have for DevOps engineers, cloud administrators, and developers who want to manage infrastructure as code effectively. Terraform provides a powerful CLI that helps you build, change, and manage resources across AWS, Azure, GCP, and more.
In this guide, we’ll cover the most useful Terraform commands, organized for quick reference. Whether you’re a beginner or an advanced user, this cheat sheet will help you work faster, troubleshoot better, and automate confidently.
What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It allows you to define your infrastructure in configuration files written in HCL (HashiCorp Configuration Language).
By using Terraform, you can automate cloud resource provisioning, ensure consistency, and version-control your infrastructure. Unlike manual setup, Terraform makes deployments repeatable and less error-prone.
With its declarative approach, Terraform figures out the difference between your current infrastructure and your desired state, then applies the necessary changes.
Basic Terraform Workflow
Every Terraform project typically follows four key steps:
- Initialize the project with
terraform init
. - Plan changes with
terraform plan
. - Apply changes using
terraform apply
. - Destroy infrastructure with
terraform destroy
.
This workflow ensures you maintain control and visibility while managing your infrastructure lifecycle.
Terraform Commands Cheat Sheet: Initialization
The first step in any Terraform project is initialization.
Initialize a Project
terraform init
This command downloads required providers, sets up the backend, and prepares Terraform for execution.
Re-initialize with Upgrade
terraform init -upgrade
Use this when you want to upgrade to the latest provider versions.
Initialization ensures Terraform has everything it needs before you run other commands.
Terraform Commands Cheat Sheet: Validation
Before applying changes, always validate your configuration files.
Validate Syntax
terraform validate
This checks whether your configuration is syntactically valid.
Format Code
terraform fmt
It reformats code into the canonical style, making files clean and consistent.
Validation commands save time by catching errors early in the development cycle.
Terraform Commands Cheat Sheet: Planning
Planning shows you what Terraform intends to do before applying changes.
Generate Execution Plan
terraform plan
This previews the actions Terraform will take to match the desired state.
Save Plan to File
terraform plan -out=plan.out
You can save the plan and apply it later for consistent execution.
Planning is especially useful for code reviews and production deployments.
Terraform Commands Cheat Sheet: Applying
Applying is where Terraform makes real changes to your infrastructure.
Apply Changes
terraform apply
This applies the changes defined in your configuration.
Apply Saved Plan
terraform apply plan.out
This ensures Terraform applies exactly what you reviewed earlier.
Always review plans before applying to avoid accidental infrastructure changes.
Terraform Commands Cheat Sheet: Destroying
Sometimes, you need to remove infrastructure completely.
Destroy Resources
terraform destroy
This removes all resources defined in the current configuration.
Auto-Approve Destroy
terraform destroy -auto-approve
Use this for automation, but be cautious—it skips confirmation prompts.
Destroying is useful for testing environments or decommissioning old systems.
Terraform State Management Commands
Terraform keeps track of resources in a state file. Managing this file is critical.
Show Current State
terraform show
Displays the current state in a human-readable format.
List Resources in State
terraform state list
Outputs all resources tracked by the state file.
Remove Resource from State
terraform state rm <resource_name>
Useful when a resource was deleted outside of Terraform.
Proper state management ensures Terraform knows the real status of your infrastructure.
Terraform Commands Cheat Sheet: Workspaces
Workspaces allow you to manage multiple environments (dev, staging, production) using the same configuration.
Create New Workspace
terraform workspace new staging
Switch Workspace
terraform workspace select staging
List Workspaces
terraform workspace list
Workspaces help maintain clean separation between environments without duplicating code.
Terraform Commands Cheat Sheet: Providers
Providers connect Terraform with specific cloud platforms or services.
Initialize Provider
terraform init
This downloads the required provider plugin.
Lock Provider Versions
provider "aws" {
version = "~> 5.0"
region = "us-east-1"
}
Locking provider versions ensures consistent behavior across teams and CI/CD pipelines.
Terraform Commands Cheat Sheet: Modules
Modules are reusable building blocks for infrastructure.
Use a Module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.0.0"
}
Update Modules
terraform get -update
Modules promote reusability and simplify large-scale projects.
Debugging Terraform Commands
Sometimes, debugging is necessary when things go wrong.
Enable Debug Logging
TF_LOG=DEBUG terraform apply
Redirect Logs to File
TF_LOG=DEBUG terraform apply > debug.log
Debugging helps uncover issues in provider communication, state management, or configuration.
Terraform Cheat Sheet: Best Practices
To get the most out of Terraform, follow these best practices:
- Use Version Control: Store configurations in Git for collaboration and rollback.
- Leverage Workspaces: Manage environments without duplicating code.
- Secure State Files: Store remote states in S3, GCS, or Terraform Cloud with locking enabled.
- Document Configurations: Use comments and meaningful variable names.
- Test in Staging First: Apply configurations in non-production environments before rollout.
Adopting best practices ensures stability, collaboration, and scalability.
Complete Example: Terraform for AWS EC2
Here’s a simple example of deploying an EC2 instance:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = "TerraformDemo"
}
}
Commands to Run
terraform init
terraform plan
terraform apply
terraform destroy
This workflow provisions, manages, and tears down the EC2 instance with minimal effort.
FAQ: Terraform Commands Cheat Sheet
1. What is the most important Terraform command?
The most commonly used commands are terraform init
, terraform plan
, terraform apply
, and terraform destroy
as they form the core workflow.
2. How can I upgrade Terraform providers?
Run terraform init -upgrade
to update provider plugins to the latest versions.
3. Where is the Terraform state file stored?
By default, it’s stored locally as terraform.tfstate
, but you can configure remote storage for collaboration.
4. Can I undo Terraform apply?
Terraform does not support an “undo” command. Instead, use terraform destroy
to remove applied resources.
5. What is the difference between terraform plan
and terraform apply
?
terraform plan
shows a preview of changes, while terraform apply
executes those changes on your infrastructure.