|
| 1 | +--- |
| 2 | +page_title: providers meta-argument reference |
| 3 | +description: Learn how the `providers` meta-argument works in Terraform configuration. |
| 4 | +--- |
| 5 | + |
| 6 | +# `providers` reference |
| 7 | + |
| 8 | +By default, child modules inherit the default provider configurations of their parent module. You can specify an alternate provider configuration in the `module` block using the `providers` argument. The `providers` argument instructs Terraform to use the reference provider configuration to create the module resources. |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +In a [`module` block](/terraform/language/module/syntax), you can add the optional `providers` meta-argument to specify which [provider configurations](/terraform/language/modules/configuration) from the parent module is available inside the child module. |
| 13 | + |
| 14 | +The value of `providers` is a map, where the keys are the provider configuration names used inside the child module and the values are provider configuration names from the parent module. |
| 15 | + |
| 16 | +Keys and values are unquoted references to provider configurations. |
| 17 | +For default configurations, the reference is the local name of the provider. For |
| 18 | +alternate configurations, the reference uses a `<PROVIDER>.<ALIAS>` format. |
| 19 | + |
| 20 | +Within a child module, either Terraform chooses a default based on the name of the resource |
| 21 | +type, or the resource specifies an alternate configuration with the `provider` |
| 22 | +argument. If the module receives a `providers` map when it's called, the |
| 23 | +provider configuration names used within the module are effectively remapped to |
| 24 | +refer the specified configurations from the parent module. |
| 25 | + |
| 26 | +In the following example, Terraform uses the default `aws` configuration for AWS resources in the root module where no explicit provider instance is selected, but the example child moduleuses the alternate configuration with the alias `usw2`. As a result, any AWS resources the module defines uses the `us-west-2` region. |
| 27 | + |
| 28 | + |
| 29 | +```hcl |
| 30 | +provider "aws" { |
| 31 | + region = "us-west-1" |
| 32 | +} |
| 33 | +
|
| 34 | +provider "aws" { |
| 35 | + alias = "usw2" |
| 36 | + region = "us-west-2" |
| 37 | +} |
| 38 | +
|
| 39 | +module "example" { |
| 40 | + source = "./example" |
| 41 | + providers = { |
| 42 | + aws = aws.usw2 |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Default behavior |
| 48 | + |
| 49 | +The `providers` argument is optional when the child module does not declare a [`configuration_aliases`](/terraform/language/modules/develop/providers#provider-aliases-within-modules). When you omit the argument, a child module inherits all of the default provider configurations from its parent module. Default provider configurations are configurations that don't have an `alias` argument. |
| 50 | + |
| 51 | +Specifying a `providers` argument cancels the default behavior. As a result, the child module only has access to the provider configurations you specify. |
| 52 | + |
| 53 | + |
| 54 | +### When to specify `providers` |
| 55 | + |
| 56 | +Add the `providers` argument when one of the following conditions apply: |
| 57 | + |
| 58 | +- Using different default provider configurations for a child module. |
| 59 | +- Configuring a module that requires multiple configurations of the same provider. |
| 60 | + |
| 61 | +### Change default provider configurations |
| 62 | + |
| 63 | +Most re-usable modules only use default provider configurations, which they can |
| 64 | +automatically inherit from their caller when `providers` is omitted. |
| 65 | + |
| 66 | +However, in Terraform configurations that use multiple configurations of the same provider, you might want some child modules to use the default provider configuration and others to use an alternate. |
| 67 | +A common use case is using one configuration to manage resources in multiple different regions of the same cloud provider. |
| 68 | + |
| 69 | +Using `providers` argument lets you accommodate this without needing to edit the child module. Although the code |
| 70 | +within the child module always refers to the default provider configuration, the |
| 71 | +actual configuration of that default can be different for each instance. |
| 72 | + |
| 73 | + |
| 74 | +## Information for module developers |
| 75 | + |
| 76 | +For more details and guidance about working with providers inside a re-usable |
| 77 | +child module, refer to [Providers Within Modules](/terraform/language/modules/develop/providers). |
| 78 | + |
| 79 | +## Supported constructs |
| 80 | + |
| 81 | +You can use `providers` in the following Terraform configuration blocks: |
| 82 | + |
| 83 | +- [`module` blocks](/terraform/language/block/module) |
| 84 | + |
| 85 | +### Example use cases |
| 86 | + |
| 87 | +The following use cases describe common patterns for the `providers` argument. |
| 88 | + |
| 89 | +### Apply different provider configurations for module resources |
| 90 | + |
| 91 | +In the following example, the `tunnel` module includes resources that support their own provider configuration. As a result, each resource alias maps to a different provider configurations declared in the root module: |
| 92 | + |
| 93 | +```hcl |
| 94 | +provider "aws" { |
| 95 | + alias = "usw1" |
| 96 | + region = "us-west-1" |
| 97 | +} |
| 98 | +
|
| 99 | +provider "aws" { |
| 100 | + alias = "usw2" |
| 101 | + region = "us-west-2" |
| 102 | +} |
| 103 | +
|
| 104 | +module "tunnel" { |
| 105 | + source = "./tunnel" |
| 106 | + providers = { |
| 107 | + aws.src = aws.usw1 |
| 108 | + aws.dst = aws.usw2 |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### Modules with alternate provider configurations |
| 114 | + |
| 115 | +In rare cases, a single re-usable module might require multiple configurations |
| 116 | +of the same provider. For example, a module that configures connectivity between |
| 117 | +networks in two AWS regions is likely to need both a `source` and a `destination` |
| 118 | +region. The following example shows possible root module configuration for this use case: |
| 119 | + |
| 120 | +```hcl |
| 121 | +provider "aws" { |
| 122 | + alias = "usw1" |
| 123 | + region = "us-west-1" |
| 124 | +} |
| 125 | +
|
| 126 | +provider "aws" { |
| 127 | + alias = "usw2" |
| 128 | + region = "us-west-2" |
| 129 | +} |
| 130 | +
|
| 131 | +module "tunnel" { |
| 132 | + source = "./tunnel" |
| 133 | + providers = { |
| 134 | + aws.src = aws.usw1 |
| 135 | + aws.dst = aws.usw2 |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +Non-default provider configurations are never automatically inherited, so any |
| 141 | +module that works in this manner always requires a `providers` argument. The |
| 142 | +documentation for the module should specify all of the provider configuration |
| 143 | +names it needs. |
0 commit comments