Variables, Outputs & Data Sources
Hard-coded values don't scale. This lesson introduces the three pieces of HCL that make a configuration reusable. If you think in functions, they map cleanly:
| HCL | Like a function's… | Purpose |
|---|---|---|
variable | parameter (input) | values you pass in so the config isn't hard-coded |
output | return value | values you surface out after apply |
data | lookup | read something that already exists and feed it in |
Each mini lesson builds on the state the one before it left, so they unlock in order.
Interview prep
Variables, outputs, and data sources come up constantly — interviewers use them to check you can write config that isn't copy-pasted.
What's the difference between a resource and a data source?
A resource block manages infrastructure — Terraform creates, updates, and destroys it, and records it in state. A data block only reads something that already exists (an image, a VPC, an account ID) so you can use its attributes; Terraform never creates or destroys it and it doesn't show up in the plan's add/change/destroy counts.
When would you use an output?
To surface a value after apply — typically a computed attribute you can't know in advance (an IP, an ARN, an id) — so a human, a script, or a parent module can consume it. Outputs are the documented return values of a configuration or module.