Terragrunt: Is it possible to DRY-up references to dependencies?

Created on 21 Aug 2020  ยท  1Comment  ยท  Source: gruntwork-io/terragrunt

Hi. I have a use-case that I haven't been able to figure out how to DRY up.

Consider the following directory layout.

โ”œโ”€โ”€ account.hcl
โ”œโ”€โ”€ develop
โ”‚ย ย  โ””โ”€โ”€ aws
โ”‚ย ย      โ””โ”€โ”€ us-west-2
โ”‚ย ย          โ”œโ”€โ”€ app1
โ”‚ย ย          โ”‚ย ย  โ”œโ”€โ”€ app
โ”‚ย ย          โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย          โ”‚ย ย  โ”œโ”€โ”€ env.hcl
โ”‚ย ย          โ”‚ย ย  โ”œโ”€โ”€ mysql
โ”‚ย ย          โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย          โ”‚ย ย  โ””โ”€โ”€ support
โ”‚ย ย          โ”‚ย ย      โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย          โ”œโ”€โ”€ app2
โ”‚ย ย          โ”‚ย ย  โ”œโ”€โ”€ app
โ”‚ย ย          โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย          โ”‚ย ย  โ”œโ”€โ”€ env.hcl
โ”‚ย ย          โ”‚ย ย  โ”œโ”€โ”€ mysql
โ”‚ย ย          โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย          โ”‚ย ย  โ””โ”€โ”€ support
โ”‚ย ย          โ”‚ย ย      โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย          โ””โ”€โ”€ region.hcl
โ”œโ”€โ”€ production
โ”‚ย ย  โ”œโ”€โ”€ aos
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ app1
โ”‚ย ย  โ”‚ย ย      โ”œโ”€โ”€ app
โ”‚ย ย  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ secrets.yaml
โ”‚ย ย  โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย  โ”‚ย ย      โ”œโ”€โ”€ mysql
โ”‚ย ย  โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ support
โ”‚ย ย  โ”‚ย ย          โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย  โ”œโ”€โ”€ aws
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ ca-central-1
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ app1
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ app
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ secrets.yaml
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ seed.json
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ mysql
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ s3
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ sqs
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ app2
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ region.hcl
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ eu-west-2
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ region.hcl
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ us-west-2
โ”‚ย ย  โ”‚ย ย      โ”œโ”€โ”€ region.hcl
โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ uw
โ”‚ย ย  โ”‚ย ย          โ”œโ”€โ”€ app
โ”‚ย ย  โ”‚ย ย          โ””โ”€โ”€ mysql
โ”‚ย ย  โ”œโ”€โ”€ azure
โ”‚ย ย  โ””โ”€โ”€ gcp
โ”œโ”€โ”€ staging
โ”‚ย ย  โ””โ”€โ”€ us-west-2
โ”‚ย ย      โ”œโ”€โ”€ preview
โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ app
โ”‚ย ย      โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ env.hcl
โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ mysql
โ”‚ย ย      โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ support
โ”‚ย ย      โ”‚ย ย      โ””โ”€โ”€ terragrunt.hcl
โ”‚ย ย      โ””โ”€โ”€ region.hcl
โ””โ”€โ”€ terragrunt.hcl

In each of the ./app/terragrunt.hcl you will find:

include {
  path = find_in_parent_folders()
}

locals {
  env = read_terragrunt_config(find_in_parent_folders("env.hcl"))
}

dependency "mysql" {
  config_path = "../mysql"
}

dependency "support" {
  config_path = "../support"
}

terraform {
  source = "/path/to/modules//app"
}

inputs = {
  release_name = local.env.locals.environment
  values = {
    "inboxQueue" = dependency.support.inbox_queue_url
    "outboxQueue" = dependency.support.outbox_queue_url
    "externalDatabase.host" = dependency.mysql.outputs.host
    "externalDatabase.port" = dependency.mysql.outputs.port
    "externalDatabase.dbName" = dependency.mysql.outputs.db_name
    "externalDatabase.user" = dependency.mysql.outputs.username
  }
  secrets = {
    "externalDatabase.password" = dependency.mysql.outputs.password
  }
}

Ideally, I would be able to DRY up the usage of dependency and inputs that utilizes the outputs from said dependencies.

Is this possible?

enhancement

Most helpful comment

This is not possible yet. You sort of can do this with read_terragrunt_config, but it is not recommended due to https://github.com/gruntwork-io/terragrunt/issues/1128.

We plan on supporting this use case in the upcoming imports block feature but we have not had the chance to fully implement it yet. We will update this ticket when that is implemented.

>All comments

This is not possible yet. You sort of can do this with read_terragrunt_config, but it is not recommended due to https://github.com/gruntwork-io/terragrunt/issues/1128.

We plan on supporting this use case in the upcoming imports block feature but we have not had the chance to fully implement it yet. We will update this ticket when that is implemented.

Was this page helpful?
0 / 5 - 0 ratings