Obay’s Terraform Naming Convention (OTN) is a standardized approach to organizing Terraform files that promotes clarity, consistency, and maintainability in infrastructure-as-code projects. The core principle is simple: one block per file, named according to the block type and identifier.
Core Principles#
- One Block Per File: Each Terraform file contains exactly one resource, data source, provider, variable, output, or module block
- Predictable Naming: File names follow a consistent pattern based on the block type and name
- Self-Documenting: File names immediately indicate what infrastructure component they define
- Tool-Supported: The tfren tool automates file renaming according to this convention
Naming Patterns#
Resources and Data Sources#
{block_type}.{resource_type}.{resource_name}.tfExamples:
resource.azurerm_virtual_network.sandbox-vnet.tfresource.aws_instance.web-server.tfdata.aws_ami.ubuntu.tfdata.azurerm_resource_group.production-rg.tf
Providers#
provider.{provider_name}.tfExamples:
provider.aws.tfprovider.azurerm.tfprovider.google.tf
With Aliases:
provider.{provider_name}.{alias}.tfExamples:
provider.aws.us-east-1.tfprovider.azurerm.production.tf
Variables and Outputs#
{block_type}.{name}.tfExamples:
variable.environment.tfvariable.instance_type.tfoutput.public_ip.tfoutput.database_url.tf
Modules#
module.{module_name}.tfExamples:
module.vpc.tfmodule.database.tfmodule.load_balancer.tf
Special Cases#
Locals Block:
locals.tfTerraform Block:
terraform.tfBenefits#
1. Immediate Understanding#
When you see a file named resource.aws_lb.web-alb.tf, you instantly know:
- It’s a resource block
- It defines an AWS load balancer
- The resource is named “web-alb”
2. Easier Navigation#
No more guessing what’s inside each file. The file name tells you everything you need to know.
3. Reduced Merge Conflicts#
With one block per file, multiple developers can work on different resources without conflicts.
4. Better Code Reviews#
Reviewers can quickly identify which infrastructure components are being modified.
5. Automated Tooling#
The tfren tool automatically renames files according to this convention.
Implementation with tfren#
The tfren tool automates the application of OTN:
Installation#
Linux & macOS:
brew tap obay/tap
brew install tfrenWindows:
scoop bucket add obay https://github.com/obay/scoop-bucket.git
scoop install obay/tfrenUsage#
Simply run tfren in your Terraform directory:
cd your-terraform-project
tfrenThe tool will:
- Parse each
.tffile - Identify the first block in each file
- Generate the appropriate filename according to OTN
- Rename files that don’t follow the convention
Example Project Structure#
Before OTN:
project/
├── main.tf
├── variables.tf
├── outputs.tf
├── providers.tf
└── networking.tfAfter OTN:
project/
├── resource.aws_vpc.main.tf
├── resource.aws_subnet.public.tf
├── resource.aws_subnet.private.tf
├── resource.aws_internet_gateway.main.tf
├── resource.aws_route_table.public.tf
├── variable.environment.tf
├── variable.instance_type.tf
├── output.vpc_id.tf
├── output.public_subnet_ids.tf
├── provider.aws.tf
└── terraform.tfBest Practices#
1. Start with OTN from Day One#
Apply the naming convention when creating new Terraform projects to avoid future refactoring.
2. Use Descriptive Resource Names#
Choose resource names that clearly indicate their purpose:
web-serverinstead ofserverproduction-databaseinstead ofdbpublic-subnet-1ainstead ofsubnet1
3. Consistent Naming Across Environments#
Use the same resource names across environments, with environment-specific values in variables:
resource.aws_instance.web-server.tf(same in dev, staging, prod)variable.environment.tf(contains “dev”, “staging”, or “prod”)
4. Group Related Resources#
While each resource is in its own file, group related resources in the same directory:
networking/
├── resource.aws_vpc.main.tf
├── resource.aws_subnet.public.tf
└── resource.aws_subnet.private.tf
compute/
├── resource.aws_instance.web-server.tf
├── resource.aws_instance.app-server.tf
└── resource.aws_autoscaling_group.web-asg.tfMigration Guide#
If you have existing Terraform projects, you can migrate to OTN:
Install tfren:
brew tap obay/tap brew install tfrenBackup your project:
cp -r your-project your-project-backupRun tfren:
cd your-project tfrenReview changes: The tool will show you which files were renamed and why.
Update CI/CD pipelines: Update any scripts that reference specific file names.
Limitations and Considerations#
1. Comments at File Start#
The tfren tool expects the first line to contain the resource definition. Comments at the start of files may not be handled correctly.
2. Complex Blocks#
For very complex resource definitions with many nested blocks, consider if the resource should be split into multiple resources.
3. Tool Compatibility#
Ensure your CI/CD tools and Terraform workflows are compatible with the new file structure.
Conclusion#
Obay’s Terraform Naming Convention provides a clear, consistent, and maintainable approach to organizing Terraform code. By following this convention and using the tfren tool, teams can improve their infrastructure-as-code practices and reduce the cognitive load of managing complex Terraform projects.
The convention is designed to be simple yet powerful, making it easy to adopt and maintain across teams of all sizes.
Resources:
