Files
opentf/website/docs/language/meta-arguments/resource-provider.mdx
2024-12-05 07:46:02 -05:00

81 lines
3.2 KiB
Plaintext

---
description: >-
The provider meta-argument specifies the provider configuration OpenTofu
should use for a resource, overriding OpenTofu's default behavior.
---
import CodeBlock from '@theme/CodeBlock';
import ExampleDynamicInstances from '!!raw-loader!./examples/resource-provider-dynamic-instances.tf'
# The Resource `provider` Meta-Argument
The `provider` meta-argument specifies which provider instance is responsible for managing
each instance of a resource, overriding OpenTofu's default behavior of
[automatically selecting a default provider configuration](../../language/providers/configuration.mdx#default-provider-configurations).
As described in [Provider Configuration](../../language/providers/configuration.mdx), you
can optionally declare multiple configurations for a single provider, or multiple dynamic
instances of a single provider configuration, such as when managing resources across
different regions when using a provider which forces only a single region per provider
instance.
By default, OpenTofu interprets the initial word in the resource type name (separated by
underscores) as the local name of a provider, and uses that provider's default
configuration. For example, the resource type `google_compute_instance` is associated
automatically with the default configuration for the provider whose local name
in the current module is `google`.
Using the `provider` meta-argument you can select an alternate provider configuration
for a resource:
```hcl
# default configuration
provider "google" {
region = "us-central1"
}
# alternate configuration, whose alias is "europe"
provider "google" {
alias = "europe"
region = "europe-west1"
}
resource "google_compute_instance" "example" {
# This "provider" meta-argument selects the google provider
# configuration whose alias is "europe", rather than the
# default configuration.
provider = google.europe
# ...
}
```
If you select a provider configuration that uses `for_each` then you
must also dynamically select a different instance of the provider configuration for
each instance of the resource by including an instance key expression in brackets:
<CodeBlock language="hcl">{ExampleDynamicInstances}</CodeBlock>
{/* NOTE: The above example is shared with ../providers/configuration.mdx
and its text refers to specific declarations in the example. */}
You can find more detail on the syntax used with the `provider` argument in
[Referring to Provider Instances](../../language/providers/configuration.mdx#referring-to-provider-instances).
:::warning
**The `for_each` expression for a resource must not exactly match the
`for_each` expression for its associated provider configuration.**
OpenTofu uses a provider instance to plan and apply _all_ actions related
to a resource instance, including destroying a resource instance that
has been removed from the configuration.
Therefore the provider instance associated with any resource instance must
always remain in the configuration for at least one more plan/apply round
after the resource instance has been removed, or OpenTofu will fail to
plan to destroy the resource instance.
You can find more information on this constraint in
[Referring to Provider Instances](../../language/providers/configuration.mdx#referring-to-provider-instances).
:::