Skip to content

Naming Conventions

Loading...

Naming Conventions


When naming variables in your codebase, it is important to follow a consistent naming convention. This will help you and your team understand the purpose of each variable and make it easier to navigate the codebase.


Well-structured variable


When naming variables, avoid repeating the variable type in the name. For example, instead of naming a variable my_variable, you can simply name it {resource name}_{argument reference}. This makes the code more readable and avoids redundancy.

  • ✅ variable "db_instance_name"
  • ✅ variable "instance_type"
  • ❌ variable "db_instance_name"
  • ❌ variable "instance_type"

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 variables.

  • ✅ variable "db_instance_name" { description = "The name of the RDS instance." }
  • ✅ variable "instance_type" { description = "The type of EC2 instance to launch." }
  • ❌ variable "db_instance_name" { description = "The name of the RDS instance."}
  • ❌ variable "instance_type" { description = "The type of EC2 instance to launch."}

Types:


Utilize Type any for Flexibility:


When defining variables, use type = any for flexibility. This allows you to pass in any type of value to the variable.

  • ✅ variable "codepipeline_artifact_store" { type = any }

Prioritize Simple Types:


When defining variables, prioritize simple types such as string, number, bool, list(...), map(...), and any over complex types such as object({...}) unless strict constraints on each key are necessary.


Choose Specific Types for Homogeneous Maps:


When defining variables that are expected to be a map of homogeneous values, choose a specific type for the map. This makes the code more readable and helps catch errors early.

  • ✅ variable "tags" { type = map(string) }
  • ❌ variable "tags" { type = map(any) }

Opt for Plural Form in Variable Names


When naming variables that type is list(..) or map(..), use the plural form of the noun to indicate that the variable is a collection of values.

  • ✅ variable "security_groups"
  • ✅ variable "subnets"
  • ❌ variable "security_group"
  • ❌ variable "subnet"

Understand Validation Limitations:


Terraform does not support complex validation rules for variables. If you need to validate the value of a variable, you can use the validation block to define simple validation rules.

  • ✅ variable "instance_type" { validation { condition = var.instance_type != "t2.micro" error_message = "Invalid instance type." } }
  • ❌ variable "instance_type" { validation { condition = var.instance_type != "t2.micro" error_message = "Invalid instance type. Must be t2.micro." } }
  • ❌ variable "instance_type" { validation { condition = var.instance_type != "t2.micro" error_message = "Invalid instance type. Must be t2.micro." } }

Organize Variable Blocks:


When defining variables, organize them in blocks based on their purpose. This makes it easier to understand the variables and their purpose.

Order keys in a variable block as follows:

  • type
  • description
  • default
  • validation