github.com/hugorut/terraform@v1.1.3/website/docs/language/providers/configuration.mdx (about)

     1  ---
     2  page_title: Provider Configuration - Configuration Language
     3  description: >-
     4    Learn how to set up providers, including how to use the alias meta-argument to
     5    specify multiple configurations for a single provider.
     6  ---
     7  
     8  # Provider Configuration
     9  
    10  Providers allow Terraform to interact with cloud providers, SaaS providers, and
    11  other APIs.
    12  
    13  Some providers require you to configure them with endpoint URLs, cloud regions,
    14  or other settings before Terraform can use them. This page documents how to
    15  configure settings for providers.
    16  
    17  Additionally, all Terraform configurations must declare which providers they
    18  require so that Terraform can install and use them. The
    19  [Provider Requirements](/language/providers/requirements)
    20  page documents how to declare providers so Terraform can install them.
    21  
    22  ## Provider Configuration
    23  
    24  Provider configurations belong in the root module of a Terraform configuration.
    25  (Child modules receive their provider configurations from the root module; for
    26  more information, see
    27  [The Module `providers` Meta-Argument](/language/meta-arguments/module-providers)
    28  and [Module Development: Providers Within Modules](/language/modules/develop/providers).)
    29  
    30  A provider configuration is created using a `provider` block:
    31  
    32  ```hcl
    33  provider "google" {
    34    project = "acme-app"
    35    region  = "us-central1"
    36  }
    37  ```
    38  
    39  The name given in the block header (`"google"` in this example) is the
    40  [local name](/language/providers/requirements#local-names) of the provider to
    41  configure. This provider should already be included in a `required_providers`
    42  block.
    43  
    44  The body of the block (between `{` and `}`) contains configuration arguments for
    45  the provider. Most arguments in this section are defined by the provider itself;
    46  in this example both `project` and `region` are specific to the `google`
    47  provider.
    48  
    49  You can use [expressions](/language/expressions) in the values of these
    50  configuration arguments, but can only reference values that are known before the
    51  configuration is applied. This means you can safely reference input variables,
    52  but not attributes exported by resources (with an exception for resource
    53  arguments that are specified directly in the configuration).
    54  
    55  A provider's documentation should list which configuration arguments it expects.
    56  For providers distributed on the
    57  [Terraform Registry](https://registry.terraform.io), versioned documentation is
    58  available on each provider's page, via the "Documentation" link in the
    59  provider's header.
    60  
    61  Some providers can use shell environment variables (or other alternate sources,
    62  like VM instance profiles) as values for some of their arguments; when
    63  available, we recommend using this as a way to keep credentials out of your
    64  version-controlled Terraform code.
    65  
    66  There are also two "meta-arguments" that are defined by Terraform itself
    67  and available for all `provider` blocks:
    68  
    69  - [`alias`, for using the same provider with different configurations for different resources][inpage-alias]
    70  - [`version`, which we no longer recommend][inpage-versions] (use
    71    [provider requirements](/language/providers/requirements) instead)
    72  
    73  Unlike many other objects in the Terraform language, a `provider` block may
    74  be omitted if its contents would otherwise be empty. Terraform assumes an
    75  empty default configuration for any provider that is not explicitly configured.
    76  
    77  ## `alias`: Multiple Provider Configurations
    78  
    79  [inpage-alias]: #alias-multiple-provider-configurations
    80  
    81  You can optionally define multiple configurations for the same provider, and
    82  select which one to use on a per-resource or per-module basis. The primary
    83  reason for this is to support multiple regions for a cloud platform; other
    84  examples include targeting multiple Docker hosts, multiple Consul hosts, etc.
    85  
    86  To create multiple configurations for a given provider, include multiple
    87  `provider` blocks with the same provider name. For each additional non-default
    88  configuration, use the `alias` meta-argument to provide an extra name segment.
    89  For example:
    90  
    91  ```hcl
    92  # The default provider configuration; resources that begin with `aws_` will use
    93  # it as the default, and it can be referenced as `aws`.
    94  provider "aws" {
    95    region = "us-east-1"
    96  }
    97  
    98  # Additional provider configuration for west coast region; resources can
    99  # reference this as `aws.west`.
   100  provider "aws" {
   101    alias  = "west"
   102    region = "us-west-2"
   103  }
   104  ```
   105  
   106  To declare a configuration alias within a module in order to receive an
   107  alternate provider configuration from the parent module, add the
   108  `configuration_aliases` argument to that provider's `required_providers`
   109  entry. The following example declares both the `mycloud` and
   110  `mycloud.alternate` provider configuration names within the containing module:
   111  
   112  ```hcl
   113  terraform {
   114    required_providers {
   115      mycloud = {
   116        source  = "mycorp/mycloud"
   117        version = "~> 1.0"
   118        configuration_aliases = [ mycloud.alternate ]
   119      }
   120    }
   121  }
   122  ```
   123  
   124  ### Default Provider Configurations
   125  
   126  A `provider` block without an `alias` argument is the _default_ configuration
   127  for that provider. Resources that don't set the `provider` meta-argument will
   128  use the default provider configuration that matches the first word of the
   129  resource type name. (For example, an `aws_instance` resource uses the default
   130  `aws` provider configuration unless otherwise stated.)
   131  
   132  If every explicit configuration of a provider has an alias, Terraform uses the
   133  implied empty configuration as that provider's default configuration. (If the
   134  provider has any required configuration arguments, Terraform will raise an error
   135  when resources default to the empty configuration.)
   136  
   137  ### Referring to Alternate Provider Configurations
   138  
   139  When Terraform needs the name of a provider configuration, it expects a
   140  reference of the form `<PROVIDER NAME>.<ALIAS>`. In the example above,
   141  `aws.west` would refer to the provider with the `us-west-2` region.
   142  
   143  These references are special expressions. Like references to other named
   144  entities (for example, `var.image_id`), they aren't strings and don't need to be
   145  quoted. But they are only valid in specific meta-arguments of `resource`,
   146  `data`, and `module` blocks, and can't be used in arbitrary expressions.
   147  
   148  ### Selecting Alternate Provider Configurations
   149  
   150  By default, resources use a default provider configuration (one without an
   151  `alias` argument) inferred from the first word of the resource type name.
   152  
   153  To use an alternate provider configuration for a resource or data source, set
   154  its `provider` meta-argument to a `<PROVIDER NAME>.<ALIAS>` reference:
   155  
   156  ```hcl
   157  resource "aws_instance" "foo" {
   158    provider = aws.west
   159  
   160    # ...
   161  }
   162  ```
   163  
   164  To select alternate provider configurations for a child module, use its
   165  `providers` meta-argument to specify which provider configurations should be
   166  mapped to which local provider names inside the module:
   167  
   168  ```hcl
   169  module "aws_vpc" {
   170    source = "./aws_vpc"
   171    providers = {
   172      aws = aws.west
   173    }
   174  }
   175  ```
   176  
   177  Modules have some special requirements when passing in providers; see
   178  [The Module `providers` Meta-Argument](/language/meta-arguments/module-providers)
   179  for more details. In most cases, only _root modules_ should define provider
   180  configurations, with all child modules obtaining their provider configurations
   181  from their parents.
   182  
   183  <a id="provider-versions"></a>
   184  
   185  ## `version` (Deprecated)
   186  
   187  [inpage-versions]: #provider-versions
   188  
   189  The `version` meta-argument specifies a version constraint for a provider, and
   190  works the same way as the `version` argument in a
   191  [`required_providers` block](/language/providers/requirements). The version
   192  constraint in a provider configuration is only used if `required_providers`
   193  does not include one for that provider.
   194  
   195  **The `version` argument in provider configurations is deprecated.**
   196  In Terraform 0.13 and later, always declare provider version constraints in
   197  [the `required_providers` block](/language/providers/requirements). The `version`
   198  argument will be removed in a future version of Terraform.