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.
Interactive Skill Mastery
Mark commands as learned to build your customized reference tracker. Retained locally in this browser.
CLI Operations
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 initOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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 planOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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 applyOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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-approveOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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 destroyOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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 providersOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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 showOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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 tfplanOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.State Management
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 listOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Variables & Outputs
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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Configuration Linting
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 validateOutput Example
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.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 fmtOutput Example
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: State lock acquired. Please wait
Another team member or CI pipeline is applying changes. If stuck, run 'terraform force-unlock <lock_id>' cautiously.
Resource already exists in cloud account
The resource is active in the cloud but untracked in Terraform state. Use 'terraform import' to import it into your state.
Provider configuration blocks mismatch
Verify provider credentials and lock files (terraform.lock.hcl) to ensure matching versions are active across machines.
Cyclic dependency reference loop found
Resource A references Resource B, which loops back to reference Resource A. Untangle references or use separate resource blocks.
Variables missing default definitions
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.
Related Resources
REST API Tester
Test API routes and endpoints directly in your browser with full request controls.
JSON Formatter & Validator
Beautify, validate, and minify JSON structures instantly.
Best Free Online Developer Tools
An expert review of must-have online utilities for developers.
Git Cheatsheet
Essential command reference for local and remote version control repositories.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com