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
Setup
- Sign in at
app.terraform.ioand create an organization, for exampletf-course-<yourname>. Organization names are globally unique. - Create a project named
tf-course.
Starting file
Create ~/tf-course/lab8a/main.tf:
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
cd ~/tf-course/lab8a
terraform init && terraform apply -auto-approveNote the deployment output.
terraform loginExpected 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:
cloud {
organization = "tf-course-yourname"
workspaces {
name = "tf-course-cli"
}
}terraform initExpected 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.
terraform planExpected 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-courseproject.
Then:
terraform applyConfirm 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
terraform destroyConfirm the prompt. Then delete the workspace in Settings → Destruction and Deletion. Keep the variable set and the project for Lab 8B.
This lab has no verification checklist in the course file, so it counts as complete when every step is checked.