Skip to content

Example

Loading...

Example


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


Avoid Repeating Resource Type


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

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

bad-example.tf
1
module "web_server_module" {
2
# ...
3
}

Use this for Generic Modules:


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

bad-example.tf
1
module "my_module" {
2
# ...
3
}

bad-example.tf
1
module "module" {
2
# ...
3
}

Use Descriptive Names


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

bad-example.tf
1
module "module" {
2
# ...
3
}

bad-example.tf
1
module "web" {
2
# ...
3
}

Use underscores for Multi-word Names


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

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

bad-example.tf
1
module "webServer" {
2
# ...
3
}

Order of Module Arguments Reference


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

good-example.tf
1
module "web_server" {
2
source = "./path/to/module"
3
4
count = 2
5
6
# ...
7
8
depends_on = [
9
aws_instance.web_server
10
]
11
}

bad-example.tf
1
module "web_server" {
2
count = 2
3
4
source = "./path/to/module"
5
6
# ...
7
8
depends_on = [
9
aws_instance.web_server
10
]
11
}

Use source for External Modules


When using external modules, use the source argument to specify the path to the module.

good-example.tf
1
module "web_server" {
2
source = "git@github.com:example/module.git//src/module?ref=v1.0.0"
3
4
count = 2
5
6
# ...
7
8
depends_on = [
9
aws_instance.web_server
10
]
11
}

bad-example.tf
1
module "web_server" {
2
source = "https://github.com/example/module.git//src/module?ref=v1.0.0"
3
4
count = 2
5
6
# ...
7
8
depends_on = [
9
aws_instance.web_server
10
]
11
}

Conclusion


In this example, we have created a naming convention for Modules in a Terraform codebase. By following these conventions, you can make your code more readable and consistent, and easier to maintain and scale over time.