random provider. You will:
- author a simple configuration that generates a short “pet” name,
- initialize the working directory,
- validate and plan changes,
- apply and inspect state,
- change the configuration to trigger resource replacement, and
- destroy the resources.
- Terraform CLI reference: https://developer.hashicorp.com/terraform/cli
- random provider docs: https://registry.terraform.io/providers/hashicorp/random/latest/docs

Prerequisites
- Terraform installed and on your PATH. Confirm with
terraform version. - An empty working directory for this demo.
1 — Create the configuration
Create a file namedmain.tf with the following configuration. This uses the random_pet resource to create a short, human-readable name.
2 — Verify Terraform installation
Check that Terraform is installed and view the version:3 — Format the configuration (optional)
Keep your files consistent with:4 — Initialize the working directory
Initialize the directory to download provider plugins and create the.terraform directory and lockfile:
5 — Validate configuration
Validate that the configuration syntax and references are correct:6 — Generate and inspect a plan
Preview what Terraform will do without making changes:Save a plan to a file (optional)
To ensure you apply exactly the same plan later, save it:7 — Apply the plan (create resources)
Apply changes. By default Terraform will prompt for confirmation:yes at the prompt to proceed.
Example interaction:
Using
-auto-approve (for apply or destroy) skips interactive confirmation. Use it only in automated contexts where you are sure about the changes.8 — Change the resource (replacement behavior)
Editmain.tf to change the random_pet length from 2 to 3:
length forces replacement for random_pet in this provider, the plan will show a destroy and create action:
9 — Inspect the Terraform state
Terraform records metadata about managed resources in the state file (e.g.,terraform.tfstate). You can examine the state file directly or use state subcommands.
JSON excerpt of the state outputs (example):
10 — Destroy resources
When finished, destroy the managed resources:yes:
11 — Multiple resources and unique labels
If you add multiple resources of the same type, each resource block must have a unique label (allowed characters: letters, digits, and underscores). Example adding anotherrandom_pet resource:
terraform state list output:
terraform destroy -auto-approve to remove everything.
Quick reference — common Terraform CLI commands
Links and references
- Terraform CLI docs: https://developer.hashicorp.com/terraform/cli
- random provider: https://registry.terraform.io/providers/hashicorp/random/latest/docs
- Terraform docs (general): https://developer.hashicorp.com/terraform/docs