gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.04k stars 975 forks source link

Unable to reference dependencies module #1937

Closed hunkeelin closed 2 years ago

hunkeelin commented 2 years ago

My directory at the moment

.
├── terragrunt.hcl
└── vault
    └── terragrunt.hcl

terragrunt.hcl

include {
  path = find_in_parent_folders()
}
locals {
  // locals
}

terraform {
  source = // sources 
}

inputs = {
 // code
}

Then in my vault/terragrunt.hcl I have the following:

terraform {
  source = // source
}

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

generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite"
  contents  = <<EOF
provider "vault" {
  address         = "vault.test"
  skip_tls_verify = true
}
provider "postgresql" {
  host            = dependency.database.outputs.endpoint
  port            = 5432
  database        = "postgres"
  username        = dependency.database.outputs.username
  password        = dependency.database.outputs.password
  connect_timeout = 15
  sslmode         = "disable"
}
EOF
}

inputs = {
// inputs
}

When I run terragrunt run-all plan

The terragrunt.hcl on the root directory ran fine but the one from vault/terragrunt.hcl gave me the following error:

│ Error: Reference to undeclared resource
│ 
│   on provider.tf line 11, in provider "postgresql":
│   11:   password        = dependency.database.outputs.password
│ 
│ A managed resource "dependency" "database" has not been declared in
│ the root module.

Why I am unable to reference the module?

yorinasub17 commented 2 years ago

You need to escape with ${} so that it is treated as a terragrunt interpolation:

generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite"
  contents  = <<EOF
provider "vault" {
  address         = "vault.test"
  skip_tls_verify = true
}
provider "postgresql" {
  host            = "${dependency.database.outputs.endpoint}"
  port            = 5432
  database        = "postgres"
  username        = "${dependency.database.outputs.username}"
  password        = "${dependency.database.outputs.password}"
  connect_timeout = 15
  sslmode         = "disable"
}
EOF
}