Exercise 2 ยท 20 minutes

Make Terraform say hello

Create your first configuration, inspect Terraform's proposed change, and apply it locally. No Azure login or cloud resources are required.

DifficultyFirst steps Azure costNone CreatesLocal state only

Before you start

Goal and prerequisites

Your goal

Use Terraform to produce the output Hello, Admiral engineer! and see the same change move through format, initialize, validate, plan, and apply.

You need

  • Terraform 1.9 installed
  • VS Code and a PowerShell terminal
  • An empty disposable folder

Participant task

Build your first configuration

  1. 01

    Create a working folder

    Keep this exercise separate from the course website repository.

    New-Item -ItemType Directory terraform-exercise-2
    Set-Location terraform-exercise-2
  2. 02

    Create main.tf

    Add the following configuration. terraform_data is built into Terraform, so this exercise does not create a cloud resource or download a provider.

    main.tf
    terraform {
      required_version = "~> 1.9"
    }
    
    variable "learner_name" {
      description = "Name used in the greeting"
      type        = string
      default     = "Admiral engineer"
    }
    
    resource "terraform_data" "greeting" {
      input = "Hello, ${var.learner_name}!"
    }
    
    output "greeting" {
      value = terraform_data.greeting.output
    }
  3. 03

    Format, initialize, and validate

    Run each command separately. Read the output before moving to the next command.

    terraform fmt
    terraform init
    terraform validate
  4. 04

    Review the plan

    Save the plan, then confirm that Terraform proposes exactly one resource to add and no cloud infrastructure.

    terraform plan -out main.tfplan
  5. 05

    Apply the reviewed plan

    Apply the same saved plan you just inspected, then read the output value.

    terraform apply main.tfplan
    terraform output greeting

Expected outcome

Check your result

Validation

terraform validate reports that the configuration is valid, the plan says Plan: 1 to add, 0 to change, 0 to destroy, and the final command prints:

"Hello, Admiral engineer!"

What just happened?

Terraform read the desired configuration, proposed a change, recorded the applied result in local state, and exposed a value through an output.

Which command gave you a reviewable preview without changing state?

Finish safely

Clean up and extend

Cleanup

Remove the item from Terraform state, then delete the disposable folder when you no longer need it.

terraform destroy

Optional extension

Run a new plan with your own name. Predict the change before reading the plan output.

terraform plan -var="learner_name=Your name"