Overview
Terraform configurations often use nested modules to modularize infrastructure, promote reusability, and maintain separation of concerns. In HCP Terraform, variable inheritance issues in nested modules can cause failed runs, unexpected plan outputs, or policy enforcement errors.
Symptoms
• Plan/Apply Errors:
Error: Missing required argument
on modules/network/main.tf line 12, in resource "aws_vpc" "main"
The argument "cidr_block" is required, but no definition was found.
Common Causes
1. Variables Not Passed Correctly from Root Module
source = "./modules/network"
# Missing cidr_block
}
2. Name Mismatch Between Root and Nested Module Variables
• Variable names must match between the root module and the nested module. Terraform does not automatically map differently named variables.
Example:
#Nested module variables.tf
variable "vpc_cidr" {
type = string
}
# Root module
module "network" {
source = "./modules/network"
cidr_block = var.default_cidr # Wrong key name
}
3. Missing Default Values in Nested Modules
4. Type Mismatch
Example:
type = list(string)
}
source = "./modules/compute"
subnet_ids = var.subnet_id # Incorrect type (string instead of list)
}
5. Workspace Variable Overrides
6. Sentinel Policy Dependencies
• Sentinel policies may depend on specific variable values (for tagging, naming, region enforcement). If variable inheritance fails, the policy evaluation will fail even if the configuration is correct.
Resolution Steps
Step 1: Analyze Error Messages
• Identify the resource and variable that triggered the failure.
• Check the error type: missing argument, type mismatch, or null value.
Step 2: Review Nested Module Interfaces
• Examine variables.tf in each module to confirm required variables, default values, and type constraints.
• Document the module interface for clarity.
Step 3: Ensure Proper Variable Passing
• Root module must explicitly pass required values to nested modules
module "network" {
source = "./modules/network"
vpc_cidr = var.vpc_cidr
environment = var.environment
}
• Use consistent naming conventions across root and nested modules to avoid confusion.
Step 4: Validate Type Consistency
• Verify that the type of the value passed matches the variable’s type.
• Use functions like tolist() or tomap() if conversion is needed.subnet_ids = tolist(var.subnet_ids)
Step 5: Check Workspace Variable Overrides
• Navigate to HCP Terraform workspace > Variables.
• Ensure workspace variables do not conflict with values passed from the root module.
• Remove or adjust workspace variables if they cause unexpected overrides.
Step 6: Test Modules Independently
• Run terraform plan on nested modules in isolation to confirm correct variable behavior.
• Use terraform console to inspect variable values at runtime.
Step 7: Verify Sentinel Policy Compliance
• Ensure that all required values expected by policies are correctly inherited.
• Run terraform plan with Sentinel policy evaluation in advisory mode to detect potential failures before blocking applies.