1.2.5.1. local Blocks
To improve code readability, you can extract complex expressions or repeatedly used expressions into local expressions.
1.2.5.1.1. locals.tf Files Should Contain Only One locals Block
Sometimes we choose to create a locals.tf file to store widely referenced local expressions. Since these expressions are widely referenced, they don't seem to fit well anywhere else, so it's better to place them in locals.tf. All local expressions in this file should be defined within a single locals block.
1.2.5.1.2. local Expressions Should Be Sorted Alphabetically
1.2.5.1.3. local Expressions Should Use Precise Types Whenever Possible
For example, an object({ name=string, age=number }) type:
{
name = "John"
age = 52
}
is preferred over a map(string) type:
{
"name": "John"
"age": "52"
}
1.2.5.1.4. No Empty Lines Between Two local Expressions
1.2.5.1.5. Complex local Expressions Should Have Corresponding Tests Covering Various Scenarios
For complex local expressions, test code should serve as "runnable documentation." Module maintainers can understand the design intent, expected inputs and outputs, and other key information of complex local expressions by reading the test code.
We will discuss how to write unit tests in subsequent chapters.