我有点鸡鸡蛋问题。想象一下多个订阅,每个订阅都有自己的VNet。我需要使用同样在其订阅中的集线器来查看所有这些内容。问题是我为每个订阅使用单独的状态文件。
问题:我没有一个Terraform文件来构建整个环境。我为每个订阅运行一个申请。这意味着我将需要运行两次。。一次是为每个订阅进行站立操作以将VNet放置到位,然后在将模块/资源添加到文件中进行对等之后再次进行VNet对等。
这可能会引起很多问题。特别是如果我执行销毁/重新申请。
是否有更好的方法?如果它是整个环境的一个文件,Terraform将确保VNet存在,或者我可以使用'depends_on'属性。但是,我这样做的方式可能会破坏一切。
想法:
谢谢!
答案 0 :(得分:2)
如果您已在每个订阅中部署了每个VNet,我认为您可以执行两个步骤:一次运行设置订阅/ Vnet,第二次进行对等。 terraform对等配置将类似于this,将alias
用于将要引用的一个特定订阅,使用data
来查询每个订阅中的现有资源。确保您使用的服务主体对两个订阅都具有权限,或者对每个提供者块都使用了不同的服务主体(具有关联的权限)。
例如,
provider "azurerm" {
version = "xxx"
tenant_id = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
subscription_id = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
client_id = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
client_secret = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
alias = "dev"
}
provider "azurerm" {
version = "xxx"
tenant_id = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
subscription_id = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
client_id = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
client_secret = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
alias = "test"
}
data "azurerm_virtual_network" "dev" {
name = "dev-network"
resource_group_name = "dev-network-rg"
provider = "azurerm.dev"
}
data "azurerm_virtual_network" "test" {
name = "test-network"
resource_group_name = "test-network-rg"
provider = "azurerm.test"
}
resource "azurerm_virtual_network_peering" "dev-to-test" {
name = "dev-to-test"
resource_group_name = "${data.azurerm_virtual_network.test.resource_group_name}"
virtual_network_name = "${data.azurerm_virtual_network.test.name}"
remote_virtual_network_id = "${data.azurerm_virtual_network.test.id}"
allow_virtual_network_access = true
allow_forwarded_traffic = true
provider = "azurerm.dev"
}
resource "azurerm_virtual_network_peering" "test-to-dev" {
name = "test-to-dev"
resource_group_name = "${data.azurerm_virtual_network.dev.resource_group_name}"
virtual_network_name = "${data.azurerm_virtual_network.dev.name}"
remote_virtual_network_id = "${data.azurerm_virtual_network.dev.id}"
allow_virtual_network_access = true
allow_forwarded_traffic = true
provider = "azurerm.test"
}
如果使用的是Azure CLI身份验证,则可以参考this。
或者,如您所想,您可以尝试在引用this example的depends_on
块中使用azurerm_virtual_network_peering
属性。
resource "azurerm_virtual_network" "spoke1-vnet" {
provider = "azurerm.dev"
name = "spoke1-vnet"
location = azurerm_resource_group.spoke1-vnet-rg.location
resource_group_name = azurerm_resource_group.spoke1-vnet-rg.name
address_space = ["10.1.0.0/16"]
}
resource "azurerm_virtual_network_peering" "spoke1-hub-peer" {
provider = "azurerm.dev"
name = "spoke1-hub-peer"
resource_group_name = azurerm_resource_group.spoke1-vnet-rg.name
virtual_network_name = azurerm_virtual_network.spoke1-vnet.name
remote_virtual_network_id = azurerm_virtual_network.hub-vnet.id
allow_virtual_network_access = true
allow_forwarded_traffic = true
allow_gateway_transit = false
use_remote_gateways = true
depends_on = ["azurerm_virtual_network.spoke1-vnet", "azurerm_virtual_network.hub-vnet" , "azurerm_virtual_network_gateway.hub-vnet-gateway"]
}