Modules
Hands-on: Try theReuse Configuration with Modules tutorials.
Modules are containers for multiple resources that are used together. A moduleconsists of a collection of.tf and/or.tf.json files kept together in adirectory.
Modules are the main way to package and reuse resource configurations withTerraform.
The Root Module
Every Terraform configuration has at least one module, known as itsroot module, which consists of the resources defined in the.tf files inthe main working directory.
Child Modules
A Terraform module (usually the root module of a configuration) cancall othermodules to include their resources into the configuration. A module that hasbeen called by another module is often referred to as achild module.
Child modules can be called multiple times within the same configuration, andmultiple configurations can use the same child module.
Published Modules
In addition to modules from the local filesystem, Terraform can load modulesfrom a public or private registry. This makes it possible to publish modules forothers to use, and to use modules that others have published.
TheTerraform Registry hosts abroad collection of publicly available Terraform modules for configuring manykinds of common infrastructure. These modules are free to use, and Terraform candownload them automatically if you specify the appropriate source and version ina module call block.
Also, members of your organization might produce modules specifically craftedfor your own infrastructure needs.HCP Terraform andTerraform Enterprise both include a privatemodule registry for sharing modules internally within your organization.
Using Modules
Module Blocks documents the syntax forcalling a child module from a parent module, including meta-arguments like
for_each.Module Sources documents what kinds of paths,addresses, and URIs can be used in the
sourceargument of a module block.The Meta-Arguments section documents special arguments that can be used withevery module, including
providers,depends_on,count,andfor_each.
Developing Modules
For information about developing reusable modules, seeModule Development.