Workflow


Initialize

terraform init
Initialize working directory (configure the backend, install providers and modules, and create lock file).

terraform init -backend-config=myBackendVars.tfvars
Initialize with a backend var file.

terraform init -upgrade
Initialize and upgrade providers to the latest version that respects the constraint in the required_providers block. Update the version and signature in the lock file.

terraform get
Upgrade module version when you change a module's source or version (same as doing terraform init again).

terraform providers
Show providers used in the current configuration.

terraform providers -schema
Print detailed schemas for the providers.


Validate and Format (optional)

terraform validate
Check for syntax errors in configuration files.

terraform init -backend=false terraform validate
Validate the configuration without accessing the backend.

terraform fmt
Format configuration files in current directory (print out names of modified files).

terraform fmt -recursive
Format configuration files in current directory and subdirectories.


Plan

terraform plan
Show expected changes (Execution Plan).

terraform plan -destroy
Show expected deletions (Destruction Plan).

terraform plan -out=my_tfplan.zip
Save plan to a zip archive.

Plan output signification:

  • +: Create
  • -: Destroy
  • -/+: Destroy and Recreate
  • ~: Update

Apply

terraform apply
Run Plan, then Apply changes.

terraform apply my_tfplan.zip
Apply changes form Plan file (will not ask for approbation).

terraform apply -replace=my_resource
Force the replacement of a resource even if there are no changes in its configuration.

terraform apply -parallelism=12
Set the number of concurrent operations (default is 10).


Destroy

terraform destroy
Delete provisioned infrastructure.

Workspaces


Explore

terraform workspace list
List current workspace.

terraform workspace select myWorkspace
Change workspace.


Create

terraform workspace new myWorkspace
Create new workspace.


Delete

terraform workspace delete myWorkspace
Delete a workspace. Note: The Default workspace can't be deleted.

State


Browse State

Warning: State files can contain sensitive data. Never commit a State file to version control system.

terraform state pull
Return the full state.

terraform state list
List resources in current state.

terraform state show myResource.myProperty
Display resource property.


Modify State

terraform state mv my_old_resource my_new_resource
Rename a resource.

terraform state remove myResourceType.myResourceName
Remove resource from state (the resource will not be destroyed, just not managed by Terraform anymore).

terraform state push my_state.json
Overwrite remote state with a local state file (this should be avoided if possible).

terraform import myResourceType.myResourceName cloudResourceID
Import a resource (add it to the state). The resource has to be defines in a tf file.

terraform refresh
Update state to match actual remote resources. Deprecated because it is unsafe. It is an alias for terraform apply -refresh-only -auto-approve.


Lock

terraform force-unlock my_lock_ID
Manually unlock state.

Environment Variables


Log

export TF_LOG=trace
Enable maximum verbosity (TRACE > DEBUG > INFO > WARN > ERROR).

export TF_LOG=off
Disable detailed logs.


Input

export TF_INPUT=0
Disable prompts for variables that haven't had their values specified (same as -input=false).


Config

export TF_CLI_CONFIG_FILE="$HOME/.terraformrc-custom"
Set the location of the CLI config file.