How to indicate custom configuration files for terragrunt modules?

373 views Asked by At

I am trying to build Terragrunt script for deploying the infrastructure to Microsoft Azure cloud. Things are working fairly well but I am not able to figure out one thing.

The structure of setup looks something like this:

rootdir
  terragrunt.hcl
  someconfig.hcl
    module1dir
      terragrunt.hcl
      config.auto.tfvars.json
    module2dir   
      terragrunt.hcl
      config.auto.tfvars.json
    module3dir   
      terragrunt.hcl
      config.auto.tfvars.json

Each module is configured using Terraform autoload tfvars feature with config.auto.tfvars.json. What I would like is to have these files outside of the directory structure and somehow instruct Terragrunt to apply correct external configuration file to correct submodule.

Any ideas?

1

There are 1 answers

0
Darko Miletic On BEST ANSWER

I solved this in the following manner:

Define environment variable you plan on using which should contain location to the configuration files. Make sure it is not clashing with anything existing. In this example we will use TGR_CFGDIR. In the external configuration module place the module configuration files and make sure they are properly named. Each file should be named as the module and end with .auto.tfvars.json. So if your module is named foo you should have config file foo.auto.tfvars.json. Change your terragrunt modules (terragrunt.hcl) to have these statements:

locals {
  moduleconfig = get_env("TGR_CFGDIR")
  modulename   = basename(get_terragrunt_dir())
}

generate "configuration" {
  path              = "config.auto.tfvars.json"
  if_exists         = "overwrite"
  disable_signature = true
  contents          = file("${local.moduleconfig}/${local.modulename}.auto.tfvars.json")
}

And finally call terragrunt cli like this:

TGR_CFGDIR="<configdir>" terragrunt "<somecommand>"