Comment on page
Naming conventions
There should be no reason to not follow at least these conventions :)
Beware that actual cloud resources often have restrictions in allowed names. Some resources, for example, can't contain dashes, some must be camel-cased. The conventions in this book refer to Terraform names themselves.
- 1.Use
_
(underscore) instead of-
(dash) everywhere (in resource names, data source names, variable names, outputs, etc). - 2.Prefer to use lowercase letters and numbers (even though UTF-8 is supported).
- 1.Do not repeat resource type in resource name (not partially, nor completely):
`resource "aws_route_table" "public" {}`
`resource "aws_route_table" "public_route_table" {}`
`resource "aws_route_table" "public_aws_route_table" {}`
- 1.Resource name should be named
this
if there is no more descriptive and general name available, or if the resource module creates a single resource of this type (eg, in AWS VPC module there is a single resource of typeaws_nat_gateway
and multiple resources of typeaws_route_table
, soaws_nat_gateway
should be namedthis
andaws_route_table
should have more descriptive names - likeprivate
,public
,database
). - 2.Always use singular nouns for names.
- 3.Use
-
inside arguments values and in places where value will be exposed to a human (eg, inside DNS name of RDS instance). - 4.Include argument
count
/for_each
inside resource or data source block as the first argument at the top and separate by newline after it. - 5.Include argument
tags,
if supported by resource, as the last real argument, following bydepends_on
andlifecycle
, if necessary. All of these should be separated by a single empty line. - 6.When using conditions in an argument
count
/for_each
prefer boolean values instead of usinglength
or other expressions.
main.tf
resource "aws_route_table" "public" {
count = 2
vpc_id = "vpc-12345678"
# ... remaining arguments omitted
}
resource "aws_route_table" "private" {
for_each = toset(["one", "two"])