Interview › Infrastructure as Code (Terraform, Ansible)
What are Terraform meta-arguments (count, for_each, provider, depends_on, lifecycle)?
Infrastructure as Code (Terraform, Ansible) · Intermediate level
Answer
Terraform meta-arguments are language features that modify how resources or modules behave. Common examples are count, for_each, provider, depends_on, and lifecycle. They are not provider-specific arguments; they control Terraform's graph, instance addressing, provider selection, and lifecycle decisions.
Technical explanation
Meta-arguments are evaluated by Terraform before provider-specific API calls.
They affect addressing, dependencies, lifecycle, and provider binding.
Changing a meta-argument can change resource addresses, so review plans carefully.
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. Model repeated resources for: What are Terraform meta-arguments (count, for_each, provider, depends_on, lifecycle)?
2. Prefer stable keys with for_each:
variable "subnets" {
type = map(object({ cidr = string, az = string }))
}
resource "aws_subnet" "private" {
for_each = var.subnets
vpc_id = aws_vpc.main.id
cidr_block = each.value.cidr
availability_zone = each.value.az
tags = { Name = "private-${each.key}" }
}3. For nested blocks, use dynamic only when the input list genuinely drives repeated nested configuration:
dynamic "ingress" {
for_each = var.ingress_rules
content { from_port = ingress.value.port to_port = ingress.value.port protocol = "tcp" cidr_blocks = ingress.value.cidrs }
}4. Remove one key and run plan; confirm only that keyed instance is affected rather than later list indexes shifting.
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 sensitive flag on variables and outputs?
- How do you test Terraform code (validate, fmt, plan, tflint, terratest)?
- Why does the state file potentially contain sensitive values, and how do you protect it?
- How would you integrate Terraform into a CI/CD pipeline safely?
- How do you manage secrets in Terraform without leaking them into state?
- What is a policy-as-code check for Terraform (OPA, Sentinel, checkov)?
- What is a remote state data source, and what is the security concern with it?
- How do you handle a Terraform apply that fails halfway through?