Proper naming conventions in Terraform not only improve the readability of your code but also makes your infrastructure easier to maintain and scale. By following consistent, descriptive naming practices, teams can more easily manage and collaborate on infrastructure as code projects. This guide will cover the best practices for naming Terraform files, modules, resources, and variables.
File naming conventions
1. Use meaningful, descriptive names
File names should clearly reflect their purpose. Avoid vague names like main.tf
or data.tf
across multiple modules without context. Instead, use specific descriptive names that describe what the file contains or does.
Examples:
networking.tf
for networking resources like VPCs, subnets, and gateways.compute_instances.tf
for resources related to compute instances.database_rds.tf
for RDS resources.
2. Keep it lowercase
Stick to lowercase to avoid issues with case-sensitive file systems, which can lead to confusion or errors in environments that differentiate file names based on case.
3. Use underscores to separate words
Use underscores (_
) instead of spaces or dashes to separate words. This maintains readability and avoids complications with tools that may misinterpret spaces or dashes.
Examples:
load_balancer_setup.tf
security_group_rules.tf
Module naming conventions
1. Reflect module purpose
Module names should clearly indicate what the module is intended to do or what it manages. This helps in understanding the infrastructure layout at a glance.
Examples:
module "aws_vpc" {...}
for a module managing AWS VPC resources.module "azure_app_service" {...}
for a module managing Azure App Services.
2. Use concise, descriptive names
While being descriptive, also be concise. Avoid overly long or complex names that are hard to read or understand.
Examples:
module "dns_config"
module "network_firewall"
Resource and variable naming conventions
1. Consistent naming structure
Develop a consistent naming structure for resources and variables. Typically, you should start with the type, followed by the purpose.
Examples:
aws_instance.web_server
google_compute_network.private_network
2. Use descriptive qualifiers
Use qualifiers that add useful information about the resource or variable, such as its environment, usage, or unique attributes.
Examples:
s3_bucket_logs
vm_db_production
3. Ensure consistency in resource naming
Maintain a consistent naming convention across all your Terraform resources to reduce confusion and enhance readability. This approach makes it easier for both new and existing team members to understand and manage the infrastructure.
Examples:
- Use
network_interface
instead of alternating betweennetwork_interface
andnet_interface
. - Stick with
application_load_balancer
rather than using bothalb
andapplication_load_balancer
interchangeably.
Adopting effective naming conventions for your Terraform files, modules, resources, and variables is crucial for maintaining a well-organized codebase. These conventions help in quicker navigation, easier maintenance, and better collaboration across teams. Always aim for clarity and consistency in your naming practices, and ensure that new team members are familiar with these conventions to maintain standards as your project evolves.
For further reading on Terraform, see the official Terraform documentation.