【发布时间】:2019-07-19 11:30:56
【问题描述】:
我从 3 个单独的 terraform 目录开始,用于网络、虚拟机和数据库(在 Azure 中),我会在每个目录中使用 terraform apply。
terraform 文件中存在一些重复,例如每个文件中的 Azure 资源组。
# Create a resource group
resource "azurerm_resource_group" "resource-group" {
name = "terraform-rg"
location = "eastus"
}
因此,我现在想重组代码,以便从根处的单个 main.tf 调用所有 3 个代码,而我只调用一次 terraform apply。
但是,我是新手,如果资源组位于根目录且不再位于同一文件中,我不确定如何引用它。
例如用于 VM 的 vnet 如下所示:
# Create a virtual network for the VM
resource "azurerm_virtual_network" "vm-vnet" {
name = "terraform-client1-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.resource-group.location
resource_group_name = azurerm_resource_group.resource-group.name
}
location 和 resource_group_name 不再解析,因为 azurerm_resource_group.resource-group 不再位于同一文件中,而是位于根目录下的 main.tf 中。
正确重构此问题以解决所有问题的过程是什么?
有import 声明吗?
【问题讨论】:
标签: terraform