Skip to main content
This tutorial demonstrates common Terraform CLI workflows by creating a minimal Terraform configuration that uses the built-in 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.
Relevant references:
A dark-themed Visual Studio Code window showing the Explorer sidebar with a "DEMO" folder and an empty editor area featuring a large VS Code logo and command shortcut hints. The window sits on a colorful macOS desktop background.

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 named main.tf with the following configuration. This uses the random_pet resource to create a short, human-readable name.
Save the file and continue to the CLI steps below.

2 — Verify Terraform installation

Check that Terraform is installed and view the version:
Example output:

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:
Example (truncated):

5 — Validate configuration

Validate that the configuration syntax and references are correct:
Success message:
Example validation error (if an output references a nonexistent resource):

6 — Generate and inspect a plan

Preview what Terraform will do without making changes:
Example plan (truncated):

Save a plan to a file (optional)

To ensure you apply exactly the same plan later, save it:
Example output:

7 — Apply the plan (create resources)

Apply changes. By default Terraform will prompt for confirmation:
Type yes at the prompt to proceed. Example interaction:
To skip interactive confirmation (use only in automation):
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)

Edit main.tf to change the random_pet length from 2 to 3:
Run a plan to see what will happen:
Because increasing length forces replacement for random_pet in this provider, the plan will show a destroy and create action:
Apply the change (example using auto-approve):
Example output:

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):
List resources tracked in state:

10 — Destroy resources

When finished, destroy the managed resources:
You will be prompted to confirm by typing yes:
To skip confirmation (use with caution):

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 another random_pet resource:
Apply and then list state:
Example terraform state list output:
When finished, use terraform destroy -auto-approve to remove everything.

Quick reference — common Terraform CLI commands

This concludes the Terraform CLI demonstration. Continue experimenting with different providers and resource types to learn how plan/apply/destroy behave with real infrastructure.

Watch Video