Skip to content

Naming Conventions Example

Loading...

Naming Conventions Example


In this example, we will create a naming convention for resources in a Terraform codebase. We will use the following naming conventions:


Avoid Repeating Resource Type


good-example.tf
1
resource "aws_instance" "web_server" {
2
# ...
3
}

bad-example.tf
1
resource "aws_instance" "aws_web_server" {
2
# ...
3
}

bad-example.tf
1
resource "aws_instance" "web_server_instance" {
2
# ...
3
}

Use this for Generic Resources:


good-example.tf
1
resource "aws_db_instance" "this" {
2
# ...
3
}

bad-example.tf
1
resource "aws_db_instance" "my_db_instance" {
2
# ...
3
}

bad-example.tf
1
resource "aws_db_instance" "db_instance" {
2
# ...
3
}

Use Descriptive Names


good-example.tf
1
resource "aws_instance" "web_server" {
2
# ...
3
}

good-example.tf
1
resource "aws_instance" "database_server" {
2
# ...
3
}

bad-example.tf
1
resource "aws_instance" "instance" {
2
# ...
3
}

Use Singular Nouns


good-example.tf
1
resource "aws_instance" "web_server" {
2
# ...
3
}

bad-example.tf
1
resource "aws_instances" "web_servers" {
2
# ...
3
}

bad-example.tf
1
resource "aws_instance" "dbs_servers" {
2
# ...
3
}

Use underscores for Multi-word Names


good-example.tf
1
resource "aws_instance" "web_server" {
2
# ...
3
}

bad-example.tf
1
resource "aws_instance" "web-server" {
2
# ...
3
}

Order of Resource Arguments Reference


When naming resources, follow the order of arguments in the resource block. This makes it easier to reference the resource in other parts of the codebase.

good-example.tf
1
resource "aws_instance" "web_server" {
2
count = 2
3
4
# arguments
5
6
tags = merge(
7
{
8
Name = "..."
9
},
10
var.tags
11
)
12
13
lifecycle {
14
create_before_destroy = true
15
}
16
17
depends_on = [aws_vpc.vpc]
18
}
bad-example.tf
1
resource "aws_instance" "web_server" {
2
count = 2
3
4
# arguments
5
6
lifecycle {
7
create_before_destroy = true
8
}
9
10
tags = merge(
11
{
12
Name = "..."
13
},
14
var.tags
15
)
16
17
depends_on = [aws_vpc.vpc]
18
}

Conclusion


By following these naming conventions, you can create a consistent and readable codebase that is easy to understand and maintain. This will make it easier for you and your team to work on the codebase and collaborate effectively.