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

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