Best practice for defining values tor modules (vars vs locals)

Hi, I wanted to get some feedback on 2 possible ways to define variables for a module.
Hopefully the description is clear.

Case 1 seems cleaner, or at least more properly defined? However, every module variable needs to be defined twice (in main and in module).

Case 2 has less definitions, and feels less bloated. I want to avoid though definitions that are hacky or not clean.

Same between both Cases (var a1, var a2) (var b1)

Case 1

main (var a1, var a2, var b1).
main terraformv.tfars (a1=…, a2=…) - Module_A.a1=var.a1. etc

Case 2

main (locals{ a1=…, a2=…, b1=…} - Module_A.a1=local.a1. etc

I’m very new to TF, so I’m open to hear different opinions, or downsides to either implementation.


Use locals when the target audience for customizing the values is people working on the Terraform configuration code in a text editor.

Use variables when the target audience for customizing the values is a source outside of the Terraform itself - for example, if you want to use the same Terraform configuration code in multiple different Terraform Cloud workspaces, with different variable values.

1 Like