Interview Infrastructure as Code (Terraform, Ansible)

How do you pass outputs from one module to another?

Infrastructure as Code (Terraform, Ansible) · Intermediate level

Answer

Outputs from one module are passed to another by referencing module.<name>.<output>. The producing module defines an output block, and the root module wires that value into an input variable on the consuming module. This keeps dependencies explicit and avoids hidden remote lookups.

Technical explanation

Outputs are the clean contract between modules.

Passing values through the root module makes dependencies visible in code review.

Avoid making child modules read each other's remote state directly unless a deliberate stack boundary exists.

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: How do you pass outputs from one module to another?

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.

Preparing for an interview?

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

← All Infrastructure as Code (Terraform, Ansible) questions