DEV Community

ShoppingJaws
ShoppingJaws

Posted on

My favorite terraform coding Rule

The naming convention for our Terraform environment was one of our favorites because it clearly indicated the declaration location.
So, I built my favorite terraform coding rule via tflint.

here

The rule is very simple.
Every resource must defined in the its resource-name file.

  • variable,locals,output,provider,module are all defined in variable.tf,locals.tf,output.tf,provider.tf,module.tf
  • data are all defined in data_<data_type>.tf
  • resource are all defined in <resource_type>.tf

Installation

.tflint.hcl

plugin "file-name-is-resource-name" {
enabled = true
source = "github.com/shoppingjaws/tflint-ruleset-file-name-is-resource-name"
version = "1.0.0"
}
Enter fullscreen mode Exit fullscreen mode

Settings

File name rules that should be defined can be overwritten

rule "file_name_is_resource_name" {
enabled = true
module_file_name_pattern = "^main.tf$"
variable_file_name_pattern= "^variable.tf$"
locals_file_name_pattern= "^locals.tf$"
provider_file_name_pattern= "^provider.tf$"
output_file_name_pattern= "^output.tf$"
module_file_name_pattern= "^module.tf$"
data_file_name_pattern= "^data_.*.tf$"
}
Enter fullscreen mode Exit fullscreen mode

Pros and cons of this rule

Pros

  • Easy to search Using the function to search by file name in vscode, etc., makes it easy to narrow down by resource name
  • Easy to review The creator's intentions and habits are not included, and the declaration is uniquely determined, reducing fluctuations

Cons

  • If tfstate is not divided, a large number of resources will be declared in one file

I'd be happy to receive feedback

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay