[Sticky] Terraform top 100 commands
-
Initialize Terraform Configuration:
terraform init
-
Validate Terraform Configuration:
terraform validate
-
Plan Terraform Configuration:
terraform plan
-
Apply Terraform Configuration:
terraform apply
-
Destroy Resources:
terraform destroy
-
Show Terraform State:
terraform show
-
Refresh Terraform State:
terraform refresh
-
Import Existing Infrastructure:
terraform import <resource_type>.<resource_name> <existing_id>
-
Output Values:
terraform output
-
List Resources in State:
terraform state list
-
Taint a Resource:
terraform taint <resource_type>.<resource_name>
-
Untaint a Resource:
terraform untaint <resource_type>.<resource_name>
-
Graph Terraform Resources:
terraform graph | dot -Tpng > graph.png
-
Workspace Commands:
terraform workspace list
terraform workspace new <workspace_name>
terraform workspace select <workspace_name>
terraform workspace delete <workspace_name>
-
Input Variables:
terraform apply -var="key=value"
-
Backend Configuration:
terraform init -backend-config="key=value"
-
Format Terraform Configuration:
terraform fmt
-
Validate Provider Plugins:
terraform providers
-
Import Modules:
terraform get -update
-
State Pull and Push:
terraform state pull > terraform.tfstate
terraform state push terraform.tfstate
-
Upgrade Providers:
terraform init -upgrade
-
Set Environment Variables:
export TF_VAR_variable_name=value
-
Lock Terraform State:
terraform state lock <lock_id>
-
Unlock Terraform State:
terraform state unlock <lock_id>
-
Run Subcommands in Parallel:
terraform -parallelism=n <command>
-
Version of Terraform:
terraform version
-
Force Unlock Terraform State:
terraform force-unlock <lock_id>
-
Init with Backend Configuration:
terraform init -backend-config=backend.hcl
-
Apply Specific Resource:
terraform apply -target=<resource_type>.<resource_name>
-
Destroy Specific Resource:
terraform destroy -target=<resource_type>.<resource_name>
-
Remote State Configuration:
terraform remote config
-
Init from a Module Source:
terraform init -from-module=<module_source>
-
Get Module:
terraform get
-
Check and Print Terraform Configuration Files:
terraform fmt -check
terraform fmt -write=true
-
State Migrate:
terraform state mv <from> <to>
-
Output JSON:
terraform output -json
-
Locking Management Commands:
terraform state list
terraform state show <resource_type>.<resource_name>
-
Set Backend Configuration:
terraform init -backend-config=backend.hcl
-
Run Provisioner Locally:
terraform provisioners <resource_type>.<resource_name>
-
Get Resource Address:
terraform show -json | jq '.values.root_module.resources[].address'
-
Create Resource Import Script:
terraform import -backup=path/terraform.tfstate <resource_type>.<resource_name> <resource_id>
-
Read Input Variables from File:
terraform apply -var-file=variables.tfvars
-
Lock Infrastructure State:
terraform apply -lock=true
-
Unlock Infrastructure State:
terraform apply -lock=false
-
Detailed Output:
terraform apply -detailed-exitcode
-
Set Input Variable Defaults:
terraform apply -var="variable_name=value"
-
Get Resource Dependencies:
terraform graph | dot -Tpng > graph.png
-
Copy Files from Local to Remote:
terraform apply -var='copy_files=true'
-
Local Variables:
terraform apply -var='local_var=value'
-
Enable State Snapshots:
terraform apply -refresh=true
Adjust these commands based on your specific Terraform configurations and requirements.
-
Backend Initialization:
terraform init -backend=true
-
Override Variable Interactively:
terraform apply -var='variable_name=?'
-
Disable Colorized Output:
terraform init -no-color
-
Backend Initialization from Configuration File:
terraform init -backend-config=backend-config.tf
-
Lock Timeout Duration:
terraform apply -lock-timeout=5m
-
Detailed Resource Changes:
terraform apply -detailed-exitcode
-
Resource Dependencies in Graph Format:
terraform graph | dot -Tsvg > graph.svg
-
Parallelism during Apply:
terraform apply -parallelism=10
-
Resource Import with Custom ID:
terraform import -backup=path/terraform.tfstate <resource_type>.<resource_name> <custom_id>
-
Variable Description:
terraform apply -var='variable_name=value' -var-description='Description of variable'
-
Refresh and Update State:
terraform apply -refresh=true -update=true
-
Check Only During Plan:
terraform plan -detailed-exitcode
-
Create Resource Import Script with Backup:
terraform import -backup=path/terraform.tfstate <resource_type>.<resource_name> <resource_id>
-
Output Variable Value:
terraform output variable_name
-
Unset Input Variables:
terraform apply -var='variable_name='
-
Backup Terraform State:
terraform state backup > terraform_state_backup
-
Plan Output to File:
terraform plan -out=tfplan
-
Display Plan Output File:
terraform show tfplan
-
Input Variable from File:
terraform apply -var-file=variables.tfvars
-
Revert to Specific Terraform State:
terraform state push terraform_state_backup
-
Run Terraform from Specific Directory:
terraform init /path/to/config
-
Output Variables to JSON File:
terraform output -json > output.json
-
Apply Without Approval Prompt:
terraform apply -auto-approve
-
Input Variable from Environment Variable:
export TF_VAR_variable_name=value
-
Override Variable File:
terraform apply -var-file=override.tfvars
-
Provision Resources and Run Commands:
terraform apply -provision
-
Generate Config from Existing Resources:
terraform show -no-color > existing_config.tf
-
Disable Parallelism:
terraform apply -parallelism=1
-
Destroy and Suppress Confirmation:
terraform destroy -auto-approve
-
Plan Output to Specific File:
terraform plan -out=tfplan
-
Force Remote State Configuration:
terraform remote config -force-copy
-
Apply Plan from File:
terraform apply tfplan
-
Detailed Resource Output:
terraform show -no-color -json
-
Backup and Restore State:
terraform state pull > terraform_state_backup
terraform state push terraform_state_backup
-
Specific Variable File:
terraform apply -var-file=specific.tfvars
-
Check for Changes in Specific Module:
terraform plan -target=module.module_name
-
Override Variable and Show Plan Output:
terraform apply -var='variable_name=value' -detailed-exitcode
-
Display Output in JSON Format:
terraform output -json
-
Override Variable Interactively during Apply:
terraform apply -var='variable_name=?'
-
Output to Specific File:
terraform output -json > output.json
-
Graph with Specific Output Format:
terraform graph | dot -Tpng > graph.png
-
Print Configuration:
terraform show -no-color
-
Resource Changes Summary:
terraform show -no-color -module-depth=-1
-
Plan Output with Compact Display:
terraform show -no-color -compact
-
Destroy with Specific Resource Target:
terraform destroy -target=resource_type.resource_name
-
Detailed Changes with Dependencies:
terraform plan -detailed-exitcode -refresh=true
-
Lock Timeout with Specific Duration:
terraform apply -lock-timeout=10s
-
Output to Environment Variable:
export TF_VAR_output_variable=$(terraform output output_variable_name)
-
Override Backend Configuration:
terraform init -backend-config=backend-config.tf
-
Override Resource Configuration:
bash terraform apply -var='variable_name=value' -replace=resource_type.resource_name