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
1resource "aws_instance" "web_server" {2 # ...3}1resource "aws_instance" "aws_web_server" {2 # ...3}1resource "aws_instance" "web_server_instance" {2 # ...3}Use this for Generic Resources:
1resource "aws_db_instance" "this" {2 # ...3}1resource "aws_db_instance" "my_db_instance" {2 # ...3}1resource "aws_db_instance" "db_instance" {2 # ...3}Use Descriptive Names
1resource "aws_instance" "web_server" {2 # ...3}1resource "aws_instance" "database_server" {2 # ...3}1resource "aws_instance" "instance" {2 # ...3}Use Singular Nouns
1resource "aws_instance" "web_server" {2 # ...3}1resource "aws_instances" "web_servers" {2 # ...3}1resource "aws_instance" "dbs_servers" {2 # ...3}Use underscores for Multi-word Names
1resource "aws_instance" "web_server" {2 # ...3}1resource "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.
1resource "aws_instance" "web_server" {2 count = 23
4 # arguments5
6 tags = merge(7 {8 Name = "..."9 },10 var.tags11 )12
13 lifecycle {14 create_before_destroy = true15 }16
17 depends_on = [aws_vpc.vpc]18}1resource "aws_instance" "web_server" {2 count = 23
4 # arguments5
6 lifecycle {7 create_before_destroy = true8 }9
10 tags = merge(11 {12 Name = "..."13 },14 var.tags15 )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.