What are input variables, output values, and locals in Terraform?
Understand Terraform input variables, output values and locals with syntax, real examples, interview tips and MCQs to write reusable IaC configs.
Expected Interview Answer
Input variables are parameters you pass into a Terraform configuration, output values are results a configuration exposes after apply, and locals are named expressions computed once and reused inside a module to keep code DRY.
Input variables (`variable` blocks) make a module reusable by accepting values from CLI flags, tfvars files, or environment variables. Output values (`output` blocks) surface useful data — like an IP or ARN — to the user or to a parent module that calls this one. Locals (`locals` blocks) define intermediate values and expressions so you can name a computation once and reference it as `local.name` throughout the configuration.
- Variables make modules reusable across environments
- Outputs expose results to users and parent modules
- Locals reduce repetition and centralize logic
- Improves readability and maintainability
- Enables clean composition of modules
AI Mentor Explanation
Input variables are like the team instructions handed to players before a match — the target score or the required run rate you feed in. Locals are the running calculations the scorer keeps, such as the current required rate derived from those inputs. Outputs are the final scorecard published after the game. Terraform takes your inputs, computes locals along the way, and publishes outputs when the innings ends.
Step-by-Step Explanation
Step 1
Declare input variables
Use `variable` blocks with types and optional defaults to parameterize the configuration.
Step 2
Supply values
Pass values via -var, a .tfvars file, or TF_VAR_ environment variables at plan/apply time.
Step 3
Compute locals
Use a `locals` block to name reusable expressions and reference them as local.<name>.
Step 4
Define outputs
Add `output` blocks to expose results like IPs or ARNs after apply.
Step 5
Consume across modules
Parent modules read child outputs and feed them as inputs to other modules.
What Interviewer Expects
- Correct role for each of variables, outputs, and locals
- How values are supplied to variables (tfvars, CLI, env)
- Understanding that locals are computed once and reused
- Knowing outputs pass data to parent modules and users
- Awareness of variable types, defaults, and validation
Common Mistakes
- Using locals when a variable input is actually needed
- Hardcoding values instead of parameterizing with variables
- Thinking outputs change infrastructure rather than expose data
- Confusing local.<name> with var.<name> references
- Forgetting sensitive = true on outputs that expose secrets
Best Answer (HR Friendly)
“Variables are the settings you feed into Terraform so the same code works in different situations. Locals are shortcuts for values you use repeatedly, and outputs are the useful results Terraform shows you after it finishes, like a server's address.”
Code Example
variable "environment" {
type = string
default = "dev"
}
locals {
name_prefix = "app-${var.environment}"
}
resource "aws_s3_bucket" "data" {
bucket = "${local.name_prefix}-data"
}
output "bucket_name" {
value = aws_s3_bucket.data.bucket
}Follow-up Questions
- How do you supply a value to an input variable?
- When would you use a local instead of a variable?
- How do outputs pass data between modules?
- What does marking an output as sensitive do?
- How do variable validation blocks work?
MCQ Practice
1. Which construct is used to parameterize a Terraform module with external values?
Input `variable` blocks accept external values, making a module reusable across environments.
2. How do you reference a local value named region?
Locals are referenced with the local.<name> syntax, for example local.region.
3. What is the main purpose of an output value?
Outputs expose computed results to the user or to a parent module after apply.
Flash Cards
What is a Terraform input variable? — A parameter passed into a configuration to make it reusable across environments.
What is a local value? — A named expression computed once and reused via local.<name> to keep code DRY.
What is an output value? — A result a configuration exposes after apply, also consumable by parent modules.
How do you supply variable values? — Via -var flags, .tfvars files, or TF_VAR_ environment variables.