github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration-0-11/modules.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Modules - 0.11 Configuration Language"
     4  sidebar_current: "docs-conf-old-modules"
     5  description: |-
     6    Modules are used in Terraform to modularize and encapsulate groups of resources in your infrastructure. For more information on modules, see the dedicated modules section.
     7  ---
     8  
     9  # Module
    10  
    11  -> **Note:** This page is about Terraform 0.11 and earlier. For Terraform 0.12
    12  and later, see
    13  [Configuration Language: Modules](../configuration/modules.html).
    14  
    15  A _module_ is a container for multiple resources that are used together.
    16  
    17  Every Terraform configuration has at least one module, known as its
    18  _root module_, which consists of the resources defined in the `.tf` files in
    19  the main working directory.
    20  
    21  A module can call other modules, which lets you include the child module's
    22  resources into the configuration in a concise way. Modules
    23  can also be called multiple times, either within the same configuration or
    24  in separate configurations, allowing resource configurations to be packaged
    25  and re-used.
    26  
    27  This page describes how to call one module from another. Other pages in this
    28  section of the documentation describe the different elements that make up
    29  modules, and there is further information about how modules can be used,
    30  created, and published in [the dedicated _Modules_ section](/docs/modules/index.html).
    31  
    32  ## Calling a Child Module
    33  
    34  To _call_ a module means to include the contents of that module into the
    35  configuration with specific values for its
    36  [input variables](./variables.html). Modules are called
    37  from within other modules using `module` blocks:
    38  
    39  ```hcl
    40  module "servers" {
    41    source = "./app-cluster"
    42  
    43    servers = 5
    44  }
    45  ```
    46  
    47  A module that includes a `module` block like this is the _calling module_ of the
    48  child module.
    49  
    50  The label immediately after the `module` keyword is a local name, which the
    51  calling module can use to refer to this instance of the module.
    52  
    53  Within the block body (between `{` and `}`) are the arguments for the module.
    54  Most of the arguments correspond to [input variables](./variables.html)
    55  defined by the module, including the `servers` argument in the above example.
    56  Terraform also defines a few meta-arguments that are reserved by Terraform
    57  and used for its own purposes; we will discuss those throughout the rest of
    58  this section.
    59  
    60  All modules require a `source` argument, which is a meta-argument defined by
    61  Terraform CLI. Its value is either the path to a local directory of the
    62  module's configuration files, or a remote module source that Terraform should
    63  download and use. This value must be a literal string with no template
    64  sequences; interpolations are not allowed. For more information on
    65  possible values for this argument, see [Module Sources](/docs/modules/sources.html).
    66  
    67  The same source address can be specified in multiple `module` blocks to create
    68  multiple copies of the resources defined within, possibly with different
    69  variable values.
    70  
    71  After adding, removing, or modifying `module` blocks, you must re-run
    72  `terraform init` to allow Terraform the opportunity to adjust the installed
    73  modules. By default this command will not upgrade an already-installed module;
    74  use the `-upgrade` option to instead upgrade to the newest available version.
    75  
    76  ## Accessing Module Output Values
    77  
    78  The resources defined in a module are encapsulated, so the calling module
    79  cannot access their attributes directly. However, the child module can
    80  declare [output values](./outputs.html) to selectively
    81  export certain values to be accessed by the calling module.
    82  
    83  For example, if the `./app-cluster` module referenced in the example above
    84  exported an output value named `instance_ids` then the calling module
    85  can reference that result using the expression `module.servers.instance_ids`:
    86  
    87  ```hcl
    88  resource "aws_elb" "example" {
    89    # ...
    90  
    91    instances = module.servers.instance_ids
    92  }
    93  ```
    94  
    95  For more information about referring to named values, see
    96  [Interpolation](./interpolation.html).
    97  
    98  ## Module Versions
    99  
   100  We recommend explicitly constraining the acceptable version numbers for
   101  each external module to avoid unexpected or unwanted changes.
   102  
   103  Use the `version` attribute in the `module` block to specify versions:
   104  
   105  ```shell
   106  module "consul" {
   107    source  = "hashicorp/consul/aws"
   108    version = "0.0.5"
   109  
   110    servers = 3
   111  }
   112  ```
   113  
   114  The `version` attribute value may either be a single explicit version or
   115  a version constraint expression. Constraint expressions use the following
   116  syntax to specify a _range_ of versions that are acceptable:
   117  
   118  * `>= 1.2.0`: version 1.2.0 or newer
   119  * `<= 1.2.0`: version 1.2.0 or older
   120  * `~> 1.2.0`: any non-beta version `>= 1.2.0` and `< 1.3.0`, e.g. `1.2.X`
   121  * `~> 1.2`: any non-beta version `>= 1.2.0` and `< 2.0.0`, e.g. `1.X.Y`
   122  * `>= 1.0.0, <= 2.0.0`: any version between 1.0.0 and 2.0.0 inclusive
   123  
   124  When depending on third-party modules, references to specific versions are
   125  recommended since this ensures that updates only happen when convenient to you.
   126  
   127  For modules maintained within your organization, a version range strategy
   128  may be appropriate if a semantic versioning methodology is used consistently
   129  or if there is a well-defined release process that avoids unwanted updates.
   130  
   131  Version constraints are supported only for modules installed from a module
   132  registry, such as the [Terraform Registry](https://registry.terraform.io/) or
   133  [Terraform Cloud's private module registry](/docs/cloud/registry/index.html).
   134  Other module sources can provide their own versioning mechanisms within the
   135  source string itself, or might not support versions at all. In particular,
   136  modules sourced from local file paths do not support `version`; since
   137  they're loaded from the same source repository, they always share the same
   138  version as their caller.
   139  
   140  ## Other Meta-arguments
   141  
   142  Along with the `source` meta-argument described above, module blocks have
   143  some more meta-arguments that have special meaning across all modules,
   144  described in more detail in other sections:
   145  
   146  * `version` - (Optional) A [version constraint](#module-versions)
   147    string that specifies which versions of the referenced module are acceptable.
   148    The newest version matching the constraint will be used. `version` is supported
   149    only for modules retrieved from module registries.
   150  
   151  * `providers` - (Optional) A map whose keys are provider configuration names
   152    that are expected by child module and whose values are corresponding
   153    provider names in the calling module. This allows
   154    [provider configurations to be passed explicitly to child modules](#passing-providers-explicitly).
   155    If not specified, the child module inherits all of the default (un-aliased)
   156    provider configurations from the calling module.
   157  
   158  In addition to the above, the argument names `count`, `for_each` and
   159  `lifecycle` are not currently used by Terraform but are reserved for planned
   160  future features.
   161  
   162  Since modules are a complex feature in their own right, further detail
   163  about how modules can be used, created, and published is included in
   164  [the dedicated section on modules](/docs/modules/index.html).
   165  
   166  ## Providers within Modules
   167  
   168  In a configuration with multiple modules, there are some special considerations
   169  for how resources are associated with provider configurations.
   170  
   171  While in principle `provider` blocks can appear in any module, it is recommended
   172  that they be placed only in the _root_ module of a configuration, since this
   173  approach allows users to configure providers just once and re-use them across
   174  all descendent modules.
   175  
   176  Each resource in the configuration must be associated with one provider
   177  configuration, which may either be within the same module as the resource
   178  or be passed from the parent module. Providers can be passed down to descendent
   179  modules in two ways: either _implicitly_ through inheritance, or _explicitly_
   180  via the `providers` argument within a `module` block. These two options are
   181  discussed in more detail in the following sections.
   182  
   183  In all cases it is recommended to keep explicit provider configurations only in
   184  the root module and pass them (whether implicitly or explicitly) down to
   185  descendent modules. This avoids the provider configurations from being "lost"
   186  when descendent modules are removed from the configuration. It also allows
   187  the user of a configuration to determine which providers require credentials
   188  by inspecting only the root module.
   189  
   190  Provider configurations are used for all operations on associated resources,
   191  including destroying remote objects and refreshing state. Terraform retains, as
   192  part of its state, a reference to the provider configuration that was most
   193  recently used to apply changes to each resource. When a `resource` block is
   194  removed from the configuration, this record in the state is used to locate the
   195  appropriate configuration because the resource's `provider` argument (if any)
   196  is no longer present in the configuration.
   197  
   198  As a consequence, it is required that all resources created for a particular
   199  provider configuration must be destroyed before that provider configuration is
   200  removed, unless the related resources are re-configured to use a different
   201  provider configuration first.
   202  
   203  ### Implicit Provider Inheritance
   204  
   205  For convenience in simple configurations, a child module automatically inherits
   206  default (un-aliased) provider configurations from its parent. This means that
   207  explicit `provider` blocks appear only in the root module, and downstream
   208  modules can simply declare resources for that provider and have them
   209  automatically associated with the root provider configurations.
   210  
   211  For example, the root module might contain only a `provider` block and a
   212  `module` block to instantiate a child module:
   213  
   214  ```hcl
   215  provider "aws" {
   216    region = "us-west-1"
   217  }
   218  
   219  module "child" {
   220    source = "./child"
   221  }
   222  ```
   223  
   224  The child module can then use any resource from this provider with no further
   225  provider configuration required:
   226  
   227  ```hcl
   228  resource "aws_s3_bucket" "example" {
   229    bucket = "provider-inherit-example"
   230  }
   231  ```
   232  
   233  This approach is recommended in the common case where only a single
   234  configuration is needed for each provider across the entire configuration.
   235  
   236  In more complex situations there may be [multiple provider instances](/docs/configuration/providers.html#multiple-provider-instances),
   237  or a child module may need to use different provider settings than
   238  its parent. For such situations, it's necessary to pass providers explicitly
   239  as we will see in the next section.
   240  
   241  ## Passing Providers Explicitly
   242  
   243  When child modules each need a different configuration of a particular
   244  provider, or where the child module requires a different provider configuration
   245  than its parent, the `providers` argument within a `module` block can be
   246  used to define explicitly which provider configs are made available to the
   247  child module. For example:
   248  
   249  ```hcl
   250  # The default "aws" configuration is used for AWS resources in the root
   251  # module where no explicit provider instance is selected.
   252  provider "aws" {
   253    region = "us-west-1"
   254  }
   255  
   256  # A non-default, or "aliased" configuration is also defined for a different
   257  # region.
   258  provider "aws" {
   259    alias  = "usw2"
   260    region = "us-west-2"
   261  }
   262  
   263  # An example child module is instantiated with the _aliased_ configuration,
   264  # so any AWS resources it defines will use the us-west-2 region.
   265  module "example" {
   266    source    = "./example"
   267    providers = {
   268      aws = "aws.usw2"
   269    }
   270  }
   271  ```
   272  
   273  The `providers` argument within a `module` block is similar to
   274  the `provider` argument within a resource as described for
   275  [multiple provider instances](/docs/configuration/providers.html#multiple-provider-instances),
   276  but is a map rather than a single string because a module may contain resources
   277  from many different providers.
   278  
   279  Once the `providers` argument is used in a `module` block, it overrides all of
   280  the default inheritance behavior, so it is necessary to enumerate mappings
   281  for _all_ of the required providers. This is to avoid confusion and surprises
   282  that may result when mixing both implicit and explicit provider passing.
   283  
   284  Additional provider configurations (those with the `alias` argument set) are
   285  _never_ inherited automatically by child modules, and so must always be passed
   286  explicitly using the `providers` map. For example, a module
   287  that configures connectivity between networks in two AWS regions is likely
   288  to need both a source and a destination region. In that case, the root module
   289  may look something like this:
   290  
   291  ```hcl
   292  provider "aws" {
   293    alias  = "usw1"
   294    region = "us-west-1"
   295  }
   296  
   297  provider "aws" {
   298    alias  = "usw2"
   299    region = "us-west-2"
   300  }
   301  
   302  module "tunnel" {
   303    source    = "./tunnel"
   304    providers = {
   305      aws.src = "aws.usw1"
   306      aws.dst = "aws.usw2"
   307    }
   308  }
   309  ```
   310  
   311  In the `providers` map, the keys are provider names as expected by the child
   312  module, while the values are the names of corresponding configurations in
   313  the _current_ module. The subdirectory `./tunnel` must then contain
   314  _proxy configuration blocks_ like the following, to declare that it
   315  requires configurations to be passed with these from the `providers` block in
   316  the parent's `module` block:
   317  
   318  ```hcl
   319  provider "aws" {
   320    alias = "src"
   321  }
   322  
   323  provider "aws" {
   324    alias = "dst"
   325  }
   326  ```
   327  
   328  Each resource should then have its own `provider` attribute set to either
   329  `"aws.src"` or `"aws.dst"` to choose which of the two provider instances to use.
   330  
   331  At this time it is required to write an explicit proxy configuration block
   332  even for default (un-aliased) provider configurations when they will be passed
   333  via an explicit `providers` block:
   334  
   335  ```hcl
   336  provider "aws" {
   337  }
   338  ```
   339  
   340  If such a block is not present, the child module will behave as if it has no
   341  configurations of this type at all, which may cause input prompts to supply
   342  any required provider configuration arguments. This limitation will be
   343  addressed in a future version of Terraform.
   344  
   345  ## Multiple Instances of a Module
   346  
   347  A particular module source can be instantiated multiple times:
   348  
   349  ```hcl
   350  # my_buckets.tf
   351  
   352  module "assets_bucket" {
   353    source = "./publish_bucket"
   354    name   = "assets"
   355  }
   356  
   357  module "media_bucket" {
   358    source = "./publish_bucket"
   359    name   = "media"
   360  }
   361  ```
   362  
   363  ```hcl
   364  # publish_bucket/bucket-and-cloudfront.tf
   365  
   366  variable "name" {} # this is the input parameter of the module
   367  
   368  resource "aws_s3_bucket" "example" {
   369    # ...
   370  }
   371  
   372  resource "aws_iam_user" "deploy_user" {
   373    # ...
   374  }
   375  ```
   376  
   377  This example defines a local child module in the `./publish_bucket`
   378  subdirectory. That module has configuration to create an S3 bucket. The module
   379  wraps the bucket and all the other implementation details required to configure
   380  a bucket.
   381  
   382  We can then instantiate the module multiple times in our configuration by
   383  giving each instance a unique name -- here `module "assets_bucket"` and
   384  `module "media_bucket"` -- whilst specifying the same `source` value.
   385  
   386  Resources from child modules are prefixed with `module.<module-instance-name>`
   387  when displayed in plan output and elsewhere in the UI. For example, the
   388  `./publish_bucket` module contains `aws_s3_bucket.example`, and so the two
   389  instances of this module produce S3 bucket resources with [_resource addresses_](/docs/internals/resource-addressing.html)
   390  `module.assets_bucket.aws_s3_bucket.example` and `module.media_bucket.aws_s3_bucket.example`
   391  respectively. These full addresses are used within the UI and on the command
   392  line, but are not valid within interpolation expressions due to the
   393  encapsulation behavior described above.
   394  
   395  When refactoring an existing configuration to introduce modules, moving
   396  resource blocks between modules causes Terraform to see the new location
   397  as an entirely separate resource to the old. Always check the execution plan
   398  after performing such actions to ensure that no resources are surprisingly
   399  deleted.
   400  
   401  Each instance of a module may optionally have different providers passed to it
   402  using the `providers` argument described above. This can be useful in situations
   403  where, for example, a duplicated set of resources must be created across
   404  several regions or datacenters.
   405  
   406  ## Tainting resources within a module
   407  
   408  The [taint command](/docs/commands/taint.html) can be used to _taint_ specific
   409  resources within a module:
   410  
   411  ```shell
   412  $ terraform taint -module=salt_master aws_instance.salt_master
   413  ```
   414  
   415  It is not possible to taint an entire module. Instead, each resource within
   416  the module must be tainted separately.