⌨️ CORE CLI COMMANDS| ⌨ Command | ℹ️ Description |
|---|---|
terraform init |
Initialize working directory, download providers & modules |
terraform init -upgrade |
Upgrade providers to latest allowed versions |
terraform init -reconfigure |
Reconfigure backend (ignores existing state) |
terraform validate |
Validate configuration syntax & internal consistency |
terraform fmt |
Reformat .tf files to canonical HCL style |
terraform fmt -recursive |
Format all files in directory tree |
terraform plan |
Preview changes before applying |
terraform plan -out=plan.tfplan |
Save plan to file for later apply |
terraform apply |
Apply changes to infrastructure |
terraform apply plan.tfplan |
Apply a saved plan file |
terraform apply -auto-approve |
Apply without interactive approval prompt |
terraform destroy |
Destroy all managed infrastructure |
terraform destroy -auto-approve |
Destroy without interactive approval |
| ⌨ Command | ℹ️ Description |
|---|---|
terraform show |
Display current state or saved plan (human-readable) |
terraform show -json plan.tfplan |
Output plan as JSON for machine parsing |
terraform output |
Display all output values |
terraform output <name> |
Display a specific output value |
terraform output -json |
Display outputs as JSON |
terraform state list |
List all resources in state file |
terraform state show <resource> |
Show detailed attributes of a resource |
terraform state pull |
Download & print current remote state |
terraform state push |
Manually upload local state to remote backend |
terraform providers |
Show providers required by configuration |
terraform version |
Print Terraform and provider versions |
terraform graph |
Generate DOT-format dependency graph |
| Flag | ℹ️ Description |
|---|---|
terraform plan -var 'k=v' |
Pass a single variable on CLI |
terraform plan -var-file=f.tfvars |
Load variables from a file |
terraform plan -target=aws_instance.web |
Plan only specific resource |
terraform plan -replace=aws_instance.web |
Force replace a specific resource |
terraform plan -refresh=false |
Skip refreshing state from real infra |
terraform plan -parallelism=10 |
Set concurrency for resource operations |
terraform apply -compact-warnings |
Show condensed warning messages |
terraform apply -lock=false |
Disable state locking (use with caution) |
terraform apply -lock-timeout=60s |
Wait up to N seconds to acquire lock |
| Block Type | Kind | Purpose |
|---|---|---|
terraform {} |
terraform | Global Terraform settings, required_providers, backend |
provider {} |
provider | Configure a provider (AWS, Azure, GCP, etc.) |
resource {} |
resource | Declare a managed infrastructure resource |
data {} |
data source | Read existing infrastructure or external data |
variable {} |
variable | Declare an input variable |
output {} |
output | Expose values to the caller or user |
locals {} |
locals | Define local computed values (like constants) |
module {} |
module | Embed a reusable Terraform module |
moved {} |
moved | Rename/move resources without destroy & recreate |
import {} |
import | Import existing infra into Terraform state (v1.5+) 🆕 |
check {} |
check | Define custom validation assertions (v1.5+) 🆕 |
| Type | Example | ℹ️ Description |
|---|---|---|
| string | "hello" | UTF-8 text |
| number | 42 / 3.14 | Integer or float |
| bool | true / false | Boolean value |
| list(type) | ["a", "b"] | Ordered sequence of values |
| set(type) | toset(["a","b"]) | Unordered unique values |
| map(type) | {key = "value"} | Key-value lookup table |
| object({}) | {name="x", count=1} | Named attribute collection |
| tuple([]) | ["a", 1, true] | Fixed-length mixed-type sequence |
| any | any type | Type constraint that accepts any type |
| null | null | Absence of a value |