Naming Conventions
Loading...
Naming Conventions
When naming locals in your codebase, it is important to follow a consistent naming convention. This will help you and your team understand the purpose of each local and make it easier to navigate the codebase.
Avoid Repeating Local Type
When naming locals, avoid repeating the local type in the name. For example, instead of naming a local aws_instance, you can simply name it (resource name)_(argument reference). This makes the code more readable and avoids redundancy.
- ✅
locals { db_instance_name = "mydb" } - ✅
locals { instance_type = "t2.micro" } - ❌
locals { db_instance_name = "mydb" } - ❌
locals { instance_type = "t2.micro" }
Leverage Resource Argument Standards
In resource modules, adhere to the standards set in the “Argument Reference” section for the relevant resource. Utilize name, description, and default values for locals.
- ✅
locals { db_instance_name = "mydb" } - ✅
locals { instance_type = "t2.micro" } - ❌
locals { db_name = "mydb" } - ❌
locals { ec2_type = "t2.micro" }
When To Use Local Values Over Variables
Use local values when you need to define a value that is used multiple times within a module. This can help reduce redundancy and make your code more readable.