gruntwork-io/terragrunt-infrastructure-live-example

Module dependencies example

Vad1mo opened this issue · 2 comments

based on this example here a created two modules that have dependency to each other. DB to APP.

RDS Module has dependencies to my App Module with vpc_id and subnets.

Based on dependencies section in the docs I created to following setup:
But when I run plan-all in stage I get asked to enter vpc_id and subnets.which should not be the case.

myProject: 
  modules:
    app:
      - main.tf
      - terragrunt.hcl # empty file
    rds:
      - main.tf
      - terragrunt.hcl 
  prod:
  stage:
    eu-west-1:
      db:
        - terragrunt.hcl

The content of myProject/modules/rds/terragrunt.hcl is

dependency "vpc" {
  config_path = "../app"
}

inputs = {
  vpc_id     = dependency.vpc.outputs.vpc_id
  subnet_ids = dependency.vpc.outputs.private_subnets
}

This is the content of my myProject/stage/eu-west-1/db/terragrunt.hcl

locals {
  environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
  region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
  env        = local.environment_vars.locals.environment
  aws_region = local.region_vars.locals.aws_region
}

terraform {
  source = "${get_parent_terragrunt_dir()}/modules/rds/"
}
include {
  path = find_in_parent_folders()
}

inputs = {
  name = "sample-rds"
}

Maybe you could extend your example project and add module dependencies.

Hmm it looks like you have a terragrunt.hcl file in modules/rds, which you reference as the terraform source in another terragrunt.hcl file.

Terragrunt doesn't do any configuration processing of the module pointed to by the terraform block source attribute, as it assumes that is a terraform module. So it won't pick up the terragrunt.hcl file in your modules/rds folder, which is why it is not getting any dependencies.

If you look at the examples, you will notice that the infrastructure-modules repo only contains terraform code, and all the terragrunt config lives here (and points only to the infrastructure-modules repo). Part of doing that is to make it extra clear that there should be no terragrunt configuration in the modules.

Given all that, to do what you want:

  • Have the dependency block and inputs attributes in myProject/stage/eu-west-1/db/terragrunt.hcl
  • If you want to make that reusable, you need to use read_terragrunt_config to import it in, or wait for import blocks

thx, somehow I got the impression it was also possible to do dependencies on modules.