Terragrunt: Terragrunt after_hook for apply called during output

Created on 8 May 2020  路  1Comment  路  Source: gruntwork-io/terragrunt

When calling terragrunt apply-all on a service (mod1) with a dependency block defined (mod2), the after_hook of mod2 is called twice. Once when it is applied, and then again when terraform output -json is called on it to apply mod1.

[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Detected 1 Hooks
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Executing hook: migrate_tfplan
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Running command: mv /home/tmoss/scratch/tg_bug/envs/mod2/tfplan /home/tmoss/scratch/tg_bug/envs/mod2/$
fplan.success
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Module /home/tmoss/scratch/tg_bug/envs/mod2 has finished successfully!
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod1] 2020/05/08 13:01:04 Dependency /home/tmoss/scratch/tg_bug/envs/mod2 of module /home/tmoss/scratch/tg_bug/envs/mod1 just f$
nished successfully. Module /home/tmoss/scratch/tg_bug/envs/mod1 must wait on 0 more dependencies.
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod1] 2020/05/08 13:01:04 Running module /home/tmoss/scratch/tg_bug/envs/mod1 now
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod1] 2020/05/08 13:01:04 Reading Terragrunt config file at /home/tmoss/scratch/tg_bug/envs/mod1/terragrunt.hcl
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Reading Terragrunt config file at /home/tmoss/scratch/tg_bug/envs/mod2/terragrunt.hcl
...
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Running command: terraform output -json
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Detected 1 Hooks
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Executing hook: migrate_tfplan
[terragrunt] [/home/tmoss/scratch/tg_bug/envs/mod2] 2020/05/08 13:01:04 Running command: mv /home/tmoss/scratch/tg_bug/envs/mod2/tfplan /home/tmoss/scratch/tg_bug/envs/mod2/t
fplan.success
mv: cannot stat '/home/tmoss/scratch/tg_bug/envs/mod2/tfplan': No such file or directory

This are what I think are the useful bits of the apply log. You can see that mod2 is applied, then mod1 reads that it's a dependent, and goes back into mod2 runs the terraform output -json command, at which point the hook fires again, and fails.

the parent hcl files looks like this:

terraform {
  extra_arguments "create_plan_files" {
    commands = [
      "plan",
    ]
    arguments = [
      "-out=${get_terragrunt_dir()}/tfplan",
    ]
  }
  after_hook "migrate_tfplan" {
    commands = ["apply"]
    execute = ["mv", "${get_terragrunt_dir()}/tfplan", "${get_terragrunt_dir()}/tfplan.success"]
    run_on_error = false
  }
}

Can upload the whole test setup to a repo, or somewhere if it helps.

EDIT:
Terraform: 0.12.20
Terragrunt: 0.22.4
I didn't see any issues reported that looked like this, so I assumed it hadn't been patched in the most, most up to date version of terragrunt, but can check if that helps

bug needs investigation

Most helpful comment

This does sound like a bug! I suspect the hooks are running whether a module is being called directly or just being used for outputs as part of a dependency block. If anyone has time to dig into this and submit a PR, we'd probably want (a) to pass some flag along when fetching outputs as part of a dependency block and (b) to update the shouldRunHook method to return false if that flag was passed.

>All comments

This does sound like a bug! I suspect the hooks are running whether a module is being called directly or just being used for outputs as part of a dependency block. If anyone has time to dig into this and submit a PR, we'd probably want (a) to pass some flag along when fetching outputs as part of a dependency block and (b) to update the shouldRunHook method to return false if that flag was passed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GauntletWizard picture GauntletWizard  路  3Comments

antonbabenko picture antonbabenko  路  3Comments

zachwhaley picture zachwhaley  路  3Comments

dingobar picture dingobar  路  3Comments

mmorianos picture mmorianos  路  3Comments