从模块中引用 terraform_remote_state

Referencing terraform_remote_state from within module

使用 Terraform v0.13.5

我有一个模块,其中有一些输出来自其中的一些子模块,例如:

module "egressvnet" {
  source = "../modules/vnet/egress"
}

output "subnet" {
  value = module.egressvnet.subnet
}

terraform output 确认我期望输出的是。

在另一个 terraform 设置中,我想参考上面的输出。

所以我的 terraform 配置中有这个:

data "terraform_remote_state" "network" {
  backend = "azurerm"

  config = {
    resource_group_name  = "xxx"
    storage_account_name = "xxx"
    container_name       = "terraform"
    key                  = "network.tfstate"
  }
}

module "web" {
  source = "../modules/web"

  subnet_id = terraform_remote_state.network.outputs.subnet

}
Is what I'm trying to do possible?

但是当我做一个计划时我得到这个错误:

Error: Reference to undeclared resource

  on base.tf line 111, in module "web":
 111:   subnet_id = terraform_remote_state.network.outputs.subnet

A managed resource "terraform_remote_state" "network" has not been declared in
the root module.

由于您的 terraform_remote_statedata source,您应该使用 data.:

来引用它
subnet_id = data.terraform_remote_state.network.outputs.subnet