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
7.93k stars 965 forks source link

terragrunt hooks and extra arguments - functions invoked many times - regardless of commands. #2218

Open skakala opened 2 years ago

skakala commented 2 years ago

example 1: lets code 2 hooks in terraform block - separeta for plan and apply:

  before_hook "before_hook_plan" {
    commands     = ["plan"]
    execute      = [
              "echo",
              run_cmd("echo", "running plan")
     ]
    run_on_error = false
  }

  before_hook "before_hook_apply" {
    commands     = ["apply"]
    execute      = [
              "echo",
              run_cmd("echo", "running apply")
     ]
    run_on_error = false
  }

then run plan:

pl60117:skala-dev-mongodb-d1$ terragrunt plan
running plan                                                                   <---- invoked before hook
running apply                                                                  <---- invoked before hook
INFO[0000] Executing hook: before_hook_plan              prefix=[/home/pl60117/Praca/GIT_IBM/GitOps-Terraform_infras/Projects/SKALA/nonprod/skala-dev-mongodb-d1] 
running plan                                                                    <----  proper hook execution
Acquiring state lock. This may take a few moments...

as You can see simple echo command was run 3 times (this was simplified example..)

The same is for extra_arguments

    extra_arguments "set_admin_pass_plan" {
    commands = [
      "plan"
]
    env_vars = {
      TF_VAR_password = run_cmd("echo", "running plan")

    }
  }

      extra_arguments "set_admin_pass_apply" {
    commands = [
      "apply"
]
    env_vars = {
      TF_VAR_password = run_cmd("echo", "running apply")
    }
  }

lets run terragrunt plan :

pl60117:skala-dev-mongodb-d1$ terragrunt plan
running plan
running apply
Acquiring state lock. This may take a few moments...
data.ibm_resource_group.group: Reading...

So run_cmd runs for "apply" command even if terragrunt plan only was invoked...

denis256 commented 2 years ago

Hi, AFAIK run_cmd is evaluated when HCL is parsed so it is invoked in any case

skakala commented 2 years ago

Hi, thanks! - according to first example run_cmd is executed both at HCL parsing level, and then again at hook level, proper behaviour is at hook level. IMAO this should be marked as bug as well.