# Automatically injecting module parameters

**URL:** <https://discuss.hashicorp.com/t/automatically-injecting-module-parameters/2548>\
**Category:** Terraform\
**Created:** [August 26, 2019, 12:53pm UTC](https://discuss.hashicorp.com/t/automatically-injecting-module-parameters/2548 "2019-08-26T12:53:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gtirloni](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gtirloni/32/1363_2.png) [@gtirloni](https://discuss.hashicorp.com/u/gtirloni)\
**Post date:** [August 26, 2019, 12:53pm UTC](https://discuss.hashicorp.com/t/automatically-injecting-module-parameters/2548/1 "2019-08-26T12:53:42Z")

</div>

I’m thinking about ways to decrease the amount of boilerplate code when using modules.

There are certain parameters that ALL our modules require. Things like “environment” or “region”, so they can be used to name things created by the module.

Would it be possible for a module to access the caller’s environment? This sounds awful from a security perspective but is it possible at all?

Failing that, is there any mechanism that would allow for automatically injecting certain input parameters to every module?

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [August 26, 2019, 5:25pm UTC](https://discuss.hashicorp.com/t/automatically-injecting-module-parameters/2548/2 "2019-08-26T17:25:22Z")

</div>

Hi @gtirloni,

The closest thing is to group all of these “cross-cutting variables” into a single object variable so that it appears only as a single line in your module calls:

```auto
variable "context" {
  type = object({
    environment = string
    region = string
  })
}

```

Then in the calling module:

```auto
variable "environment" {
  type = string
}

variable "region" {
  type = string
}

locals {
  shared_context = {
    environment = var.environment
    region = var.region
  }
}

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

  context = local.shared_context
  # etc, etc
}

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

  context = local.shared_context
  # etc, etc
}

```

Terraform requires explicit passing so that the data flow is explicit between modules, due to the principle of favoring the reader over the writer unless some other need overrides that.

However, this approach (of grouping multiple values together into a single object by topic) also lends itself well to later refactoring to adopt [the Module Composition patterns](https://www.terraform.io/docs/modules/composition.html): if your idea of “environment” is described by a conventional object type then you might later refactor to make “the environment” be managed in a separate configuration from more specific bits of infrastructure and then use [a data-only module](https://www.terraform.io/docs/modules/composition.html#data-only-modules) to retrieve that environment data. If that module’s outputs match the environment object type you defined then you can use the module object as a whole to populate the variable:

```auto
module "environment_context" {
  # It's a "soft convention" to give data-only modules
  # a prefix like "join-", to avoid potential naming conflicts
  # with a module that _manages_ the environment.
  source = "./modules/join-environment"

  # whatever arguments are needed to choose an environment
}

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

  context = module.environment_context
  # etc, etc
}

```

---

<div class="post-metadata">

**Author:** ![gtirloni](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gtirloni/32/1363_2.png) [@gtirloni](https://discuss.hashicorp.com/u/gtirloni)\
**Post date:** [August 27, 2019, 2:59pm UTC](https://discuss.hashicorp.com/t/automatically-injecting-module-parameters/2548/3 "2019-08-27T14:59:57Z")

</div>

Very interesting, thanks a lot! I’ll experiment with these ideas here.
