github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/website/docs/language/providers/requirements.mdx (about)

     1  ---
     2  page_title: Provider Requirements - Configuration Language
     3  description: >-
     4    Providers are plugins that allow Terraform to interact with services, cloud
     5    providers, and other APIs. Learn how to declare providers in a configuration.
     6  ---
     7  
     8  # Provider Requirements
     9  
    10  Terraform relies on plugins called "providers" to interact with remote systems.
    11  Terraform configurations must declare which providers they require, so that
    12  Terraform can install and use them. This page documents how to declare providers
    13  so Terraform can install them.
    14  
    15  > **Hands-on:** Try the [Perform CRUD Operations with Providers](https://learn.hashicorp.com/tutorials/terraform/provider-use) tutorial on HashiCorp Learn.
    16  
    17  Additionally, some providers require configuration (like endpoint URLs or cloud
    18  regions) before they can be used. The [Provider
    19  Configuration](/language/providers/configuration) page documents how
    20  to configure settings for providers.
    21  
    22  -> **Note:** This page is about a feature of Terraform 0.13 and later; it also
    23  describes how to use the more limited version of that feature that was available
    24  in Terraform 0.12.
    25  
    26  ## Requiring Providers
    27  
    28  Each Terraform module must declare which providers it requires, so that
    29  Terraform can install and use them. Provider requirements are declared in a
    30  `required_providers` block.
    31  
    32  A provider requirement consists of a local name, a source location, and a
    33  version constraint:
    34  
    35  ```hcl
    36  terraform {
    37    required_providers {
    38      mycloud = {
    39        source  = "mycorp/mycloud"
    40        version = "~> 1.0"
    41      }
    42    }
    43  }
    44  ```
    45  
    46  The `required_providers` block must be nested inside the top-level
    47  [`terraform` block](/language/settings) (which can also contain other settings).
    48  
    49  Each argument in the `required_providers` block enables one provider. The key
    50  determines the provider's [local name](#local-names) (its unique identifier
    51  within this module), and the value is an object with the following elements:
    52  
    53  * `source` - the global [source address](#source-addresses) for the
    54    provider you intend to use, such as `hashicorp/aws`.
    55  
    56  * `version` - a [version constraint](#version-constraints) specifying
    57    which subset of available provider versions the module is compatible with.
    58  
    59  -> **Note:** The `name = { source, version }` syntax for `required_providers`
    60  was added in Terraform v0.13. Previous versions of Terraform used a version
    61  constraint string instead of an object (like `mycloud = "~> 1.0"`), and had no
    62  way to specify provider source addresses. If you want to write a module that
    63  works with both Terraform v0.12 and v0.13, see [v0.12-Compatible Provider
    64  Requirements](#v0-12-compatible-provider-requirements) below.
    65  
    66  ## Names and Addresses
    67  
    68  Each provider has two identifiers:
    69  
    70  * A unique _source address,_ which is only used when requiring a provider.
    71  * A _local name,_ which is used everywhere else in a Terraform module.
    72  
    73  -> **Note:** Prior to Terraform 0.13, providers only had local names, since
    74  Terraform could only automatically download providers distributed by HashiCorp.
    75  
    76  ### Local Names
    77  
    78  Local names are module-specific, and are assigned when requiring a provider.
    79  Local names must be unique per-module.
    80  
    81  Outside of the `required_providers` block, Terraform configurations always refer
    82  to providers by their local names. For example, the following configuration
    83  declares `mycloud` as the local name for `mycorp/mycloud`, then uses that local
    84  name when [configuring the provider](/language/providers/configuration):
    85  
    86  ```hcl
    87  terraform {
    88    required_providers {
    89      mycloud = {
    90        source  = "mycorp/mycloud"
    91        version = "~> 1.0"
    92      }
    93    }
    94  }
    95  
    96  provider "mycloud" {
    97    # ...
    98  }
    99  ```
   100  
   101  Users of a provider can choose any local name for it. However, nearly every
   102  provider has a _preferred local name,_ which it uses as a prefix for all of its
   103  resource types. (For example, resources from `hashicorp/aws` all begin with
   104  `aws`, like `aws_instance` or `aws_security_group`.)
   105  
   106  Whenever possible, you should use a provider's preferred local name. This makes
   107  your configurations easier to understand, and lets you omit the `provider`
   108  meta-argument from most of your resources. (If a resource doesn't specify which
   109  provider configuration to use, Terraform interprets the first word of the
   110  resource type as a local provider name.)
   111  
   112  ### Source Addresses
   113  
   114  A provider's source address is its global identifier. It also specifies the
   115  primary location where Terraform can download it.
   116  
   117  Source addresses consist of three parts delimited by slashes (`/`), as
   118  follows:
   119  
   120  `[<HOSTNAME>/]<NAMESPACE>/<TYPE>`
   121  
   122  * **Hostname** (optional): The hostname of the Terraform registry that
   123    distributes the provider. If omitted, this defaults to
   124    `registry.terraform.io`, the hostname of
   125    [the public Terraform Registry](https://registry.terraform.io/).
   126  
   127  * **Namespace:** An organizational namespace within the specified registry.
   128    For the public Terraform Registry and for Terraform Cloud's private registry,
   129    this represents the organization that publishes the provider. This field
   130    may have other meanings for other registry hosts.
   131  
   132  * **Type:** A short name for the platform or system the provider manages. Must
   133    be unique within a particular namespace on a particular registry host.
   134  
   135    The type is usually the provider's preferred local name. (There are
   136    exceptions; for example,
   137    [`hashicorp/google-beta`](https://registry.terraform.io/providers/hashicorp/google-beta/latest)
   138    is an alternate release channel for `hashicorp/google`, so its preferred
   139    local name is `google`. If in doubt, check the provider's documentation.)
   140  
   141  For example,
   142  [the official HTTP provider](https://registry.terraform.io/providers/hashicorp/http)
   143  belongs to the `hashicorp` namespace on `registry.terraform.io`, so its
   144  source address is `registry.terraform.io/hashicorp/http` or, more commonly, just
   145  `hashicorp/http`.
   146  
   147  The source address with all three components given explicitly is called the
   148  provider's _fully-qualified address_. You will see fully-qualified address in
   149  various outputs, like error messages, but in most cases a simplified display
   150  version is used. This display version omits the source host when it is the
   151  public registry, so you may see the shortened version `"hashicorp/random"` instead
   152  of `"registry.terraform.io/hashicorp/random"`.
   153  
   154  -> **Note:** If you omit the `source` argument when requiring a provider,
   155  Terraform uses an implied source address of
   156  `registry.terraform.io/hashicorp/<LOCAL NAME>`. This is a backward compatibility
   157  feature to support the transition to Terraform 0.13; in modules that require
   158  0.13 or later, we recommend using explicit source addresses for all providers.
   159  
   160  ### Handling Local Name Conflicts
   161  
   162  Whenever possible, we recommend using a provider's preferred local name, which
   163  is usually the same as the "type" portion of its source address.
   164  
   165  However, it's sometimes necessary to use two providers with the same preferred
   166  local name in the same module, usually when the providers are named after a
   167  generic infrastructure type. Terraform requires unique local names for each
   168  provider in a module, so you'll need to use a non-preferred name for at least
   169  one of them.
   170  
   171  When this happens, we recommend combining each provider's namespace with
   172  its type name to produce compound local names with a dash:
   173  
   174  ```hcl
   175  terraform {
   176    required_providers {
   177      # In the rare situation of using two providers that
   178      # have the same type name -- "http" in this example --
   179      # use a compound local name to distinguish them.
   180      hashicorp-http = {
   181        source  = "hashicorp/http"
   182        version = "~> 2.0"
   183      }
   184      mycorp-http = {
   185        source  = "mycorp/http"
   186        version = "~> 1.0"
   187      }
   188    }
   189  }
   190  
   191  # References to these providers elsewhere in the
   192  # module will use these compound local names.
   193  provider "mycorp-http" {
   194    # ...
   195  }
   196  
   197  data "http" "example" {
   198    provider = hashicorp-http
   199    #...
   200  }
   201  ```
   202  
   203  Terraform won't be able to guess either provider's name from its resource types,
   204  so you'll need to specify a `provider` meta-argument for every affected
   205  resource. However, readers and maintainers of your module will be able to easily
   206  understand what's happening, and avoiding confusion is much more important than
   207  avoiding typing.
   208  
   209  ## Version Constraints
   210  
   211  Each provider plugin has its own set of available versions, allowing the
   212  functionality of the provider to evolve over time. Each provider dependency you
   213  declare should have a [version constraint](/language/expressions/version-constraints) given in
   214  the `version` argument so Terraform can select a single version per provider
   215  that all modules are compatible with.
   216  
   217  The `version` argument is optional; if omitted, Terraform will accept any
   218  version of the provider as compatible. However, we strongly recommend specifying
   219  a version constraint for every provider your module depends on.
   220  
   221  To ensure Terraform always installs the same provider versions for a given
   222  configuration, you can use Terraform CLI to create a
   223  [dependency lock file](/language/files/dependency-lock)
   224  and commit it to version control along with your configuration. If a lock file
   225  is present, Terraform Cloud, CLI, and Enterprise will all obey it when
   226  installing providers.
   227  
   228  > **Hands-on:** Try the [Lock and Upgrade Provider Versions](https://learn.hashicorp.com/tutorials/terraform/provider-versioning) tutorial on HashiCorp Learn.
   229  
   230  ### Best Practices for Provider Versions
   231  
   232  Each module should at least declare the minimum provider version it is known
   233  to work with, using the `>=` version constraint syntax:
   234  
   235  ```hcl
   236  terraform {
   237    required_providers {
   238      mycloud = {
   239        source  = "hashicorp/aws"
   240        version = ">= 1.0"
   241      }
   242    }
   243  }
   244  ```
   245  
   246  A module intended to be used as the root of a configuration — that is, as the
   247  directory where you'd run `terraform apply` — should also specify the
   248  _maximum_ provider version it is intended to work with, to avoid accidental
   249  upgrades to incompatible new versions. The `~>` operator is a convenient
   250  shorthand for allowing the rightmost component of a version to increment. The
   251  following example uses the operator to allow only patch releases within a 
   252  specific minor release:
   253  
   254  ```hcl
   255  terraform {
   256    required_providers {
   257      mycloud = {
   258        source  = "hashicorp/aws"
   259        version = "~> 1.0.4"
   260      }
   261    }
   262  }
   263  ```
   264  
   265  Do not use `~>` (or other maximum-version constraints) for modules you intend to
   266  reuse across many configurations, even if you know the module isn't compatible
   267  with certain newer versions. Doing so can sometimes prevent errors, but more
   268  often it forces users of the module to update many modules simultaneously when
   269  performing routine upgrades. Specify a minimum version, document any known
   270  incompatibilities, and let the root module manage the maximum version.
   271  
   272  ## Built-in Providers
   273  
   274  While most Terraform providers are distributed separately as plugins, there
   275  is currently one provider that is built in to Terraform itself, which
   276  provides
   277  [the `terraform_remote_state` data source](/language/state/remote-state-data).
   278  
   279  Because this provider is built in to Terraform, you don't need to declare it
   280  in the `required_providers` block in order to use its features. However, for
   281  consistency it _does_ have a special provider source address, which is
   282  `terraform.io/builtin/terraform`. This address may sometimes appear in
   283  Terraform's error messages and other output in order to unambiguously refer
   284  to the built-in provider, as opposed to a hypothetical third-party provider
   285  with the type name "terraform".
   286  
   287  There is also an existing provider with the source address
   288  `hashicorp/terraform`, which is an older version of the now-built-in provider
   289  that was used by older versions of Terraform. `hashicorp/terraform` is not
   290  compatible with Terraform v0.11 or later and should never be declared in a
   291  `required_providers` block.
   292  
   293  ## In-house Providers
   294  
   295  Anyone can develop and distribute their own Terraform providers. See
   296  the [Call APIs with Terraform Providers](https://learn.hashicorp.com/collections/terraform/providers)
   297  collection on HashiCorp Learn for more
   298  about provider development.
   299  
   300  Some organizations develop their own providers to configure
   301  proprietary systems, and wish to use these providers from Terraform without
   302  publishing them on the public Terraform Registry.
   303  
   304  One option for distributing such a provider is to run an in-house _private_
   305  registry, by implementing
   306  [the provider registry protocol](/internals/provider-registry-protocol).
   307  
   308  Running an additional service just to distribute a single provider internally
   309  may be undesirable, so Terraform also supports
   310  [other provider installation methods](/cli/config/config-file#provider-installation),
   311  including placing provider plugins directly in specific directories in the
   312  local filesystem, via _filesystem mirrors_.
   313  
   314  All providers must have a [source address](#source-addresses) that includes
   315  (or implies) the hostname of a registry, but that hostname does not need to
   316  provide an actual registry service. For in-house providers that you intend to
   317  distribute from a local filesystem directory, you can use an arbitrary hostname
   318  in a domain your organization controls.
   319  
   320  For example, if your corporate domain were `example.com` then you might choose
   321  to use `terraform.example.com` as your placeholder hostname, even if that
   322  hostname doesn't actually resolve in DNS. You can then choose any namespace and
   323  type you wish to represent your in-house provider under that hostname, giving
   324  a source address like `terraform.example.com/examplecorp/ourcloud`:
   325  
   326  ```hcl
   327  terraform {
   328    required_providers {
   329      mycloud = {
   330        source  = "terraform.example.com/examplecorp/ourcloud"
   331        version = ">= 1.0"
   332      }
   333    }
   334  }
   335  ```
   336  
   337  To make version 1.0.0 of this provider available for installation from the
   338  local filesystem, choose one of the
   339  [implied local mirror directories](/cli/config/config-file#implied-local-mirror-directories)
   340  and create a directory structure under it like this:
   341  
   342  ```
   343  terraform.example.com/examplecorp/ourcloud/1.0.0
   344  ```
   345  
   346  Under that `1.0.0` directory, create one additional directory representing the
   347  platform where you are running Terraform, such as `linux_amd64` for Linux on
   348  an AMD64/x64 processor, and then place the provider plugin executable and any
   349  other needed files in that directory.
   350  
   351  Thus, on a Windows system, the provider plugin executable file might be at the
   352  following path:
   353  
   354  ```
   355  terraform.example.com/examplecorp/ourcloud/1.0.0/windows_amd64/terraform-provider-ourcloud.exe
   356  ```
   357  
   358  If you later decide to switch to using a real private provider registry rather
   359  than distribute binaries out of band, you can deploy the registry server at
   360  `terraform.example.com` and retain the same namespace and type names, in which
   361  case your existing modules will require no changes to locate the same provider
   362  using your registry server.
   363  
   364  ## v0.12-Compatible Provider Requirements
   365  
   366  Explicit provider source addresses were introduced with Terraform v0.13, so the
   367  full provider requirements syntax is not supported by Terraform v0.12.
   368  
   369  However, in order to allow writing modules that are compatible with both
   370  Terraform v0.12 and v0.13, versions of Terraform between v0.12.26 and v0.13
   371  will accept but ignore the `source` argument in a `required_providers` block.
   372  
   373  Consider the following example written for Terraform v0.13:
   374  
   375  ```hcl
   376  terraform {
   377    required_providers {
   378      aws = {
   379        source  = "hashicorp/aws"
   380        version = "~> 1.0"
   381      }
   382    }
   383  }
   384  ```
   385  
   386  Terraform v0.12.26 will accept syntax like the above but will understand it
   387  in the same way as the following v0.12-style syntax:
   388  
   389  ```hcl
   390  terraform {
   391    required_providers {
   392      aws = "~> 1.0"
   393    }
   394  }
   395  ```
   396  
   397  In other words, Terraform v0.12.26 ignores the `source` argument and considers
   398  only the `version` argument, using the given [local name](#local-names) as the
   399  un-namespaced provider type to install.
   400  
   401  When writing a module that is compatible with both Terraform v0.12.26 and
   402  Terraform v0.13.0 or later, you must follow the following additional rules so
   403  that both versions will select the same provider to install:
   404  
   405  * Use only providers that can be automatically installed by Terraform v0.12.
   406    Third-party providers, such as community providers in the Terraform Registry,
   407    cannot be selected by Terraform v0.12 because it does not support the
   408    hierarchical source address namespace.
   409  
   410  * Ensure that your chosen local name exactly matches the "type" portion of the
   411    source address given in the `source` argument, such as both being "aws" in
   412    the examples above, because Terraform v0.12 will use the local name to
   413    determine which provider plugin to download and install.
   414  
   415  * If the provider belongs to the `hashicorp` namespace, as with the
   416    `hashicorp/aws` provider shown above, omit the `source` argument and allow
   417    Terraform v0.13 to select the `hashicorp` namespace by default.
   418  
   419  * Provider type names must always be written in lowercase. Terraform v0.13
   420    treats provider source addresses as case-insensitive, but Terraform v0.12
   421    considers its legacy-style provider names to be case-sensitive. Using
   422    lowercase will ensure that the name is selectable by both Terraform major
   423    versions.
   424  
   425  This compatibility mechanism is provided as a temporary transitional aid only.
   426  When Terraform v0.12 detects a use of the new `source` argument it doesn't
   427  understand, it will emit a warning to alert the user that it is disregarding
   428  the source address given in that argument.