Skip to content

get_parent_terragrunt_dir() returns wrong path #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tiwood opened this issue Jun 18, 2019 · 11 comments
Closed

get_parent_terragrunt_dir() returns wrong path #756

tiwood opened this issue Jun 18, 2019 · 11 comments
Labels
enhancement New feature or request

Comments

@tiwood
Copy link
Contributor

tiwood commented Jun 18, 2019

Hi,

I'm currently in the process of upgrading our Terragrunt/Terraform setup.

We have the following folder structure:

.
├── environments
│   └── core
│       └── workloads
│           ├── terragrunt.hcl
│           └── workloads.tf.tpl
├── global
│   ├── foo
│   │   └── bar.ps1
└── terragrunt.hcl

We have hooks in the parent terragrunt.hcl as well as in the terragrunt.hcl in the workloads folder.

Our hook (defined in the child terragrunt.hcl) should run a script in the global folder but fails as the wrong path is provided by get_parent_terragrunt_dir(). This is the hook definition:

before_hook "prepare_workloads" {
    commands = get_terraform_commands_that_need_vars()

    execute = [
      "pwsh",
      "-nop",
      "-nol",
      "-File",
      "${get_parent_terragrunt_dir()}/global/foo/bar.ps1",
    ]

    run_on_error = false
  }

This fails as get_parent_terragrunt_dir() returns /environments/core/workloads instead of /.

I've created a test hook with both interpolations:

before_hook "test" {
    commands = get_terraform_commands_that_need_vars()

    execute = [
      "echo",
      "${get_terragrunt_dir()}",
      "${get_parent_terragrunt_dir()}"
    ]

    run_on_error = false
  }

This is the output:

[terragrunt] 2019/06/18 21:14:55 Executing hook: test
[terragrunt] 2019/06/18 21:14:55 Running command: echo /environments/core/workloads /environments/core/workloads
/environments/core/workloads /environments/core/workloads

I've tried both from the parent file with terragrunt plan-alland with terragrunt in the workloads folder.

@yorinasub17
Copy link
Contributor

Can you share the contents of the include block in the child's terragrunt.hcl file?

@tiwood
Copy link
Contributor Author

tiwood commented Jun 21, 2019

Sure, this is from the child terragrunt.hcl:

include {
  path = find_in_parent_folders()
}

@yorinasub17 yorinasub17 added the bug Something isn't working label Jun 21, 2019
@yorinasub17
Copy link
Contributor

Thanks I was able to reproduce this and this is indeed a bug. Will start working on a fix.

@yorinasub17 yorinasub17 added enhancement New feature or request help wanted and removed bug Something isn't working labels Jun 21, 2019
@yorinasub17
Copy link
Contributor

Actually, I just realized that this was a misunderstanding on my part. After starting the implementation and digging deeper into the code and rereading our docs, I realized that the get_parent_terragrunt_dir only works in the included config. In other words, this functions is meant to be used in the context of the parent terragrunt.hcl file, not the child's terragrunt file.

Instead, you should actually reuse find_in_parent_folders in combination with dirname (dirname(find_in_parent_folders())) which was included in tg 0.19.4. If you need an absolute path, you can combine it with get_terragrunt_dir: ${get_terragrunt_dir()}/${dirname(find_in_parent_folders())}.

@yorinasub17
Copy link
Contributor

I can see a function that returns the absolute path to the parent terragrunt config folder being useful, so I will keep this open, but since there is a workaround, I will not be implementing that at the moment. I might pick this up again if I have time in a few weeks.

@brikis98
Copy link
Member

Is this the same issue fixed by #753?

@yorinasub17
Copy link
Contributor

yorinasub17 commented Jun 21, 2019

Ah I think you are right @brikis98 ! When I was digging in, I was seeing that include var was null, and it looks like that PR fixes that.

@brikis98
Copy link
Member

Could you give https://github.com/gruntwork-io/terragrunt/releases/tag/v0.19.5 a shot (new binaries should show up shortly)?

@seppi91
Copy link

seppi91 commented Sep 25, 2019

Hi,
I think my issue is similar to the one of OP
terragrunt version v0.19.24
Terraform v0.12.6

.
├── environments
│   ├── rc.yml
│   └── dev.yml
│── eu-central-1
│   └── component
│          └── terragrunt.hcl
└── terragrunt.hcl

my parent terragrunt.hcl has something like this:

...
locals {
  env = yamldecode(file("${get_terragrunt_dir()}/${find_in_parent_folders(join("/", ["environments",get_env("ENVIRONMENT", "dev.yml")]))}"))
}

inputs = {
  environment_code = local.env.naming.environment_code
...

as locals are not propagated to child terragrunt.hcl I have to parse the yaml file again. So my child terragrunt.hcl looks like

include {
  path = find_in_parent_folders()
}

...

locals {
  env = yamldecode(file("${get_terragrunt_dir()}/${find_in_parent_folders(join("/", ["environments",get_env("ENVIRONMENT", "dev.yml")]))}"))
}

inputs = {
 app_settings = merge(local.env.vars, {
    "LOG_LEVEL"       = 2
  })
}

when running terragrunt plan within the child working directory everything runs smooth. But when doing a terragrunt plan-all from the parent dir, it breaks (replaced the full path output with placeholders):

Call to function "find_in_parent_folders" failed: ParentFileNotFound: Could not find a environments/dev.yml in any of the parent folders of
<PATH_TO_ROOT_TERRAGRUNT> Cause: Traversed all the way to the root..

[terragrunt] [<PATH_TO_ROOT_TERRAGRUNT_DIR>] 2019/09/25 17:44:20 Encountered error while evaluating locals.
[terragrunt] 2019/09/25 17:44:20 Error processing module at '<PATH_TO_ROOT_TERRAGRUNT>'. How this module was found: Terragrunt config file found in a subdirectory of <PATH_TO_ROOT_TERRAGRUNT_DIR>. Underlying error: <PATH_TO_ROOT_TERRAGRUNT>:47,52-75: Error in function call; Call to function "find_in_parent_folders" failed: ParentFileNotFound: Could not find a environments/dev.yml in any of the parent folders of <PATH_TO_ROOT_TERRAGRUNT>. Cause: Traversed all the way to the root..
[terragrunt] 2019/09/25 17:44:20 Unable to determine underlying exit code, so Terragrunt will exit with error code

@yorinasub17
Copy link
Contributor

Ah there is a known issue where you can't use the terragrunt directory related features in locals, because of the parsing order: include is parsed AFTER locals, not before, so unfortunately, you can't actually use functions like get_terragrunt_dir currently. See #814 (comment).

The goal is to introduce a concept like globals (#814) which would address this.

@yorinasub17
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants