Naming Conventions
Loading...
Naming Conventions
When naming resources in your codebase, it is important to follow a consistent naming convention. This will help you and your team understand the purpose of each resource and make it easier to navigate the codebase.
Avoid Repeating Resource Type
When naming resources, avoid repeating the resource type in the name. For example, instead of naming a resource aws_instance, you can simply name it web_server. This makes the code more readable and avoids redundancy.
- ✅
resource "aws_instance" "web_server" - ❌
resource "aws_instance" "aws_web_server" - ❌
resource "aws_instance" "web_server_instance"
Use this for Generic Resources:
When naming generic resources, use this as a if the resource module creates a single resource of this type and it is not necessary to give it a specific name.
- ✅
resource "aws_db_instance" "this" - ❌
resource "aws_db_instance" "my_db_instance" - ❌
resource "aws_db_instance" "db_instance"
Use Descriptive Names
Use descriptive names for resources to make it clear what the resource is used for. For example, instead of naming a resource instance, you can name it web_server_instance.
- ✅
resource "aws_instance" "web_server" - ✅
resource "aws_instance" "database_server" - ❌
resource "aws_instance" "instance" - ❌
resource "aws_instance" "server"
Use Singular Nouns
When naming resources, use singular nouns instead of plural nouns. This makes the code more readable and consistent.
- ✅
resource "aws_instance" "web_server" - ❌
resource "aws_instances" "web_servers" - ❌
resource "aws_instance" "dbs_servers"
Use Lowercase Letters
When naming resources, use lowercase letters and separate words with underscores. This makes the code more readable and consistent.
- ✅
resource "aws_instance" "web_server" - ❌
resource "aws_instance" "Web_Server" - ❌
resource "aws_instance" "WebServer"
Use underscores for Multi-word Names
Use underscores to separate words in resource names. This makes the code more readable and consistent.
- ✅
resource "aws_instance" "web_server" - ❌
resource "aws_instance" "web-server" - ❌
resource "aws_instance" "web.server"
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}