Preparing your Terraform Module directory

With Terraform, every configuration is treated as a module. The target directory containing Terraform configuration is treated as the root module when you run Terraform commands, or use Terraform Cloud or Terraform Enterprise to remotely run Terraform.

Module structure

Any local directory referenced in the source argument of a module block is treated as a module. Below you will find a typical file structure for a Terraform module:

.
├── LICENSE
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf

None of these files are required, or have any special meaning to Terraform when it uses your module. According to Terraform documentation, you can create a module with a single .tf file, or use any other file structure you like.

Each of the files listed above serves a purpose:

  • LICENSE contains the license under which the module is distributed. The LICENSE file shows the terms under which a shared Terraform module has been made available. Terraform does not use this file.
  • README.md contains the documentation for your module, in markdown format. Although Terraform does not use it, services like Terraform Registry and GitHub will display the contents of this file.
  • main.tf contains the main set of configuration for your module. You can also create other configuration files and organize them in a way which suits your needs.
  • variables.tf contains the variable definitions for your module. When your module is used by others, the variables are configured as arguments in the module block. Any variables that are not given a default value will become required arguments. Variables with default values can also be provided as module arguments, overriding the default value.
  • outputs.tf will contain the output definitions for your module. Module outputs are made available to the configuration using the module, so they are often used to pass information about the parts of your infrastructure defined by the module to other parts of your configuration.

The following files should not be distributed as part of your module:

  • terraform.tfstate and terraform.tfstate.backup: These files contain your Terraform state, and are how Terraform keeps track of the relationship between your configuration and the infrastructure provisioned by it.
  • .terraform: This directory contains the modules and plugins used to provision your infrastructure. These files are specific to a specific instance of Terraform when provisioning infrastructure, not the configuration of the infrastructure defined in .tf files.
  • *.tfvars: Since module input variables are set via arguments to the module block in your configuration, you don't need to distribute any .tfvars files with your module, unless you are also using it as a standalone Terraform configuration.

If you are tracking changes to your module in a version control system, such as git, you will want to configure your version control system to ignore these files.

Warning: Secret information, such as passwords or access keys, will become public if those files are committed to a public version control system such as GitHub.

More information is included on the official Terraform website.