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, arandom_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
Useterraform 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).
init, Terraform creates a .terraform directory with installed provider plugins. The binary layout may vary by Terraform version and OS.

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:
6) Create an execution plan
Useterraform plan to preview changes without applying them.
-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:
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, changinglength:
terraform plan may show a replacement:
9) Inspect the state
Terraform stores a state file (by defaultterraform.tfstate) that records managed resources and outputs.
Example state fragment:
10) Manage multiple resources
Every resource instance requires a unique label:terraform apply:
11) Destroy resources
terraform destroy plans and prompts to destroy all managed resources in the configuration.
Interactive:
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
| Command | Purpose | Example / Notes |
|---|---|---|
terraform version | Show Terraform version | terraform version |
terraform fmt | Format HCL files | terraform fmt |
terraform init | Initialize working directory & download providers | terraform init |
terraform validate | Validate configuration syntax and semantics | terraform validate |
terraform plan | Preview changes | terraform plan |
terraform plan -out=FILENAME | Save a plan to a file to apply later | terraform plan -out=bryan |
terraform apply | Apply changes (prompts for confirmation) | terraform apply or terraform apply -auto-approve |
terraform state list | List resources tracked in state | terraform state list |
terraform destroy | Destroy all managed resources | terraform destroy or terraform destroy --auto-approve |
Summary
This lesson demonstrated a simple end-to-end Terraform workflow with therandom 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.