terraform workspace listShow available Terraform CLI workspaces for the current configuration.
Manage Terraform CLI workspaces, local and remote state, imports, and state surgery commands.
Create, list, switch, and delete CLI workspaces.
terraform workspace listShow available Terraform CLI workspaces for the current configuration.
terraform workspace showPrint the current Terraform CLI workspace name.
terraform workspace new devCreate and switch to a new workspace.
terraform workspace select prodSwitch to an existing workspace.
terraform workspace delete devDelete a Terraform CLI workspace.
Bring existing infra under management and modify state safely.
Associate an existing infrastructure object with a resource address in state.
terraform import aws_s3_bucket.logs my-logs-bucketAssociate an existing infrastructure object with a resource address in state.
terraform import -generate-config-out=generated.tf aws_s3_bucket.logs my-logs-bucketGenerate configuration while importing supported resources.
terraform state mv aws_instance.old aws_instance.newRename or move a resource address in state.
terraform state rm aws_instance.webStop tracking a resource without destroying the remote object.
terraform state replace-provider hashicorp/aws registry.acme.corp/acme/awsUpdate provider source addresses recorded in state.
Read and write raw state when needed.
terraform state pull > terraform.tfstateDownload the latest state and write it to a local file.
terraform state push terraform.tfstateUpload a local state file into the configured backend.
terraform state list module.networkList resources beneath a specific module path.
Reconfigure backend and migrate state between backends.
terraform init -migrate-stateMove state to a newly configured backend during re-init.
terraform init -force-copyAutomatically copy existing state to a new backend without prompting.
terraform init -lockfile=readonlyUse providers from the dependency lock file without updating it.
Useful snippets for remote and local state workflows.
terraform plan -state=terraform.tfstateUse a non-default local state path for commands that support it.
Exclude paths from uploaded configuration when using remote CLI-driven runs.
.terraformignore
.git/
.terraform/
node_modules/
*.pemExclude paths from uploaded configuration when using remote CLI-driven runs.
terraform {
backend "remote" {
organization = "acme"
workspaces {
name = "networking-prod"
}
}
}Example backend configuration for HCP Terraform remote state and runs.