Pro ยท Lesson 12 of 12
Build a small root module you can init, validate, and test without a cloud account. Then write the production differences you did not turn on.
Sign in to save your place.
You cook the recipe in a toy kitchen: a name, a validation rule, and a test. The real kitchen would add a remote state lock, a reviewed apply, and a binary you named on purpose.
One variable, one validation, one local, one terraform_data resource, one output. A test file asserts the output. No AWS provider, so a missing cloud key is not a failure.
If terraform test is not in your binary, record the version and which check you could run. Do not mark the test passed.
A remote backend with the lock your version documents. A CI job that prints terraform version or tofu version and refuses to apply from a pull request. A moved block if you rename the resource later. No claim that OpenTofu and Terraform will both accept every file you wrote. Run the file with the binary you picked.
| Production piece | Lesson |
|---|---|
| Remote state and a lock | State, backends, and locking |
| Separate dev and prod | Workspaces and environments |
| Reviewed apply | CI and HCP Terraform |
| Which binary | Security, policy, and OpenTofu |
Step 1 of 3The lab stays on local state and terraform_data.
Step 2 of 3fmt, validate, and a plan are still required when a cloud provider appears.
Step 3 of 3Production adds a backend, a lock, and an apply that a human approved.
Put these files in an empty directory. Use the Terraform binary if that is what you installed. If you use OpenTofu, run tofu and treat a rejected argument as a version note, not as a pass.
cat > main.tf <<'EOF'
terraform {
required_version = ">= 1.7.0"
}
variable "environment" {
type = string
validation {
condition = contains(["dev", "test"], var.environment)
error_message = "Use dev or test in this lab."
}
}
locals {
name_prefix = "dfd-${var.environment}"
}
resource "terraform_data" "note" {
input = local.name_prefix
}
output "name_prefix" {
value = local.name_prefix
}
EOF
cat > name.tftest.hcl <<'EOF'
run "dev_prefix" {
command = plan
variables { environment = "dev" }
assert {
condition = output.name_prefix == "dfd-dev"
error_message = "Expected dfd-dev."
}
}
EOF
terraform init -input=false
terraform validate
terraform test
Checked against the linked documentation in October 2026. A later release can move a flag, a price, or a supported pair. Use the page for the version you installed.