Module 8: HCP Terraform

Lab 8A: CLI-Driven Workflow, Migration, Variables, and Execution Modes

Time
30 min
Cost
$0 (free HCP Terraform account)
Prerequisites
None listed
Objectives
0 of 10 checked

Setup

  1. Sign in at app.terraform.io and create an organization, for example tf-course-<yourname>. Organization names are globally unique.
  2. Create a project named tf-course.

Starting file

Create ~/tf-course/lab8a/main.tf:

~/tf-course/lab8a/main.tfHCL
terraform {
  required_version = "~> 1.12.0"

  required_providers {
    random = {
      source  = "hashicorp/random"
      version = "~> 3.7"
    }
  }
}

variable "pet_length" {
  type    = number
  default = 2
}

variable "owner" {
  type    = string
  default = "unset"
}

resource "random_pet" "app" {
  length = var.pet_length
}

resource "terraform_data" "deploy" {
  input = "${random_pet.app.id} owned by ${var.owner}"
}

output "deployment" {
  value = terraform_data.deploy.output
}

Steps

Shell
cd ~/tf-course/lab8a
terraform init && terraform apply -auto-approve

Note the deployment output.

Shell
terraform login

Expected output: a browser opens so you can create a token. Paste it into the terminal, and Terraform saves it to ~/.terraform.d/credentials.tfrc.json.

Insert this inside the terraform {} block, using your organization name:

HCL
  cloud {
    organization = "tf-course-yourname"

    workspaces {
      name = "tf-course-cli"
    }
  }

Shell
terraform init

Expected output: Terraform offers to migrate the existing local state into the tf-course-cli workspace. Answer yes. The workspace is created automatically, in the Default Project.

In the UI, open the workspace's Settings → General and move it to the tf-course project. Then open States and confirm that one state version, the migrated one, is listed.

Shell
terraform plan

Expected output: a message that the plan is running in HCP Terraform, with a link to the run, followed by streamed output and No changes. Open the link to see the same run in the UI.

In Variables, add a Terraform variable pet_length = 3. Run terraform plan again. Expected output: random_pet.app must be replaced, and terraform_data.deploy updates, because the workspace variable now feeds the remote run.

Go to Organization Settings → Variable sets and create tf-course-shared:

  • Add the Terraform variable owner = "travis", and tick Sensitive.
  • Apply the set to the tf-course project.

Then:

Shell
terraform apply

Confirm at the terminal prompt. Expected output: the deployment output is shown as sensitive. Back in the variable set, try to view owner. The value can't be read, only replaced.

In Settings → General, change Execution Mode to Local. Run terraform plan. Expected output: the plan now runs on your machine and doesn't stream a remote run. Workspace variables aren't applied, because HCP Terraform is now only storing state, so pet_length falls back to its default of 2, and the plan proposes another replacement. Switch back to Remote, and don't apply in Local mode.

Open States. Each apply created a version. Open one to view its contents and compare it with the previous version.

Cleanup

Shell
terraform destroy

Confirm the prompt. Then delete the workspace in Settings → Destruction and Deletion. Keep the variable set and the project for Lab 8B.

Lab 8A complete. Every step is checked.

This lab has no verification checklist in the course file, so it counts as complete when every step is checked.