This is intended to be a guide of Terraform syntax and general best practices.
As Terraform utilises HCL, you may wish to take a detailed look at its syntax guide.
Inspired by The Ruby Style Guide and The Puppet Style Guide.
Indentation should be 2 spaces (soft tabs). No hard tabs.
Attribute assignments (=
) should be aligned for clarity.
// bad
resource "aws_security_group" "main" {
name = "${var.name}"
description = "Security Group ${var.name}"
vpc_id = "${var.vpc_id}"
tags {
Name = "${var.name}"
}
}
// good
resource "aws_security_group" "main" {
name = "${var.name}"
description = "Security Group ${var.name}"
vpc_id = "${var.vpc_id}"
tags {
Name = "${var.name}"
}
}
Variables should be provided in a variables.tf
file at the root of your project.
A description should be provided for each declared variable.
// bad
variable "vpc_id" {}
// good
variable "vpc_id" {
description = "The VPC this security group will go in"
}
Outputs should be provided in an outputs.tf
file at the root of your project.
Only comment what is necessary.
Single line comments: #
or //
.
Multi-line comments: /*
followed by */
.