gruntwork-io / terragrunt-infrastructure-live-example

A repo used to show examples file/folder structures you can use with Terragrunt and Terraform
https://www.gruntwork.io/
Apache License 2.0
783 stars 472 forks source link

Unable to get locals value from child account.hcl file #82

Open yashraj-unthinkable opened 2 years ago

yashraj-unthinkable commented 2 years ago

image

fabricesemti80 commented 1 year ago

I have the same issue - any suggestions how to resolve..?

yashraj-unthinkable commented 1 year ago

@fabricesemti80 you can use terragrunt mock outputs https://terragrunt.gruntwork.io/docs/features/execute-terraform-commands-on-multiple-modules-at-once/#unapplied-dependency-and-mock-outputs

Ferroman commented 1 year ago

Can you a bit elaborate how mock outputs can help with this case? Judging by the error, Terragrunt can't find configurations,. I'm not quite understand how mocking outputs are related to the find_in_parent_folders behaviour.

pmaterer commented 2 months ago

This appears to still be an issue, and it seems using the structure in this repo doesn't work with Terragrunt.

I'm using:

❯ terragrunt --version                                
terragrunt version 0.66.9

Here is my directory layout:

~/tmp/example on ☁️  materer-dev (us-east-1) 
❯ tree                   
.
├── dev
│   ├── account.hcl
│   └── key
│       └── terragrunt.hcl
├── prod
│   ├── account.hcl
│   └── key
│       └── terragrunt.hcl
└── terragrunt.hcl

5 directories, 5 files

And the files:

File: terragrunt.hcl
Path: ./terragrunt.hcl

Contents:
locals {
  account_info = read_terragrunt_config(find_in_parent_folders("account.hcl"))
  profile = local.account_info.locals.profile
}

generate "provider" {
    path = "provider.tf"
    if_exists = "overwrite_terragrunt"
    contents = <<EOF
provider "aws" {
    region = "us-east-1"
    profile = "${local.profile}"
}
EOF
}

----------------------------------------

File: account.hcl
Path: ./prod/account.hcl

Contents:
locals {
    profile = "materer-prod"
}

----------------------------------------

File: terragrunt.hcl
Path: ./prod/key/terragrunt.hcl

Contents:
include "root" {
  path = find_in_parent_folders()
}

terraform {
  source = "tfr:///terraform-aws-modules/kms/aws?version=3.1.0"
}

inputs  = {
    description = "Test key"
}

----------------------------------------

File: account.hcl
Path: ./dev/account.hcl

Contents:
locals {
    profile = "materer-dev"
}

----------------------------------------

File: terragrunt.hcl
Path: ./dev/key/terragrunt.hcl

Contents:
include "root" {
  path = find_in_parent_folders()
}

terraform {
  source = "tfr:///terraform-aws-modules/kms/aws?version=3.1.0"
}

inputs = {
    description = "Test key"
}

----------------------------------------

And the error I get:

❯ terragrunt run-all init
ERRO[0000] Error: Error in function call

ERRO[0000]   on /Users/pmaterer/tmp/example/terragrunt.hcl line 2, in locals: 
ERRO[0000]    2:   account_info = read_terragrunt_config(find_in_parent_folders("account.hcl")) 
ERRO[0000]                                              
ERRO[0000] Call to function "find_in_parent_folders" failed: ParentFileNotFoundError: Could not find a account.hcl in any of the parent folders of /Users/pmaterer/tmp/example/terragrunt.hcl. Cause: Traversed all
the way to the root..

ERRO[0000] Error processing module at '/Users/pmaterer/tmp/example/terragrunt.hcl'. How this module was found: Terragrunt config file found in a subdirectory of /Users/pmaterer/tmp/example. Underlying error: /Users/pmaterer/tmp/example/terragrunt.hcl:2,41-64: Error in function call; Call to function "find_in_parent_folders" failed: ParentFileNotFoundError: Could not find a account.hcl in any of the parent folders of /Users/pmaterer/tmp/example/terragrunt.hcl. Cause: Traversed all the way to the root.. 
ERRO[0000] Unable to determine underlying exit code, so Terragrunt will exit with error code 1