Read Anthropic’s case study about Graphite Reviewer

Best naming conventions for Terraform files

Kenny DuMez
Kenny DuMez
Graphite software engineer

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 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.

  • 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.

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.

Use underscores (_) instead of spaces or dashes to separate words. This maintains readability and avoids complications with tools that may misinterpret spaces or dashes.

  • load_balancer_setup.tf
  • security_group_rules.tf

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.

  • module "aws_vpc" {...} for a module managing AWS VPC resources.
  • module "azure_app_service" {...} for a module managing Azure App Services.

While being descriptive, also be concise. Avoid overly long or complex names that are hard to read or understand.

  • module "dns_config"
  • module "network_firewall"

Develop a consistent naming structure for resources and variables. Typically, you should start with the type, followed by the purpose.

  • aws_instance.web_server
  • google_compute_network.private_network

Use qualifiers that add useful information about the resource or variable, such as its environment, usage, or unique attributes.

  • s3_bucket_logs
  • vm_db_production

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.

  • Use network_interface instead of alternating between network_interface and net_interface.
  • Stick with application_load_balancer rather than using both alb and application_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.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2