Interview › Infrastructure as Code (Terraform, Ansible)
What are input variables, outputs, and locals?
Infrastructure as Code (Terraform, Ansible) · Basic level
Answer
Input variables are parameters passed into a module. Outputs return values from a module, often for use by callers or other systems. Locals are named expressions used internally to simplify repeated logic. Variables are the module interface, outputs are the exported result, and locals are private implementation helpers.
Technical explanation
Variables can have types, defaults, descriptions, validation, and sensitive markings.
Outputs can expose IDs, ARNs, endpoints, and computed values for downstream modules or humans.
Locals improve readability by naming common expressions and normalizing inputs.
Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.
Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.
Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.
Hands-on example
1. Write a reusable module for: What are input variables, outputs, and locals?
2. Create modules/s3_bucket with variables, locals, resource, and outputs:
variable "name" { type = string }
variable "tags" { type = map(string) default = {} }
locals { common_tags = merge(var.tags, { managed_by = "terraform" }) }
resource "aws_s3_bucket" "this" { bucket = var.name tags = local.common_tags }
output "bucket_id" { value = aws_s3_bucket.this.id }3. Call it from the root module and pass the output to another module:
module "logs" { source = "../../modules/s3_bucket" name = "company-prod-logs" tags = var.tags }
module "app" { source = "../../modules/app" log_bucket_id = module.logs.bucket_id }4. Add validation, README examples, and a minimal Terratest or plan test before releasing v1.0.0.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What is the difference between a root module and a child module?
- What is the difference between count and for_each, and when do you use each?
- What is a Terraform module, and why use modules?
- What problem does for_each solve that count creates when removing items?
- Why pin provider and Terraform versions, and how do you do it?
- What is a dynamic block, and when would you use it?
- What is a Terraform provider, and how is it versioned?
- What is the difference between a data source and a resource?