github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/language/providers/configuration.html.md (about)

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