用作输入变量的 Terraform 输出变量

Terraform output variables to be used as input variables

我想使用一个 resource/module 的输出变量作为另一个 resource/module 的输入。那可能吗?在这里,我希望根目录中 'outputs.tf' 的输出值用作模块 'main.tf' 中的输入。

    root
    |--main.tf
    |--vars.tf
    |--outputs.tf
    |---module
         |--main.tf
         |--vars.tf

当然可以。您无需再做任何事情。像往常一样做。这是一个例子:

main.tf
├── rg
│   ├── output.tf
│   └── rg.tf
└── vnet
    ├── output.tf
    └── vnet.tf

您照原样创建模块 rgvnet。设置你需要的输出。这里我设置了输出 rg_namerg_location。我还根据需要在模块 vnet 中设置了变量 rg_namerg_location 。然后 main.rf 显示在这里:

provider "azurerm" {
  features {}
}

module "rg" {
  source = "./rg"

  rg_name = "charlesTerraform"
}

module "vnet" {
  source = "./vnet"

  rg_name = module.rg.rg_name
  rg_location = module.rg.rg_location
}

output "vnet" {
  value = module.vnet.vnet
}

你看,我使用模块 rg 的输出作为模块 vnet 的输入。希望它能帮助您理解 Terraform 模块。

更新:

结构就是你说的也是这个道理。你只需要将你需要的输出输入到模块中。例如:

resource "azurerm_resource_group" "example" {
  name = "xxxxxx"
  location = "xxxx"
}

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

  resource_group = azurerm_resource_group.example.name
}

这只是一个示例,但它向您展示了如何实现它。希望你明白。

我认为您可以使用 terraform 输出命令将输出值作为变量插入到其他方式。

查看此 terraform 文档 https://www.terraform.io/cli/commands/output