DevOps & IaC
Updated for 2026

Terraform CLI Cheatsheet & Modern IaC Cheat Sheet

Essential Terraform cheatsheet and infrastructure-as-code cheat sheet. Fast command list for initializing, preparing planning graphs, state handling, and deployments.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 21 Mastered (0%)

CLI Operations

terraform init
BeginnerBasics
Initialize local working directory, register providers, and fetch module sources.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform init

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform plan
BeginnerBasics
Create an execution dry-run to preview exact resource additions or destructions.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform plan

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform apply
BeginnerBasics
Execute provisioning steps to build infrastructure against configurations.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform apply

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform apply -auto-approve
BeginnerBasics
Apply infrastructure changes immediately, skipping manual prompt validations.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform apply -auto-approve

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform destroy
BeginnerBasics
Tear down and remove all managed resources defined inside your Terraform workspace.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform destroy

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform providers
BeginnerBasics
List all provider integrations and versions configured in the current workspace directory.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform providers

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform show
BeginnerBasics
Output the human-readable visual summary of the active infrastructure state or proposed plan file.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform show

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
lifecycle {\n prevent_destroy = true\n ignore_changes = [tags]\n}
BeginnerBasics
Declare advanced lifecycle constraints on resources to prevent accidental destruction or suppress tag drift reports.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

lifecycle {\n  prevent_destroy = true\n  ignore_changes  = [tags]\n}

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform plan -out=tfplan && terraform apply tfplan
BeginnerBasics
Compile a static binary plan snapshot to disk first, then apply it immediately to prevent runtime state changes.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform plan -out=tfplan && terraform apply tfplan

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

State Management

terraform state list
BeginnerBasics
List all individual resources currently recorded in your active state file.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform state list

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform state show <resource_name>
BeginnerBasics
Display highly detailed attributes of a specific resource recorded in state.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform state show <resource_name>

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform taint <address>
BeginnerBasics
Mark a specific managed resource as degraded, forcing it to be fully destroyed and recreated in the next apply cycle.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform taint <address>

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform import <address> <id>
BeginnerBasics
Associate an existing active cloud infrastructure asset with a defined declarative HCL resource address.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform import <address> <id>

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform workspace select <name>
BeginnerBasics
Switch the active local execution environment to a named workspace (ideal for staging vs production environments).

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform workspace select <name>

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Variables & Outputs

variable "region" {\n type = string\n default = "us-east-1"\n}
BeginnerBasics
Declare input variables with validation types and default parameter values.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

variable "region" {\n  type    = string\n  default = "us-east-1"\n}

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
output "instance_ip" {\n value = aws_instance.web.public_ip\n}
BeginnerBasics
Define output queries to retrieve and expose resource attributes after deployment.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

output "instance_ip" {\n  value = aws_instance.web.public_ip\n}

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
dynamic "ingress" {\n for_each = var.ports\n content {\n from_port = ingress.value\n }\n}
BeginnerBasics
Construct repeating nested blocks (like cloud security group rules) programmatically over variable arrays.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

dynamic "ingress" {\n  for_each = var.ports\n  content {\n    from_port = ingress.value\n  }\n}

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
validation {\n condition = length(var.vpc_id) > 4\n error_message = "VPC ID format is invalid."\n}
BeginnerBasics
Add robust, customizable input variable validation checks to catch configuration errors before plans run.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

validation {\n  condition     = length(var.vpc_id) > 4\n  error_message = "VPC ID format is invalid."\n}

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
variable "db_password" {\n type = string\n sensitive = true\n}
BeginnerBasics
Mask values with the sensitive flag to hide database passwords or API tokens from plans and output logs.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

variable "db_password" {\n  type      = string\n  sensitive = true\n}

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Configuration Linting

terraform validate
IntermediateDebugging
Verify if configuration syntax, parameter values, and structures conform to specifications.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform validate

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform fmt
BeginnerBasics
Automatically format raw HCL configurations to follow standard indentations.

When to Use

When managing cloud architectures and provisioning server networks programmatically through infrastructure code.

Common Mistakes

Modifying cloud infrastructure manually inside web consoles, putting active Terraform state files out of synchronization.

Shortcut / Pro-Tip

Run 'terraform plan' thoroughly to review additions or destructions before invoking actual 'terraform apply'.

Example

terraform fmt

Output Example

Console / Terminal
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Terraform Best Practices

1Commit Only State Schema Declarations

Never check local terraform.tfstate files or sensitive secrets into source control repositories (.gitignore them immediately).

2Establish Central Remote Backends

Always store state files in a secure remote bucket (like AWS S3) with state-locking (via DynamoDB) for team collaboration.

3Adopt Consistent Variable Schemas

Define variables inside variables.tf with explicit validation rules and describe blocks to prevent bad inputs.

4Modularize Reusable Configurations

Extract repeating setups into standalone Terraform modules to enforce consistency and keep resource code dry.

5Run Dry-Runs Before Every Deploy

Always inspect the proposed modifications on S3 or servers using 'terraform plan' before running 'terraform apply'.

Common Terraform Errors & Solutions

Error

Error: State lock acquired. Please wait

Solution

Another team member or CI pipeline is applying changes. If stuck, run 'terraform force-unlock <lock_id>' cautiously.

Error

Resource already exists in cloud account

Solution

The resource is active in the cloud but untracked in Terraform state. Use 'terraform import' to import it into your state.

Error

Provider configuration blocks mismatch

Solution

Verify provider credentials and lock files (terraform.lock.hcl) to ensure matching versions are active across machines.

Error

Cyclic dependency reference loop found

Solution

Resource A references Resource B, which loops back to reference Resource A. Untangle references or use separate resource blocks.

Error

Variables missing default definitions

Solution

Provide variables explicitly in a tfvars file or set default attributes inside variables.tf declarations.

Common Terraform Interview Questions

Q1What is Terraform and what are its key advantages in DevOps?

Terraform is an open-source Infrastructure-as-Code (IaC) tool by HashiCorp. It lets you define cloud assets in declarative HCL files. It is platform-agnostic, maintains state, automatically handles dependencies, and previews plans before running.

Q2What is the purpose of the Terraform state file (terraform.tfstate)?

The state file acts as a single source of truth mapping your HCL resources directly to actual cloud objects. It tracks metadata, manages dependencies, improves performance, and is used to detect configuration drift.

Q3Explain the difference between 'terraform plan' and 'terraform apply'.

terraform plan is a read-only dry-run that compares active cloud resources, state files, and HCL code to generate a change plan. terraform apply reviews and executes those steps to provision or destroy the resources.

Q4What are Terraform Modules and why should you use them?

Modules are self-contained packages of Terraform configurations grouped together. They promote reusable templates, abstract complexity, partition environments, and enforce standardized organization-wide infrastructure patterns.

Q5What is State Locking and why is it critical?

State locking prevents concurrent Terraform operations on the same state file, which would corrupt the state. Backends like S3 use DynamoDB to lock the state, ensuring only one person apply changes at a time.