└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Terraform Style Guide 2 | 3 | This is intended to be a guide of Terraform syntax and general best practices. 4 | 5 | As Terraform utilises [HCL](https://github.com/hashicorp/hcl), you may wish to take a detailed look at its 6 | [syntax guide](https://github.com/hashicorp/hcl/blob/master/README.md#syntax). 7 | 8 | Inspired by [The Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) and 9 | [The Puppet Style Guide](https://docs.puppetlabs.com/guides/style_guide.html). 10 | 11 | ## Table of Contents 12 | 13 | * [Code Layout](#code-layout) 14 | * [Modules](#modules) 15 | * [Variables](#variables) 16 | * [Outputs](#outputs) 17 | * [Comments](#comments) 18 | 19 | ## Code Layout 20 | 21 | Indentation should be 2 spaces (soft tabs). No hard tabs. 22 | 23 | Attribute assignments (`=`) should be aligned for clarity. 24 | 25 | ```hcl 26 | // bad 27 | resource "aws_security_group" "main" { 28 | name = "${var.name}" 29 | description = "Security Group ${var.name}" 30 | vpc_id = "${var.vpc_id}" 31 | tags { 32 | Name = "${var.name}" 33 | } 34 | } 35 | 36 | // good 37 | resource "aws_security_group" "main" { 38 | name = "${var.name}" 39 | description = "Security Group ${var.name}" 40 | vpc_id = "${var.vpc_id}" 41 | tags { 42 | Name = "${var.name}" 43 | } 44 | } 45 | ``` 46 | 47 | ## Modules 48 | 49 | ## Variables 50 | 51 | Variables should be provided in a `variables.tf` file at the root of your project. 52 | 53 | A description should be provided for each declared variable. 54 | 55 | ```hcl 56 | // bad 57 | variable "vpc_id" {} 58 | 59 | // good 60 | variable "vpc_id" { 61 | description = "The VPC this security group will go in" 62 | } 63 | ``` 64 | 65 | ## Outputs 66 | 67 | Outputs should be provided in an `outputs.tf` file at the root of your project. 68 | 69 | ## Comments 70 | 71 | Only comment what is necessary. 72 | 73 | Single line comments: `#` or `//`. 74 | 75 | Multi-line comments: `/*` followed by `*/`. 76 | --------------------------------------------------------------------------------