← topics

>_ Terraform

20 commands

Initialise a Terraform working directory, downloading providers and modules

terraform init

Preview the infrastructure changes Terraform will make without applying them

terraform plan

Apply the Terraform configuration to create or update real infrastructure

terraform apply

Destroy all infrastructure resources managed by the current Terraform configuration

terraform destroy

Check Terraform configuration files for syntax errors and internal consistency

terraform validate

Reformat Terraform configuration files to the canonical style

terraform fmt

Print the output values defined in the Terraform configuration

terraform output

List all resources tracked in the current Terraform state file

terraform state list

Display a human-readable view of the current Terraform state

terraform show

Import an existing infrastructure resource into the Terraform state without recreating it

terraform import RESOURCE_ADDRESS RESOURCE_ID

Generate a Terraform execution plan and save it to a file for later use

terraform plan -out=tfplan

Apply a previously saved Terraform execution plan file

terraform apply tfplan

Apply a Terraform configuration with an inline variable override

terraform apply -var='project_id=my-project'

Create a new Terraform workspace for environment isolation

terraform workspace new WORKSPACE_NAME

Switch to an existing Terraform workspace

terraform workspace select WORKSPACE_NAME

Apply Terraform changes to a single specific resource, ignoring others

terraform apply -target=google_compute_instance.web

Move a resource to a different address in the Terraform state file without recreating it

terraform state mv OLD_RESOURCE_ADDRESS NEW_RESOURCE_ADDRESS

Remove a resource from Terraform state without destroying the real infrastructure

terraform state rm RESOURCE_ADDRESS

Mark a Terraform-managed resource for forced recreation on the next apply

terraform taint RESOURCE_ADDRESS

Apply Terraform changes without prompting for interactive confirmation

terraform apply -auto-approve

Ready to test yourself?

Practice these commands →