Loading...
# Initialize a working directory
terraform init
# Preview execution plan
terraform plan
# Apply changes
terraform apply
# Destroy infrastructure
terraform destroy
# List state resources
terraform state list
# Show a specific resource state
terraform state show aws_instance.web
# Move resource to new address
terraform state mv aws_instance.old aws_instance.new
# Remove resource from state (without destroying)
terraform state rm aws_instance.web
# Pull remote state to stdout
terraform state pull
# Push local state to remote backend
terraform state push
# List workspaces
terraform workspace list
# Create new workspace
terraform workspace new staging
# Switch workspace
terraform workspace select production
# Show current workspace
terraform workspace show
# Import existing resource into state
terraform import aws_instance.web i-abc123
# Show output values
terraform output
terraform output vpc_id
# Format HCL files
terraform fmt
terraform fmt -recursive
# Validate configuration
terraform validate
# Save plan to file
terraform plan -out=tfplan
# Apply saved plan
terraform apply tfplan
# Plan with var file
terraform plan -var-file=prod.tfvars
# Target specific resource
terraform plan -target=aws_instance.web
# Plan destroy
terraform plan -destroy
# Download modules
terraform get
# Update modules
terraform get -update
# Force module re-download on init
terraform init -upgrade
# Enable debug logging
TF_LOG=DEBUG terraform plan
# Enable trace logging
TF_LOG=TRACE terraform apply
# Log to file
TF_LOG=DEBUG TF_LOG_PATH=./debug.log terraform plan
| Flag | Description |
|---|---|
-auto-approve |
Skip interactive approval |
-parallelism=N |
Limit parallel operations (default 10) |
-refresh=false |
Skip state refresh |
-lock=false |
Disable state locking |
-no-color |
Disable color output |
-compact-warnings |
Show compact warning output |
# S3 backend example
terraform {
backend "s3" {
bucket = "my-tfstate"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
terraform.tfvars*.auto.tfvars-var-file flag-var flagTF_VAR_*Learned the concepts? Test yourself with Terraform interview questions.
Go to the question bank →