terramate-io / terramate

Terramate CLI is an open-source Infrastructure as Code (IaC) Orchestration and Code Generation tool for Terraform, OpenTofu and Terragrunt.
https://terramate.io
Mozilla Public License 2.0
3.12k stars 86 forks source link

[FEATURE] Do not evaluate `lets` block if `condition` is false in `generate_file` and `generate_hcl` #1636

Open g13013 opened 2 months ago

g13013 commented 2 months ago

Is your feature request related to a problem? Please describe. The current behavior of the lets block is that it's evaluated all the time regardless of the condition result, this leads errors in some scenarios when lets block depends on whether the condition is true or false, the problem becomes more annoying when the lets bloc have many variables that depend on the same condition

example

generate_hcl 'some_file.tf' {
  condition = length(global.data.vnets) > 0 && global.someOtherCondition

  lets: { // Repeated condition
    data   = global.someOtherCondition ?  existantValue ? nonExistantValue
    data2 = global.someOtherCondition ? existantValue2 ? nonExistantValue2
  }
}

Describe the solution you'd like The lets block or any other block should not be evaluated if the condition is false

generate_hcl 'some_file.tf' {
  condition = length(global.data.vnets) > 0 && global.someOtherCondition

  lets: {
    data   = existantValue
    data2 = existantValue2
  }
}
i4ki commented 2 months ago

Hi @g13013

We are aware of this issue. About your proposed solution, what if the condition depends on any let variable? Would it makes sense to only evaluate the condition and any dependent variable?

In our experience, usually the condition can get complex then being able to create temporary let variables would be good.

mariux commented 2 months ago

We are planning to improve the evaluation of variables, so we only evaluate needed parts.

g13013 commented 2 months ago

@i4ki

We are aware of this issue. About your proposed solution, what if the condition depends on any let variable? Would it makes sense to only evaluate the condition and any dependent variable?

Even better, it makes a lot of sense and it would awesome to have this !