Lesson 3 of 7 in Infrastructure as Code with Terraform and OpenTofu
Resources, variables and outputs
A resource is one thing the tool manages. A variable is an input you can change without editing the resource. An output is a value the configuration reports after apply. References between them build the dependency graph that decides the order of work.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: The workflow: init, plan, apply, destroy
The problem this solves
A configuration with every value written in place works once. As soon as you need a second copy with different names or sizes, you are copy-pasting files and the copies drift apart.
Variables let one configuration produce different results from different inputs. Outputs hand useful results, such as an address or a file path, back to you or to another configuration.
How it works
- People and clients
- Control
- Runs code
- Other
You supply a value, it becomes a variable, a resource uses it, another resource references the first, and an output reports the result.
- Your value to Variable: 1 you set it
- Variable to local_file: 2 used as input
- random_pet to local_file: 3 referenced by
- local_file to Output: 4 reported
Reading the diagram from the right: you pass a value on the command line or in a terraform.tfvars file, and it lands in a variable declared with a variable block. The local_file uses var.greeting and also refers to random_pet.name.id.
Every reference like random_pet.name.id is a dependency. The tool builds a graph from them, creates the pet first, then the file, and can create unrelated resources in parallel. You rarely order things by hand.
Variables can have a type, a default and a description. A variable marked sensitive = true is hidden from plan and output text. It is still stored in state, which the next concept covers.
Try it yourself
Same free providers as before, in a fresh folder.
- 1Write config with a variable and an output
mkdir -p ~/iac-lab/vars && cd ~/iac-lab/vars cat > main.tf <<"EOF" terraform { required_providers { local = { source = "hashicorp/local" } random = { source = "hashicorp/random" } } } variable "greeting" { type = string description = "Word at the start of the file" default = "Hello" } resource "random_pet" "name" { length = 2 } resource "local_file" "note" { filename = "note.txt" content = format("%s from %s", var.greeting, random_pet.name.id) } output "who" { value = random_pet.name.id } EOF terraform init - 2Apply with the default
terraform apply -auto-approve cat note.txt; echo
-auto-approveskips the yes prompt. Use it only in a sandbox like this one. The output block printswho =followed by the pet name. - 3Override the variable
terraform apply -auto-approve -var 'greeting=Howdy' cat note.txt; echo
Only the file changes. The pet name is kept because nothing about it changed, which you can confirm in the plan.
- 4Put inputs in a file
echo 'greeting = "Namaste"' > terraform.tfvars terraform plan
terraform.tfvarsis read automatically. The plan shows the content changing from Howdy to Namaste. Command-line-varbeats the tfvars file when both are set. - 5Read outputs later, then clean up
terraform output who terraform destroy -auto-approve
terraform outputreads from state, so it works any time after apply.
What breaks, and where to look
Error: No value for required variable (or an interactive prompt asking for var.something).
- WHY IT HAPPENS
- The variable has no default and no value was supplied through
-var, a tfvars file or aTF_VAR_environment variable. In automation there is no one to answer the prompt. - CHECK FIRST
- Run
terraform plan -var 'name=value'or add the value toterraform.tfvars. Check the variable name spelling in the error.
Error: Unsupported argument. An argument named "contents" is not expected here.
- WHY IT HAPPENS
- A misspelled argument name. Each resource type accepts a fixed set of arguments, and the wrong name is rejected before anything runs.
- CHECK FIRST
- Read the resource's page in the provider documentation, for example the
local_filepage, and compare argument names.
A secret you marked sensitive is still readable somewhere.
- WHY IT HAPPENS
sensitive = trueonly hides the value from console output. The value is still written to the state file in plain text.- CHECK FIRST
- Open
terraform.tfstateand search for the value. Keep state in a protected location and prefer reading secrets at runtime from a secrets service.
When not to use this
- Do not turn every number into a variable. Variables you never change make a configuration harder to read. Add one when a value actually differs between uses.
- Putting secrets in
terraform.tfvarsand committing it exposes them. Keep such files out of Git or supply values through environment variables.
Check yourself
How does the tool decide which resource to create first?
From references between resources. If one uses another's attribute, the referenced one is created first.
Which wins when a value is given both with -var and in terraform.tfvars?
The -var command-line value wins.
Does sensitive = true keep a value out of the state file?
No. It only hides the value from displayed output. The state file still holds it in plain text.
Keep going
Lesson 65 of 92 in the DevOps path