Terraform
Complete HashiCorp Terraform CLI reference, state management, and HCL patterns

๐ Introduction
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.
[!NOTE] OpenTofu Compatibility: OpenTofu is the open-source, community-driven fork of Terraform (Linux Foundation). Almost all CLI commands and HCL syntax (
terraform ...โtofu ...) are drop-in interchangeable.
๐น Key Concepts
| Term | Description |
|---|---|
| Providers | Plugins responsible for managing specific platforms (e.g., AWS, Azure, GCP, Kubernetes). |
| Resources | Infrastructure components declared to be managed (e.g., EC2, S3, VPC). |
| Data Sources | Read-only infrastructure queries fetching existing cloud resources outside Terraform. |
| Variables | Input values passed into configurations for modularity. |
| Outputs | Return values that Terraform displays or passes to other configurations. |
| State File | Tracks real-world resources mapped to configuration (terraform.tfstate). |
| Modules | Self-contained packages of Terraform configurations that create reusable components. |
๐ Terraform Commands Quick Reference
๐ข Beginner Commands (Click to Expand)
๐น Check Version
terraform version๐น Initialize Working Directory
terraform init๐น Validate Configuration
terraform validate๐น Format Code
terraform fmt
terraform fmt -recursive๐น Show Help
terraform -help
terraform plan -help๐ก Intermediate Commands (Click to Expand)
๐น Plan Infrastructure Changes
terraform plan
terraform plan -out=tfplan๐น Apply Infrastructure Changes
terraform apply
terraform apply -auto-approve
terraform apply tfplan๐น Targeted Recreation (Modern Replacement for Taint)
terraform apply -replace="aws_instance.web"๐น Destroy Infrastructure
terraform destroy
terraform destroy -auto-approve๐น Output Variables
terraform output
terraform output -json
terraform output instance_ip๐น Manage State
terraform state list
terraform state show <resource>
terraform state mv <old_name> <new_name>
terraform state rm <resource>๐ด Advanced Commands (Click to Expand)
๐น Target Specific Resources
terraform apply -target=aws_instance.example
terraform destroy -target=module.vpc๐น Work with Modules
terraform get
terraform init -upgrade๐น Backend Configuration
terraform init -backend-config="key=my-state.tfstate" -reconfigure๐น Import Existing Infrastructure (CLI & Modern Import Block)
# Classic CLI import
terraform import aws_instance.example i-12345678
# Modern Terraform 1.5+ declarative generation
terraform plan -generate-config-out=generated.tf๐น Native Automated Tests (Terraform 1.6+)
terraform test
terraform test -verbose๐น Graph Dependency Tree
terraform graph | dot -Tpng > graph.png๐น Force Unlock State
terraform force-unlock <LOCK_ID>๐ข Beginner Commands & Core Workflow
๐น terraform version
Shows the installed version of Terraform and configured provider plugins.
terraform version๐น terraform init
Initializes the working directory containing Terraform configuration files. Downloads required provider plugins and configures the backend.
# Standard initialization
terraform init
# Upgrade provider plugins to latest allowed versions
terraform init -upgrade
# Reconfigure backend settings (migrating state storage)
terraform init -reconfigure๐ก Run this once per project after writing your .tf files or whenever you add new providers/modules.
๐น terraform fmt
Automatically rewrites Terraform configuration files to a canonical format and style.
# Format current directory
terraform fmt
# Format all files in subdirectories recursively
terraform fmt -recursive
# Check if files are formatted without modifying them (CI/CD check)
terraform fmt -check๐น terraform validate
Validates your configuration files syntactically and checks whether internal references and arguments are valid without accessing remote services.
terraform validate๐น terraform plan
Creates an execution plan, letting you preview what actions Terraform will take to reach the desired state before making changes.
# Preview changes
terraform plan
# Save the execution plan to a file (recommended for CI/CD)
terraform plan -out=tfplan
# Pass custom variable values via CLI
terraform plan -var="environment=production" -var="instance_count=3"
# Specify a custom variables file
terraform plan -var-file="prod.tfvars"๐ Always run plan before apply to inspect additions (+), changes (~), and destructions (-).
๐น terraform apply
Executes the actions proposed in a Terraform plan to create, update, or delete infrastructure.
# Apply interactively (requires confirmation prompt)
terraform apply
# Auto-approve without prompt (useful in CI/CD pipelines)
terraform apply -auto-approve
# Apply a pre-generated plan file safely
terraform apply tfplan๐น terraform destroy
Terminates all resources tracked by the current configuration and state.
# Destroy with confirmation prompt
terraform destroy
# Auto-confirm destruction
terraform destroy -auto-approve
# Preview destruction without deleting
terraform plan -destroy๐ก Intermediate Commands & State Operations
๐น Modern Replacement for terraform taint (-replace)
[!WARNING]
terraform taintis deprecated since Terraform v0.15.2 and v1.x. Useterraform apply -replaceinstead.
# Modern standard (Terraform 0.15.2+)
terraform apply -replace="aws_instance.example"
# Legacy command (retains for backwards compatibility)
terraform taint aws_instance.example
terraform untaint aws_instance.example๐น terraform show
Displays human-readable output of the current state or a saved plan file.
# Show current state
terraform show
# Inspect a saved plan file
terraform show tfplan
# Output state in JSON for parsing with jq
terraform show -json๐น terraform output
Extracts the values of output variables from the state file.
# List all outputs
terraform output
# Get a specific output
terraform output instance_ip
# Extract as raw string (strips quotes for scripting)
terraform output -raw instance_ip
# Output in JSON format
terraform output -json๐น terraform state Management
Directly inspect and modify the Terraform state file.
# List all resources currently tracked in state
terraform state list
# Show detailed attributes of a tracked resource
terraform state show aws_instance.web
# Move/rename a resource within state without destroying it
terraform state mv aws_instance.old_name aws_instance.new_name
# Move resource into a module
terraform state mv aws_instance.web module.web_cluster.aws_instance.web
# Remove a resource from state without destroying it in the cloud
terraform state rm aws_instance.unmanaged_resource
# Pull remote state file to stdout
terraform state pull > backup.tfstate๐น terraform workspace
Manages separate state instances within the same configuration directory (useful for multi-environment deployments like dev, staging, prod).
# List all workspaces
terraform workspace list
# Create a new workspace
terraform workspace new staging
# Switch between workspaces
terraform workspace select prod
# View current workspace
terraform workspace show
# Delete an empty workspace
terraform workspace delete stagingIn HCL, reference the active workspace using ${terraform.workspace}:
locals {
env = terraform.workspace
}๐น terraform console
Launches an interactive shell for evaluating expressions, testing built-in functions, and inspecting variables.
terraform console
> max(5, 12, 9)
12
> format("web-%s-%02d", "prod", 1)
"web-prod-01"
> [for s in ["apple", "banana"] : upper(s)]
[
"APPLE",
"BANANA",
]
> exit๐ด Advanced Concepts & Modern Features (Terraform 1.5 - 1.10+)
๐น Declarative import Blocks (Terraform 1.5+)
Terraform 1.5 introduced declarative imports in .tf code alongside automatic configuration generation.
# In imports.tf
import {
to = aws_instance.web
id = "i-0123456789abcdef0"
}Generate configuration automatically:
# Generates the HCL resource block into generated.tf
terraform plan -generate-config-out=generated.tf
# Apply the generated import
terraform applyTraditional CLI import:
terraform import aws_instance.web i-0123456789abcdef0๐น Native Automated Testing (terraform test, 1.6+)
Terraform 1.6+ provides a built-in testing framework using .tftest.hcl files without external tools like Terratest.
# tests/website.tftest.hcl
variables {
bucket_name = "test-company-static-website-dev"
}
run "validate_bucket_creation" {
command = plan
assert {
condition = aws_s3_bucket.website.bucket == var.bucket_name
error_message = "S3 bucket name did not match expected variable"
}
}Run test suites:
terraform test
terraform test -verbose๐น check Blocks (Continuous Validation, 1.5+)
Assert conditions in your infrastructure without failing the entire plan or apply:
check "health_check" {
data "http" "website_status" {
url = "https://${aws_lb.app.dns_name}/healthz"
}
assert {
condition = data.http.website_status.status_code == 200
error_message = "${aws_lb.app.dns_name} returned non-200 status code."
}
}๐น Resource Lifecycle Meta-Arguments
Control how Terraform creates, updates, and deletes individual resources:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
lifecycle {
# Create replacement resource before destroying the existing one
create_before_destroy = true
# Prevent accidental terraform destroy
prevent_destroy = false
# Ignore cloud-side changes to specific attributes (e.g. auto-scaled tags)
ignore_changes = [
tags["LastUpdated"],
]
# Replace resource when a specific trigger condition changes
replace_triggered_by = [
aws_security_group.web.id
]
}
}๐น for_each vs count
| Feature | count | for_each |
|---|---|---|
| Identified By | Index integer ([0], [1]) | Map key or string set (["web"], ["api"]) |
| Shift Risk | Removing middle items causes recreation of subsequent items | Safe removal; only the targeted key is destroyed |
| Use Case | Identical copies | Distinct resources with unique names/keys |
# Example with for_each:
variable "subnets" {
type = map(string)
default = {
us-east-1a = "10.0.1.0/24"
us-east-1b = "10.0.2.0/24"
}
}
resource "aws_subnet" "main" {
for_each = var.subnets
vpc_id = aws_vpc.main.id
availability_zone = each.key
cidr_block = each.value
tags = {
Name = "subnet-${each.key}"
}
}๐น dynamic Blocks
Generate nested configuration blocks dynamically from maps or lists:
variable "ingress_ports" {
type = list(number)
default = [80, 443, 8080]
}
resource "aws_security_group" "web" {
name = "web-sg"
dynamic "ingress" {
for_each = var.ingress_ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}๐น Remote Backend & State Locking (S3 + DynamoDB)
Store state files centrally with concurrency locking to prevent state corruption:
terraform {
required_version = ">= 1.5.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "my-company-terraform-state-prod"
key = "networking/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-state-lock"
}
}[!TIP] Terraform 1.10+ Native S3 Locking: Terraform 1.10+ supports native S3 conditional writes, allowing state locking directly on S3 without needing a DynamoDB table!
๐น Sensitive Values & Secret Protection
Mask passwords and credentials in CLI logs and plan outputs:
variable "db_password" {
type = string
sensitive = true
}
output "database_url" {
value = "postgresql://admin:${var.db_password}@${aws_db_instance.main.endpoint}/app"
sensitive = true
}View sensitive output explicitly when needed:
terraform output -raw database_url๐ Useful Environment Variables
| Variable | Description |
|---|---|
TF_LOG=TRACE|DEBUG|INFO|WARN|ERROR | Configures logging verbosity for debugging |
TF_LOG_PATH=terraform.log | Writes logs to a file instead of stdout |
TF_VAR_name=value | Sets variable values via shell environment |
TF_CLI_ARGS | Appends default arguments to all CLI commands |
TF_IN_AUTOMATION=1 | Adjusts output formatting for automated CI/CD runs |
๐ง Tips & Production Best Practices
- Protect Your State: Always use a remote backend (S3, GCS, Azure Blob, Terraform Cloud) with encryption enabled and state locking.
- Never Commit State Files or Secrets: Keep
.tfstate,*.tfvars, and.terraform/in.gitignore. - Prefer
for_eachOvercount: Usingfor_eachprevents cascading resource recreation when list order changes. - Pin Provider and Terraform Versions: Lock versions in
required_providersandrequired_versionto prevent breaking changes across team members. - Enforce Modular Architecture: Split large monolithic configs into small, decoupled modules (e.g.
networking,database,compute). - Integrate Security Scanners in CI/CD: Run static analysis tools like
checkov,tfsec, ortrivybeforeterraform apply.