Table of Contents
This document provides coding conventions used in all Solid Potential repositories for Terraform's HashiCorp Configuration Language (HCL). Code convetion are introduced to adhere a style guide to ensure readable and high quality code.
Parameter definitions in a resource block should be aligned. The terraform fmt command can do this for you.
- Strings are in double-quotes.
Use 2 spaces when defining resources except when defining inline policies or other inline resources.
When commenting use a hash # and a space in front of the comment. We prefer # over // or /**/ for line comments. For delimiting section header comment blocks with # ----- <SECTION_NAME> -----.
The label for blocks should be in snake case and lowercase. i.e.: example_instance , not ExampleInstance or example-instance
Variables and outputs should be created based on following rules:
- Name - similarly to blocks both variables and outputs should be in snake case and lowercase. Additinally:
- Names should reflect the attribute or argument they reference in its suffix i.e.:
foo_bar_nameorfoo_bar_idbut notfoo_bar - Use plural form in names of variables that expect a collection of items i.e.:
foo_barsfor multiple items orfoo_barfor single item
- Names should reflect the attribute or argument they reference in its suffix i.e.:
- Description - each variable or output shoul contain meaningful description
- Type - each variable or output should contain defined type even if it is of type
string(default) - Defaults (Optional) - if a module should configure anything using miminal configuration use defaults in variables rather than hardcode values used in resources
The variables.tf file should be broken down into three sections with each section arranged alphabetically. Starting at the top of the file:
- Variables that have no defaults defined
- Variables that contain defaults
- All locals blocks
Use () to break up complex conditionals across multiple lines for better readability. i.e.:
locals {
is_prod = var.environment == "prod" ? true : false
instance_name = (
var.instance_name != ""
? var.instance_name
: "vm-${var.image_name}"
)
}
Create a separate resource file for each type resource. Similar or bundled resources should be defined in the same file and named accordingly.
Only use an underscore (_) when naming Terraform resources like TYPE/NAME parameters and variables.
resource "google_compute_instance" "vm_instance" {
...
}
Only use a hyphen (-) when naming the component being created.
resource "google_compute_instance" "vm_instance" {
name = "test-vm"
...
}
A resource's NAME should be the same as the TYPE minus the provider.
resource "google_storage_bucket" "storage_bucket" {
...
}
If there are multiple resources of the same TYPE defined, add a minimalistic identifier to differentiate between the two resources. A blank line should sperate resource definitions contained in the same file.
# Create Data Bucket
resource "google_storage_bucket" "data_storage_bucket" {
...
}
# Create Images Bucket
resource "google_storage_bucket" "images_storage_bucket" {
...
}