Skip to main content

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.

As described in Provider Configuration, 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:

Code Block
# 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:

Code Block
variable "aws_regions" {
type = map(object({
vpc_cidr_block = string
}))
}

provider "aws" {
alias = "by_region"
for_each = var.aws_regions

region = each.key
}

resource "aws_vpc" "private" {
# This expression filters var.aws_regions to include only
# the elements whose value is not null. Refer to the
# warning in the text below for more information.
for_each = {
for region, config in var.aws_regions : region => config
if config != null
}
provider = aws.by_region[each.key]

cidr_block = each.value.vpc_cidr_block
}

You can find more detail on the syntax used with the provider argument in Referring to Provider Instances.