MrDuk
MrDuk

Reputation: 18322

In this Terraform config, why do I need to require variables in multiple places?

My folder hierarchy looks like this:

/
  -> live/
    : main.tf
    : variables.tf
  -> modules/
    -> logs/
      : logs.tf
      : variables.tf

In my main.tf I have:

module "logs" {
  source = "../modules/logs"

  env = var.env
  ...
}

In my logs.tf I have:

resource "datadog_monitor" "logs-fatal" {
  env = var.env
  ...
}

In both variables.tf files I have:

variable "env" {default = "test"} 

If I remove this from either of them, I'm given an Unsupported argument error - why? It's unclear to me in this scenario which variable will be taking precedence and why both sets are needed.

Upvotes: 0

Views: 109

Answers (1)

Marcin Wyszynski
Marcin Wyszynski

Reputation: 2258

In Terraform, each directory is treated as a separate namespace, and can be reused as a module. As a rule of thumb you can assume that inside a directory you need to define all variables that are used in it.

Also, Terraform does not care about directory nesting, so even if a contains b, then b is entirely independent of a. If you want to include stuff from b from directory a, you need to pull in the whole thing as a module explicitly.

Upvotes: 1

Related Questions