github.com/hugorut/terraform@v1.1.3/website/docs/language/modules/develop/structure.mdx (about)

     1  ---
     2  page_title: Standard Module Structure
     3  ---
     4  
     5  # Standard Module Structure
     6  
     7  The standard module structure is a file and directory layout we recommend for
     8  reusable modules distributed in separate repositories. Terraform tooling is
     9  built to understand the standard module structure and use that structure to
    10  generate documentation, index modules for the module registry, and more.
    11  
    12  The standard module structure expects the layout documented below. The list may
    13  appear long, but everything is optional except for the root module. Most modules
    14  don't need to do any extra work to follow the standard structure.
    15  
    16  * **Root module**. This is the **only required element** for the standard
    17    module structure. Terraform files must exist in the root directory of
    18    the repository. This should be the primary entrypoint for the module and is
    19    expected to be opinionated. For the
    20    [Consul module](https://registry.terraform.io/modules/hashicorp/consul)
    21    the root module sets up a complete Consul cluster. It makes a lot of assumptions
    22    however, and we expect that advanced users will use specific _nested modules_
    23    to more carefully control what they want.
    24  
    25  * **README**. The root module and any nested modules should have README
    26    files. This file should be named `README` or `README.md`. The latter will
    27    be treated as markdown. There should be a description of the module and
    28    what it should be used for. If you want to include an example for how this
    29    module can be used in combination with other resources, put it in an [examples
    30    directory like this](https://github.com/hashicorp/terraform-aws-consul/tree/master/examples).
    31    Consider including a visual diagram depicting the infrastructure resources
    32    the module may create and their relationship.
    33  
    34    The README doesn't need to document inputs or outputs of the module because
    35    tooling will automatically generate this. If you are linking to a file or
    36    embedding an image contained in the repository itself, use a commit-specific
    37    absolute URL so the link won't point to the wrong version of a resource in the
    38    future.
    39  
    40  * **LICENSE**. The license under which this module is available. If you are
    41    publishing a module publicly, many organizations will not adopt a module
    42    unless a clear license is present. We recommend always having a license
    43    file, even if it is not an open source license.
    44  
    45  * **`main.tf`, `variables.tf`, `outputs.tf`**. These are the recommended filenames for
    46    a minimal module, even if they're empty. `main.tf` should be the primary
    47    entrypoint. For a simple module, this may be where all the resources are
    48    created. For a complex module, resource creation may be split into multiple
    49    files but any nested module calls should be in the main file. `variables.tf`
    50    and `outputs.tf` should contain the declarations for variables and outputs,
    51    respectively.
    52  
    53  * **Variables and outputs should have descriptions.** All variables and
    54    outputs should have one or two sentence descriptions that explain their
    55    purpose. This is used for documentation. See the documentation for
    56    [variable configuration](/language/values/variables) and
    57    [output configuration](/language/values/outputs) for more details.
    58  
    59  * **Nested modules**. Nested modules should exist under the `modules/`
    60    subdirectory. Any nested module with a `README.md` is considered usable
    61    by an external user. If a README doesn't exist, it is considered for internal
    62    use only. These are purely advisory; Terraform will not actively deny usage
    63    of internal modules. Nested modules should be used to split complex behavior
    64    into multiple small modules that advanced users can carefully pick and
    65    choose. For example, the
    66    [Consul module](https://registry.terraform.io/modules/hashicorp/consul)
    67    has a nested module for creating the Cluster that is separate from the
    68    module to setup necessary IAM policies. This allows a user to bring in their
    69    own IAM policy choices.
    70  
    71    If the root module includes calls to nested modules, they should use relative
    72    paths like `./modules/consul-cluster` so that Terraform will consider them
    73    to be part of the same repository or package, rather than downloading them
    74    again separately.
    75  
    76    If a repository or package contains multiple nested modules, they should
    77    ideally be [composable](/language/modules/develop/composition) by the caller, rather than
    78    calling directly to each other and creating a deeply-nested tree of modules.
    79  
    80  * **Examples**. Examples of using the module should exist under the
    81    `examples/` subdirectory at the root of the repository. Each example may have
    82    a README to explain the goal and usage of the example. Examples for
    83    submodules should also be placed in the root `examples/` directory.
    84  
    85    Because examples will often be copied into other repositories for
    86    customization, any `module` blocks should have their `source` set to the
    87    address an external caller would use, not to a relative path.
    88  
    89  A minimal recommended module following the standard structure is shown below.
    90  While the root module is the only required element, we recommend the structure
    91  below as the minimum:
    92  
    93  ```sh
    94  $ tree minimal-module/
    95  .
    96  ├── README.md
    97  ├── main.tf
    98  ├── variables.tf
    99  ├── outputs.tf
   100  ```
   101  
   102  A complete example of a module following the standard structure is shown below.
   103  This example includes all optional elements and is therefore the most
   104  complex a module can become:
   105  
   106  ```sh
   107  $ tree complete-module/
   108  .
   109  ├── README.md
   110  ├── main.tf
   111  ├── variables.tf
   112  ├── outputs.tf
   113  ├── ...
   114  ├── modules/
   115  │   ├── nestedA/
   116  │   │   ├── README.md
   117  │   │   ├── variables.tf
   118  │   │   ├── main.tf
   119  │   │   ├── outputs.tf
   120  │   ├── nestedB/
   121  │   ├── .../
   122  ├── examples/
   123  │   ├── exampleA/
   124  │   │   ├── main.tf
   125  │   ├── exampleB/
   126  │   ├── .../
   127  ```