github.com/pdecat/terraform@v0.11.9-beta1/website/docs/configuration/providers.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Configuring Providers"
     4  sidebar_current: "docs-config-providers"
     5  description: |-
     6    Providers are responsible in Terraform for managing the lifecycle of a resource: create, read, update, delete.
     7  ---
     8  
     9  # Provider Configuration
    10  
    11  Providers are responsible in Terraform for managing the lifecycle
    12  of a [resource](/docs/configuration/resources.html): create,
    13  read, update, delete.
    14  
    15  Most providers require some sort of configuration to provide
    16  authentication information, endpoint URLs, etc. Where explicit configuration
    17  is required, a `provider` block is used within the configuration as
    18  illustrated in the following sections.
    19  
    20  By default, resources are matched with provider configurations by matching
    21  the start of the resource name. For example, a resource of type
    22  `vsphere_virtual_machine` is associated with a provider called `vsphere`.
    23  
    24  This page assumes you're familiar with the
    25  [configuration syntax](/docs/configuration/syntax.html)
    26  already.
    27  
    28  ## Example
    29  
    30  A provider configuration looks like the following:
    31  
    32  ```hcl
    33  provider "aws" {
    34    access_key = "foo"
    35    secret_key = "bar"
    36    region     = "us-east-1"
    37  }
    38  ```
    39  
    40  ## Description
    41  
    42  A `provider` block represents a configuration for the provider named in its
    43  header. For example, `provider "aws"` above is a configuration for the
    44  `aws` provider.
    45  
    46  Within the block body (between `{ }`) is configuration for the provider.
    47  The configuration is dependent on the type, and is documented
    48  [for each provider](/docs/providers/index.html).
    49  
    50  The arguments `alias` and `version`, if present, are special arguments
    51  handled by Terraform Core for their respective features described above. All
    52  other arguments are defined by the provider itself.
    53  
    54  A `provider` block may be omitted if its body would be empty. Using a resource
    55  in configuration implicitly creates an empty provider configuration for it
    56  unless a `provider` block is explicitly provided.
    57  
    58  ## Initialization
    59  
    60  Each time a new provider is added to configuration -- either explicitly via
    61  a `provider` block or by adding a resource from that provider -- it's necessary
    62  to initialize that provider before use. Initialization downloads and installs
    63  the provider's plugin and prepares it to be used.
    64  
    65  Provider initialization is one of the actions of `terraform init`. Running
    66  this command will download and initialize any providers that are not already
    67  initialized.
    68  
    69  Providers downloaded by `terraform init` are only installed for the current
    70  working directory; other working directories can have their own installed
    71  provider versions.
    72  
    73  Note that `terraform init` cannot automatically download providers that are not
    74  distributed by HashiCorp. See [Third-party Plugins](#third-party-plugins) below
    75  for installation instructions.
    76  
    77  For more information, see
    78  [the `terraform init` command](/docs/commands/init.html).
    79  
    80  ## Provider Versions
    81  
    82  Providers are released on a separate rhythm from Terraform itself, and thus
    83  have their own version numbers. For production use, it is recommended to
    84  constrain the acceptable provider versions via configuration, to ensure that
    85  new versions with breaking changes will not be automatically installed by
    86  `terraform init` in future.
    87  
    88  When `terraform init` is run _without_ provider version constraints, it
    89  prints a suggested version constraint string for each provider:
    90  
    91  ```
    92  The following providers do not have any version constraints in configuration,
    93  so the latest version was installed.
    94  
    95  To prevent automatic upgrades to new major versions that may contain breaking
    96  changes, it is recommended to add version = "..." constraints to the
    97  corresponding provider blocks in configuration, with the constraint strings
    98  suggested below.
    99  
   100  * provider.aws: version = "~> 1.0"
   101  ```
   102  
   103  To constrain the provider version as suggested, add a `version` argument to
   104  the provider configuration block:
   105  
   106  ```hcl
   107  provider "aws" {
   108    version = "~> 1.0"
   109  
   110    access_key = "foo"
   111    secret_key = "bar"
   112    region     = "us-east-1"
   113  }
   114  ```
   115  
   116  This special argument applies to _all_ providers.
   117  [`terraform providers`](/docs/commands/providers.html) can be used to
   118  view the specified version constraints for all providers used in the
   119  current configuration.
   120  
   121  The `version` attribute value may either be a single explicit version or
   122  a version constraint expression. Constraint expressions use the following
   123  syntax to specify a _range_ of versions that are acceptable:
   124  
   125  * `>= 1.2.0`: version 1.2.0 or newer
   126  * `<= 1.2.0`: version 1.2.0 or older
   127  * `~> 1.2.0`: any non-beta version `>= 1.2.0` and `< 1.3.0`, e.g. `1.2.X`
   128  * `~> 1.2`: any non-beta version `>= 1.2.0` and `< 2.0.0`, e.g. `1.X.Y`
   129  * `>= 1.0.0, <= 2.0.0`: any version between 1.0.0 and 2.0.0 inclusive
   130  
   131  When `terraform init` is re-run with providers already installed, it will
   132  use an already-installed provider that meets the constraints in preference
   133  to downloading a new version. To upgrade to the latest acceptable version
   134  of each provider, run `terraform init -upgrade`. This command also upgrades
   135  to the latest versions of all Terraform modules.
   136  
   137  ## Multiple Provider Instances
   138  
   139  You can define multiple configurations for the same provider in order to support
   140  multiple regions, multiple hosts, etc. The primary use case for this is
   141  using multiple cloud regions. Other use-cases include targeting multiple
   142  Docker hosts, multiple Consul hosts, etc.
   143  
   144  To include multiple configurations for a given provider, include multiple
   145  `provider` blocks with the same provider name, but set the `alias` field to an
   146  instance name to use for each additional instance. For example:
   147  
   148  ```hcl
   149  # The default provider configuration
   150  provider "aws" {
   151    # ...
   152  }
   153  
   154  # Additional provider configuration for west coast region
   155  provider "aws" {
   156    alias  = "west"
   157    region = "us-west-2"
   158  }
   159  ```
   160  
   161  A `provider` block with out `alias` set is known as the _default_ provider
   162  configuration. When `alias` is set, it creates an _additional_ provider
   163  configuration. For providers that have no required configuration arguments, the
   164  implied _empty_ configuration is also considered to be a _default_ provider
   165  configuration.
   166  
   167  Resources are normally associated with the default provider configuration
   168  inferred from the resource type name. For example, a resource of type
   169  `aws_instance` uses the _default_ (un-aliased) `aws` provider configuration
   170  unless otherwise stated.
   171  
   172  The `provider` argument within any `resource` or `data` block overrides this
   173  default behavior and allows an additional provider configuration to be
   174  selected using its alias:
   175  
   176  ```hcl
   177  resource "aws_instance" "foo" {
   178    provider = "aws.west"
   179  
   180    # ...
   181  }
   182  ```
   183  
   184  The value of the `provider` argument is always the provider name and an
   185  alias separated by a period, such as `"aws.west"` above.
   186  
   187  Provider configurations may also be passed from a parent module into a
   188  child module, as described in
   189  [_Providers within Modules_](/docs/modules/usage.html#providers-within-modules).
   190  
   191  ## Interpolation
   192  
   193  Provider configurations may use [interpolation syntax](/docs/configuration/interpolation.html)
   194  to allow dynamic configuration:
   195  
   196  ```hcl
   197  provider "aws" {
   198    region = "${var.aws_region}"
   199  }
   200  ```
   201  
   202  Interpolation is supported only for the per-provider configuration arguments.
   203  It is not supported for the special `alias` and `version` arguments.
   204  
   205  Although in principle it is possible to use any interpolation expression within
   206  a provider configuration argument, providers must be configurable to perform
   207  almost all operations within Terraform, and so it is not possible to use
   208  expressions whose value cannot be known until after configuration is applied,
   209  such as the id of a resource.
   210  
   211  It is always valid to use [input variables](/docs/configuration/variables.html)
   212  and [data sources](/docs/configuration/data-sources.html) whose configurations
   213  do not in turn depend on as-yet-unknown values. [Local values](/docs/configuration/locals.html)
   214  may also be used, but currently may cause errors when running `terraform destroy`.
   215  
   216  ## Third-party Plugins
   217  
   218  Anyone can develop and distribute their own Terraform providers. (See
   219  [Writing Custom Providers](/docs/extend/writing-custom-providers.html) for more
   220  about provider development.) These third-party providers must be manually
   221  installed, since `terraform init` cannot automatically download them.
   222  
   223  Install third-party providers by placing their plugin executables in the user
   224  plugins directory. The user plugins directory is in one of the following
   225  locations, depending on the host operating system:
   226  
   227  Operating system  | User plugins directory
   228  ------------------|-----------------------
   229  Windows           | `terraform.d\plugins` in your user's "Application Data" directory
   230  All other systems | `.terraform.d/plugins` in your user's home directory
   231  
   232  Once a plugin is installed, `terraform init` can initialize it normally.
   233  
   234  Providers distributed by HashiCorp can also go in the user plugins directory. If
   235  a manually installed version meets the configuration's version constraints,
   236  Terraform will use it instead of downloading that provider. This is useful in
   237  airgapped environments and when testing pre-release provider builds.
   238  
   239  ### Plugin Names and Versions
   240  
   241  The naming scheme for provider plugins is `terraform-provider-<NAME>_vX.Y.Z`,
   242  and Terraform uses the name to understand the name and version of a particular
   243  provider binary.
   244  
   245  If multiple versions of a plugin are installed, Terraform will use the newest
   246  version that meets the configuration's version constraints.
   247  
   248  Third-party plugins are often distributed with an appropriate filename already
   249  set in the distribution archive, so that they can be extracted directly into the
   250  user plugins directory.
   251  
   252  ### OS and Architecture Directories
   253  
   254  Terraform plugins are compiled for a specific operating system and architecture,
   255  and any plugins in the root of the user plugins directory must be compiled for
   256  the current system.
   257  
   258  If you use the same plugins directory on multiple systems, you can install
   259  plugins into subdirectories with a naming scheme of `<OS>_<ARCH>` (for example,
   260  `darwin_amd64`). Terraform uses plugins from the root of the plugins directory
   261  and from the subdirectory that corresponds to the current system, ignoring
   262  other subdirectories.
   263  
   264  Terraform's OS and architecture strings are the standard ones used by the Go
   265  language. The following are the most common:
   266  
   267  * `darwin_amd64`
   268  * `freebsd_386`
   269  * `freebsd_amd64`
   270  * `freebsd_arm`
   271  * `linux_386`
   272  * `linux_amd64`
   273  * `linux_arm`
   274  * `openbsd_386`
   275  * `openbsd_amd64`
   276  * `solaris_amd64`
   277  * `windows_386`
   278  * `windows_amd64`
   279  
   280  ## Provider Plugin Cache
   281  
   282  By default, `terraform init` downloads plugins into a subdirectory of the
   283  working directory so that each working directory is self-contained. As a
   284  consequence, if you have multiple configurations that use the same provider
   285  then a separate copy of its plugin will be downloaded for each configuration.
   286  
   287  Given that provider plugins can be quite large (on the order of hundreds of
   288  megabytes), this default behavior can be inconvenient for those with slow
   289  or metered Internet connections. Therefore Terraform optionally allows the
   290  use of a local directory as a shared plugin cache, which then allows each
   291  distinct plugin binary to be downloaded only once.
   292  
   293  To enable the plugin cache, use the `plugin_cache_dir` setting in
   294  [the CLI configuration file](https://www.terraform.io/docs/commands/cli-config.html).
   295  For example:
   296  
   297  ```hcl
   298  # (Note that the CLI configuration file is _not_ the same as the .tf files
   299  #  used to configure infrastructure.)
   300  
   301  plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
   302  ```
   303  
   304  This directory must already exist before Terraform will cache plugins;
   305  Terraform will not create the directory itself.
   306  
   307  Please note that on Windows it is necessary to use forward slash separators
   308  (`/`) rather than the conventional backslash (`\`) since the configuration
   309  file parser considers a backslash to begin an escape sequence.
   310  
   311  Setting this in the configuration file is the recommended approach for a
   312  persistent setting. Alternatively, the `TF_PLUGIN_CACHE_DIR` environment
   313  variable can be used to enable caching or to override an existing cache
   314  directory within a particular shell session:
   315  
   316  ```bash
   317  export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
   318  ```
   319  
   320  When a plugin cache directory is enabled, the `terraform init` command will
   321  still access the plugin distribution server to obtain metadata about which
   322  plugins are available, but once a suitable version has been selected it will
   323  first check to see if the selected plugin is already available in the cache
   324  directory. If so, the already-downloaded plugin binary will be used.
   325  
   326  If the selected plugin is not already in the cache, it will be downloaded
   327  into the cache first and then copied from there into the correct location
   328  under your current working directory.
   329  
   330  When possible, Terraform will use hardlinks or symlinks to avoid storing
   331  a separate copy of a cached plugin in multiple directories. At present, this
   332  is not supported on Windows and instead a copy is always created.
   333  
   334  The plugin cache directory must *not* be the third-party plugin directory
   335  or any other directory Terraform searches for pre-installed plugins, since
   336  the cache management logic conflicts with the normal plugin discovery logic
   337  when operating on the same directory.
   338  
   339  Please note that Terraform will never itself delete a plugin from the
   340  plugin cache once it's been placed there. Over time, as plugins are upgraded,
   341  the cache directory may grow to contain several unused versions which must be
   342  manually deleted.