0
0
Terraformcloud~5 mins

File naming conventions (.tf files) in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform uses files with the .tf extension to define infrastructure. Naming these files clearly helps organize your infrastructure code and makes it easier to manage and understand.
When you want to separate different parts of your infrastructure into logical groups.
When you need to share your Terraform code with others and want it to be easy to read.
When you want to avoid confusion by clearly naming files based on their purpose.
When you plan to reuse parts of your infrastructure code in different projects.
When you want to keep your Terraform project organized as it grows.
Commands
This command initializes the Terraform working directory. It downloads necessary provider plugins and prepares the environment to run Terraform commands.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command checks whether the Terraform files are syntactically valid and internally consistent. It helps catch errors before applying changes.
Terminal
terraform validate
Expected OutputExpected
Success! The configuration is valid.
This command shows what changes Terraform will make to your infrastructure based on the current configuration files. It helps you review changes before applying them.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example will be created + resource "aws_s3_bucket" "example" { + bucket = "my-example-bucket" + acl = "private" } Plan: 1 to add, 0 to change, 0 to destroy.
Key Concept

If you remember nothing else, remember: clear and consistent .tf file names help keep your Terraform projects organized and easy to understand.

Common Mistakes
Naming all Terraform files with generic names like main.tf without separating concerns.
This makes it hard to find and manage specific parts of your infrastructure as the project grows.
Use descriptive file names like network.tf, compute.tf, or variables.tf to group related resources.
Using inconsistent naming styles such as mixing underscores and hyphens randomly.
Inconsistent naming can confuse team members and cause mistakes when referencing files.
Choose one style (underscores or hyphens) and use it consistently across all files.
Summary
Terraform uses .tf files to define infrastructure resources.
Naming files clearly by their purpose helps organize and manage your code.
Commands like terraform init, validate, and plan help prepare and check your configuration.