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

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