Basic Commands
# Initialize a Terraform working directory
terraform init
# Create an execution plan
terraform plan
# Apply the changes required to reach the desired state of the configuration
terraform apply
# Destroy the Terraform-managed infrastructure
terraform destroy
Working with States
# List the resources in the state file
terraform state list
# Show detailed information about a single resource in the state file
terraform state show resource_name
# Move a resource from one name to another
terraform state mv old_name new_name
# Remove a resource from the state file
terraform state rm resource_name
Resource Targeting
# Apply changes to a specific resource
terraform apply -target=resource_type.resource_name
# Destroy a specific resource
terraform destroy -target=resource_type.resource_name
Plan and Apply Options
# Save the plan to a file
terraform plan -out=plan.tfplan
# Apply a saved plan
terraform apply plan.tfplan
# Refresh the state file
terraform refresh
# Format Terraform files
terraform fmt
# Validate the configuration files
terraform validate
Output and Variables
# Show the output values from the state file
terraform output
# Show a specific output value
terraform output output_name
# Set variables from a file
terraform apply -var-file="variables.tfvars"
Modules
# Initialize the modules
terraform get
# Update the modules
terraform get -update
Workspace Commands
# List all workspaces
terraform workspace list
# Create a new workspace
terraform workspace new workspace_name
# Select a workspace
terraform workspace select workspace_name
# Delete a workspace
terraform workspace delete workspace_name
# Show the current workspace
terraform workspace show
Debugging
# Enable detailed logs
export TF_LOG=DEBUG
# Save logs to a file
export TF_LOG_PATH=./terraform.log
Provider Versions
# Upgrade provider versions
terraform init -upgrade
# Lock provider versions
terraform providers lock
# Login to Terraform Cloud or Enterprise
terraform login
# Logout from Terraform Cloud or Enterprise
terraform logout