Practical infrastructure challenge

Terraform Module Design Challenge

Design a reusable Terraform module with typed inputs, validation, stable outputs and a composable interface. Practice provider boundaries and plan-based contract tests.

1. Inspect the caller's requirements

Reusable service module

The platform team needs one reusable module that accepts a service object with a name and environment, rejects environments other than dev or prod, records the qualified name in a built-in terraform_data resource and exports the name in environment-name order. The root uses the same module for catalog and billing. No external provider is required.

module "catalog" {
  source = "./modules/service"
  service = {
    name        = "catalog"
    environment = "dev"
  }
}

module "billing" {
  source = "./modules/service"
  service = {
    name        = "billing"
    environment = "prod"
  }
}

output "catalog_name" {
  value = module.catalog.qualified_name
}

output "billing_name" {
  value = module.billing.qualified_name
}
Inspect the scenario data
{
  "intent": "A root module calls one reusable service module twice. Each caller supplies a service object with name and environment. Only dev and prod environments are accepted; expose a deterministic environment-name output.",
  "root_configuration": "module \"catalog\" {\n  source = \"./modules/service\"\n  service = {\n    name        = \"catalog\"\n    environment = \"dev\"\n  }\n}\n\nmodule \"billing\" {\n  source = \"./modules/service\"\n  service = {\n    name        = \"billing\"\n    environment = \"prod\"\n  }\n}\n\noutput \"catalog_name\" {\n  value = module.catalog.qualified_name\n}\n\noutput \"billing_name\" {\n  value = module.billing.qualified_name\n}\n"
}

2. Design the module contract

Loading the practice questions...

3. Implement the child module

Write modules/service/main.tf in the editor. Declare a typed object input named service with two string attributes, validate its environment, compute the qualified name, use it as the input of terraform_data.service and expose it as qualified_name. Keep the root caller unchanged.

modules/service/main.tfTerraform ยท HCL
Need a conceptual hint?

Declare the interface using an object type and a validation block. Use a local value to compute the name once. Declare an output so the caller can reference the result without knowing the resource's internal structure.

Compare a reference example after attempting
variable "service" {
  type = object({
    name        = string
    environment = string
  })

  validation {
    condition     = contains(["dev", "prod"], var.service.environment)
    error_message = "Environment must be dev or prod."
  }
}

locals {
  qualified_name = "${var.service.environment}-${var.service.name}"
}

resource "terraform_data" "service" {
  input = local.qualified_name
}

output "qualified_name" {
  value = local.qualified_name
}

Compare the input type, validation, local value and public output. The editor provides HCL self-comparison rather than native Terraform execution.

4. Explain your module design

Explain how the root composes modules, which values must be exposed as outputs, how provider requirements differ from configurations, and how plan-mode tests can verify this interface without applying infrastructure.

Continue your learning

Explore Module Master, module contract testing research, Configuration Debugging Challenge and learning methodology.

Technical references