Terraform Commands

Terraform Commands Cheat Sheet
terraform init

Initializes your Terraform working directory. This command is always the first command that should be run.

terraform validate

Validates the Terraform files in your directory for syntax errors and internal consistency.

terraform plan

Shows you the changes that Terraform will apply to your infrastructure without actually applying them. It’s useful for previewing changes.

terraform apply

Applies the changes required to reach the desired state of the configuration. You’ll be prompted for confirmation unless you use the `-auto-approve` flag.

terraform apply -auto-approve

Applies the changes without prompting for interactive approval. Use with caution in production environments.

terraform destroy

Destroys the infrastructure managed by Terraform. You’ll be prompted for confirmation unless you use the `-auto-approve` flag.

terraform destroy -auto-approve

Destroys the infrastructure without prompting for interactive approval. Use with extreme caution.

terraform fmt

Reformats your Terraform configuration files to a canonical format. It helps maintain consistency.

terraform show

Shows the current state of your managed infrastructure.

terraform state list

Lists all the resources currently tracked in the Terraform state.

terraform state show <resource_address>

Shows the attributes of a specific resource in the Terraform state.

terraform state rm <resource_address>

Removes a resource from the Terraform state. This does not destroy the actual infrastructure, and should be used with caution.

terraform state pull

Retrieves the current state from the configured backend and outputs it to the console.

terraform state push <state_file>

Pushes a local state file to the configured backend. Use with caution as it can overwrite the remote state.

terraform providers

Shows the providers required for the current configuration.

terraform version

Shows the version of Terraform you are using.

Advanced Terraform Commands

terraform graph

Generates a visual representation of the dependency graph of your resources.

terraform import <resource_address> <id>

Imports an existing infrastructure resource into your Terraform state, allowing you to manage it with Terraform.

terraform workspace select <workspace_name>

Selects an existing Terraform workspace.

terraform workspace new <workspace_name>

Creates a new Terraform workspace.

terraform workspace list

Lists all available Terraform workspaces.

terraform workspace delete <workspace_name>

Deletes a Terraform workspace.

terraform output

Displays the output values of your Terraform configuration.

terraform output <output_name>

Displays the value of a specific output in your Terraform configuration.