- Help, Version
- Formatting
- Initialization
- Download and Install Modules
- Validation
- Plan Your Infrastructure
- Deploy Your Infrastructure
- Destroy Your Infrastructure
- 'Taint' or 'Untaint' Your Resources
- Refresh the State File
- Show Your State File
- Manipulate Your State File
- Import Existing Infrastructure
- Get Provider Information
- Manage Your Workspaces
- View Your Outputs
- Release a Lock on Your Workspace
- Log In and Out to a Remote Host (Terraform Cloud)
- Produce a Dependency Diagram
- Test Your Expressions
terraform -help
terraform fmt -help
terraform version
terraform fmt
terraform fmt --recursive
terraform fmt --diff
terraform fmt --check
Terraform Init Command (performs Backend Initialization, Child Module Installation, and Plugin Installation)
terraform init
terraform init -get-plugins=false
terraform init -lock=false
terraform init -input=false
terraform init -migrate-state
terraform init -verify-plugins=false
Note this is usually not required as this is part of the terraform init command.
terraform get
Check the versions of the already installed modules against the available modules and installs the newer versions if available
terraform get -update
- Terraform init should be run before 'validate' command.
terraform validate
terraform validate -json
terraform plan
- Saved file can then be passed to the terraform apply command.
terraform plan -out=<path>
terraform plan -destroy
- By default, a plan will be generated first and will need to be approved (yes/no) before it is applied.
terraform apply
- Useful in automation CI/CD pipelines.
terraform apply -auto-approve
- If provided, Terraform will take the actions in the plan without any confirmation prompts.
terraform apply <planfilename>
- Use with caution if other engineers might run concurrent commands against the same workspace.
terraform apply -lock=false
terraform apply -parallelism=<n>
terraform apply -var="environment=dev"
terraform apply -var-file="varfile.tfvars"
terraform apply -target="module.appgw.0"
terraform destroy
terraform destroy -target="module.appgw.0"
- Useful in automation CI/CD pipelines.
terraform destroy --auto-approve
terraform destroy -target="module.appgw.resource[\"key\"]"
- Use the taint command to mark a resource as not fully functional. It will be deleted and re-created.
terraform taint vm1.name
terraform untaint vm1.name
Modify the state file with updated metadata containing information on the resources being managed in Terraform
- Will not modify your infrastructure.
terraform refresh
terraform show
- If you want to read a specific state file, you can provide the path to it. If no path is provided, the current state file is shown.
terraform show <path to statefile>
terraform state list
- Move an item in the state, for example, this is useful when you need to tell Terraform that an item has been renamed, e.g. terraform state mv vm1.oldname vm1.newname
terraform state mv
terraform state pull > state.tfstate
terraform state push
terraform state replace-provider hashicorp/azurerm customproviderregistry/azurerm
- Useful when a resource has been manually deleted outside of Terraform
terraform state rm
terraform state show <resourcename>
- Import a VM with id123 into the configuration defined in the configuration files under vm1.name
terraform import vm1.name -i id123
terraform providers
terraform workspace show
terraform workspace list
terraform workspace select <workspace name>
terraform workspace new <workspace name>
terraform workspace delete <workspace name>
- These are displayed by default at the end of a terraform apply, this command can be useful if you want to view them independently.
terraform output
- '-state' option is ignored when the remote state is used.
terraform output -state=<path to state file>
terraform output -json
terraform output vm1_public_ip
- Useful when a lock has become ‘stuck’, usually after an incomplete Terraform run.
terraform force-unlock <lock_id>
terraform login
terraform login <hostname>
Remove the credentials that are stored locally after logging in, by default for Terraform Cloud (app.terraform.io)
terraform logout
terraform logout <hostname>
- This can then be rendered by a program called Graphwiz (amongst others).
terraform graph
terraform graph -plan=tfplan
terraform graph -type=plan
terraform graph -draw-cycles
terraform console