Skip to main content
This lesson walks through common Terraform CLI commands using a minimal configuration and the random provider. You’ll learn the typical Terraform local workflow: create a configuration, format it, initialize providers, plan changes, apply them, inspect state, and destroy resources. These same CLI steps apply when working with real cloud providers, though provider-specific behavior and execution times will differ. Setup: open an empty directory in VS Code and create a file named main.tf.

1) Check Terraform version

Confirm the installed Terraform version before you begin.

2) Create a minimal configuration

Add the random provider, a random_pet resource, and an output to main.tf. The random provider generates values locally and does not call external APIs. main.tf:

3) Format the configuration

Use terraform fmt to format all HCL files in the working directory.

4) Initialize the working directory

terraform init initializes the working directory, downloads provider plugins, and configures the backend (if one is configured).
After init, Terraform creates a .terraform directory with installed provider plugins. The binary layout may vary by Terraform version and OS.
The image shows a Visual Studio Code window with a file named "terraform-provider-random_v3.6.3_x5" highlighted in the Explorer. A warning indicates the file is not displayed due to unsupported text encoding, and the terminal below contains Terraform-related instructions.
The .terraform directory and provider plugin binaries are local artifacts for this working directory. They are safe to ignore in version control (add them to .gitignore).

5) Validate the configuration

terraform validate checks HCL syntax and basic semantics. Valid example:
Common validation error — undeclared reference:
Validation error message:
Fix by referencing the declared instance label:

6) Create an execution plan

Use terraform plan to preview changes without applying them.
Save a plan to a file with -out so you can apply exactly that plan later:

7) Apply the configuration

terraform apply runs a planning step and then prompts to confirm the changes. Add -auto-approve to skip interactive confirmation (use carefully in automation). Interactive apply:
Non-interactive apply:
Example apply output:

8) Update configuration: replacement vs. in-place update

Changing certain attributes may force a resource replacement depending on the provider and resource type. For example, changing length:
terraform plan may show a replacement:
Apply the replacement (example with auto-approve):
Sample replacement output:
Note: whether an attribute change forces replacement depends on the provider and resource.

9) Inspect the state

Terraform stores a state file (by default terraform.tfstate) that records managed resources and outputs. Example state fragment:
List resources tracked in state:

10) Manage multiple resources

Every resource instance requires a unique label:
After terraform apply:

11) Destroy resources

terraform destroy plans and prompts to destroy all managed resources in the configuration. Interactive:
Non-interactive:
Example destroy output:
Common typo to avoid: terraform destory (incorrect). Use terraform destroy.
Be careful with -auto-approve or --auto-approve flags on apply and destroy. They bypass interactive confirmation and can cause destructive changes if used accidentally.

Quick reference — common Terraform CLI commands

Summary

This lesson demonstrated a simple end-to-end Terraform workflow with the random provider: create a configuration, format and validate it, initialize providers, plan and apply changes, inspect state, and destroy resources. The same CLI workflow applies to cloud providers; expect provider-specific behavior and longer apply/destroy durations when interacting with remote APIs.

Watch Video