跳至主要内容

Resource provider 元参数

provider 元参数指定要用于资源的提供程序配置,覆盖 OpenTofu 基于资源类型名称选择提供程序的默认行为。其值应为未加引号的 <PROVIDER>.<ALIAS> 引用。

提供程序配置 中所述,您可以选择为单个提供程序创建多个配置(通常用于管理多区域服务的不同区域中的资源)。每个提供程序可以有一个默认配置,以及任意数量的备用配置,这些配置包含一个额外的名称段(或“别名”)。

默认情况下,OpenTofu 将资源类型名称中的第一个单词(由下划线分隔)解释为提供程序的本地名称,并使用该提供程序的默认配置。例如,资源类型 google_compute_instance 自动与名为 google 的提供程序的默认配置关联。

通过使用 provider 元参数,您可以为资源选择备用提供程序配置。

代码块
# 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

# ...
}

资源始终对其关联的提供程序具有隐式依赖关系,以确保在执行任何资源操作之前完全配置提供程序。

provider 元参数需要 <PROVIDER>.<ALIAS> 引用,无需加引号。provider 不允许使用任意表达式,因为它必须在 OpenTofu 构建依赖关系图时解析,在此之前,评估表达式是不安全的。