Workflow
Initialize
terraform initInitialize working directory (configure the backend, install providers and modules, and create lock file).
terraform init -backend-config=myBackendVars.tfvarsInitialize with a backend var file.
terraform init -upgradeInitialize 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 getUpgrade module version when you change a module's source or version (same as doing
terraform init
again).
terraform providersShow providers used in the current configuration.
terraform providers -schemaPrint detailed schemas for the providers.
Validate and Format (optional)
terraform validateCheck for syntax errors in configuration files.
terraform init -backend=false terraform validateValidate the configuration without accessing the backend.
terraform fmtFormat configuration files in current directory (print out names of modified files).
terraform fmt -recursiveFormat configuration files in current directory and subdirectories.
Plan
terraform planShow expected changes (Execution Plan).
terraform plan -destroyShow expected deletions (Destruction Plan).
terraform plan -out=my_tfplan.zipSave plan to a zip archive.
Plan output signification:
+
: Create-
: Destroy-/+
: Destroy and Recreate~
: Update
Apply
terraform applyRun Plan, then Apply changes.
terraform apply my_tfplan.zipApply changes form Plan file (will not ask for approbation).
terraform apply -replace=my_resourceForce the replacement of a resource even if there are no changes in its configuration.
terraform apply -parallelism=12Set the number of concurrent operations (default is 10).
Destroy
terraform destroyDelete provisioned infrastructure.
Workspaces
Explore
terraform workspace listList current workspace.
terraform workspace select myWorkspaceChange workspace.
Create
terraform workspace new myWorkspaceCreate new workspace.
Delete
terraform workspace delete myWorkspaceDelete 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 pullReturn the full state.
terraform state listList resources in current state.
terraform state show myResource.myPropertyDisplay resource property.
Modify State
terraform state mv my_old_resource my_new_resourceRename a resource.
terraform state remove myResourceType.myResourceNameRemove resource from state (the resource will not be destroyed, just not managed by Terraform anymore).
terraform state push my_state.jsonOverwrite remote state with a local state file (this should be avoided if possible).
terraform import myResourceType.myResourceName cloudResourceIDImport a resource (add it to the state). The resource has to be defines in a tf file.
terraform refreshUpdate 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_IDManually unlock state.
Environment Variables
Log
export TF_LOG=traceEnable maximum verbosity (TRACE > DEBUG > INFO > WARN > ERROR).
export TF_LOG=offDisable detailed logs.
Input
export TF_INPUT=0Disable 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.