Variables in terraform.tfvars in root module not passed to child module

Hi,

I have a root module that calls a child module as so:

Root module:

main.tf:

module "repo-1" {
  source = "../../terraform-github-repository"

  repository_name             = "example-repo-1"
}

variables.tf:

variable "repository_defaults" {
  description = "Default repository Settings"
  default     = {}
}

terraform.tfvars:

repository_defaults = {
  allow_auto_merge = true
}

Child module:

main.tf:

locals {
  allow_auto_merge = lookup(var.repository_defaults, "allow_auto_merge", var.repository_allow_auto_merge)
}

resource "github_repository" "main" {
  name = var.repository_name

  allow_auto_merge = local.allow_auto_merge
}

variables.tf:

variable "repository_name" {
  description = "The name of the repository."
  type        = string
}

variable "repository_defaults" {
  description = "Default repository Settings"
  default     = {}
}

variable "repository_allow_auto_merge" {
  description = "Allow auto-merging pull requests on the repository."
  default     = null
  type        = bool
}

I can not seem to get the value of var.repository_defaults, "allow_auto_merge" to be used by the root module when calling the child module?

What am I missing in how terraform.tfvars is used in a root module?

Thanks so much for any pointers you can provide.

Hi @tracphil,

It does not look like you are setting the module variable in the module call, so it’s only going to have its default value. If you want to pass the repository_defaults variable from the root module to the repo-1 module, then you need to assign it in the module call just like you do with repository_name

  repository_defaults = var.repository_defaults

@jbardin,

Thank you so much!

For future reference, should anyone need it, this is what I did to make it work:

module "repo-1" {
  source = "../../terraform-github-repository"

  repository_name    = "example-repo-1"
  repository_defaults = var.repository_defaults
}