github.com/hugorut/terraform@v1.1.3/website/docs/language/modules/develop/index.mdx (about) 1 --- 2 page_title: Creating Modules 3 description: >- 4 Modules are containers for multiple resources that are used together in a 5 configuration. Learn when to create modules and about module structure. 6 --- 7 8 # Creating Modules 9 10 > **Hands-on:** Try the [Reuse Configuration with Modules](https://learn.hashicorp.com/collections/terraform/modules?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) collection on HashiCorp Learn. 11 12 A _module_ is a container for multiple resources that are used together. 13 Modules can be used to create lightweight abstractions, so that you can 14 describe your infrastructure in terms of its architecture, rather than 15 directly in terms of physical objects. 16 17 The `.tf` files in your working directory when you run [`terraform plan`](/cli/commands/plan) 18 or [`terraform apply`](/cli/commands/apply) together form the _root_ 19 module. That module may [call other modules](/language/modules/syntax#calling-a-child-module) 20 and connect them together by passing output values from one to input values 21 of another. 22 23 To learn how to _use_ modules, see [the Modules configuration section](/language/modules). 24 This section is about _creating_ re-usable modules that other configurations 25 can include using `module` blocks. 26 27 ## Module structure 28 29 Re-usable modules are defined using all of the same 30 [configuration language](/language) concepts we use in root modules. 31 Most commonly, modules use: 32 33 * [Input variables](/language/values/variables) to accept values from 34 the calling module. 35 * [Output values](/language/values/outputs) to return results to the 36 calling module, which it can then use to populate arguments elsewhere. 37 * [Resources](/language/resources) to define one or more 38 infrastructure objects that the module will manage. 39 40 To define a module, create a new directory for it and place one or more `.tf` 41 files inside just as you would do for a root module. Terraform can load modules 42 either from local relative paths or from remote repositories; if a module will 43 be re-used by lots of configurations you may wish to place it in its own 44 version control repository. 45 46 Modules can also call other modules using a `module` block, but we recommend 47 keeping the module tree relatively flat and using [module composition](/language/modules/develop/composition) 48 as an alternative to a deeply-nested tree of modules, because this makes 49 the individual modules easier to re-use in different combinations. 50 51 ## When to write a module 52 53 In principle any combination of resources and other constructs can be factored 54 out into a module, but over-using modules can make your overall Terraform 55 configuration harder to understand and maintain, so we recommend moderation. 56 57 A good module should raise the level of abstraction by describing a new concept 58 in your architecture that is constructed from resource types offered by 59 providers. 60 61 For example, `aws_instance` and `aws_elb` are both resource types belonging to 62 the AWS provider. You might use a module to represent the higher-level concept 63 "[HashiCorp Consul](https://www.consul.io/) cluster running in AWS" which 64 happens to be constructed from these and other AWS provider resources. 65 66 We _do not_ recommend writing modules that are just thin wrappers around single 67 other resource types. If you have trouble finding a name for your module that 68 isn't the same as the main resource type inside it, that may be a sign that 69 your module is not creating any new abstraction and so the module is 70 adding unnecessary complexity. Just use the resource type directly in the 71 calling module instead. 72 73 ## Refactoring module resources 74 75 You can include [refactoring blocks](/language/modules/develop/refactoring) to record how resource 76 names and module structure have changed from previous module versions. 77 Terraform uses that information during planning to reinterpret existing objects 78 as if they had been created at the corresponding new addresses, eliminating a 79 separate workflow step to replace or migrate existing objects.