What is Terraform custom Module best practises?

Hi Community,

Actually I deploy the enterprise scale Terraform Code and I build a custom module Folder. Is there any best practises to locate the Custom module folder, at the .terraform folder or left in the root module structure? Thanks

Hi @Crallbit,

The .terraform directory inside a Terraform working directory is private to Terraform itself and you should never read or write files directly in there yourself.

Terraform doesn’t have any strong restrictions about how you lay out your directories but a common convention in the community is something like the following:

       <.tf files for root Terraform module for a particular
        component in a particular environment>
      <.tf files for a particular module>

(Some teams switch the component end environment names so that the top-level directory has the environments and the subdirectories are components. This is just a matter of personal taste and there isn’t really a universal “correct answer” for whether environment or component should be the top-level grouping.)

With this directory structure the module source addresses for the local shared modules will typically start with ../../modules/ to traverse into one of the subdirectories of modules:

module "example" {
  source = "../../modules/example"

  # ...

The above directory structure is assuming that you prefer to keep all of your root and shared Terraform modules together in a single repository and use only local paths. Some teams prefer to instead place each shared module in its own repository and make separate releases. Both approaches are valid, and the decision between these will typically depend on how the responsibilities are divided in your team; if you expect that the same people will be maintaining all of the Terraform modules together then this single-repository style tends to be the simplest approach because you can then make changes across all of the modules in a single pull request.

1 Like

Hi @Crallbit

I wanted to share some guidelines that I find useful when deciding whether to use a separate repository for a module (repository module) or whether to include a module in the same repository as the Terraform projects (local module).

I find it best to use a “repository module” if any of these hold true:

  • The module is used in multiple Terraform projects that are each managed in separate repositories
  • The module resources need to be versioned separately from the Terraform project using them
  • The module performs a function that is very separate or distinct from the function of the Terraform project that it is used in

I find it best to use a “local module” when:

  • The module presents a repeatable (via “count” or “for_each”) component of the Terraform project, but which is fundamental to the Terraform project
  • The module is small and specific to the Terraform project, and most likely not reusable for other Terraform projects

There are no hard and fast rules, as @apparentlymart mentions, and each team should choose the structure that works best for them.

I generally use a mixed approach based on the above guidelines.

1 Like