我想使用 terraform 根据快速路由电路提供状态创建快速路由网关。创建快速路由线路后,初始提供商状态将取消配置。一旦在 equinix 门户中进行配置,此状态就会更改为已配置。要更改此状态,需要几天的时间,直到那时不需要创建快速路由网关,因为它的资源有点昂贵。当我运行管道时,最初快速路由电路将创建后,其状态将为未配置,现在在此状态下应跳过快速路由网关创建。当状态更改为已配置时,我将运行管道,这里只有当更改为已配置时才必须检查配置状态,应该创建快速路由网关。
resource "azurerm_resource_group" "example-express-rg" {
name = "example-vnet-rg"
location = "West Europe"
}
resource "azurerm_virtual_network" "vnettest" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example-express-rg.location
resource_group_name = azurerm_resource_group.example-express-rg.name
}
resource "azurerm_subnet" "gateway_subnet" {
name = "GatewaySubnet"
resource_group_name = azurerm_resource_group.example-express-rg.name
virtual_network_name = azurerm_virtual_network.vnettest.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_public_ip" "publicip" {
name = "example-public-ip"
location = azurerm_resource_group.example-express-rg.location
resource_group_name = azurerm_resource_group.example-express-rg.name
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_resource_group" "expressrg" {
name = "exprtTest"
location = "West Europe"
}
resource "azurerm_express_route_circuit" "expressr" {
name = "expressRoute1"
resource_group_name = azurerm_resource_group.expressrg.name
location = azurerm_resource_group.expressrg.location
service_provider_name = "Equinix"
peering_location = "Singapore"
bandwidth_in_mbps = 1000
sku {
tier = "Standard"
family = "MeteredData"
}
tags = {
Purpose = "Resource"
ResorceOwner ="CCTeam"
}
}
# Data Source to Check the Status of the ExpressRoute Circuit
data "azurerm_express_route_circuit" "expressr_status" {
name = azurerm_express_route_circuit.expressr.name
resource_group_name = azurerm_resource_group.expressrg.name
}
# Virtual Network Gateway (Create Conditionally)
resource "azurerm_virtual_network_gateway" "example" {
depends_on = [azurerm_express_route_circuit.expressr]
count =data.azurerm_express_route_circuit.expressr_status.service_provider_provisioning_state == "Provisioned" ? 1 : 0
name = "testgw"
location = azurerm_resource_group.example-express-rg.location
resource_group_name = azurerm_resource_group.example-express-rg.name
type = "ExpressRoute"
vpn_type = "PolicyBased"
sku = "Standard"
ip_configuration {
name = "vnetGatewayConfig"
public_ip_address_id = azurerm_public_ip.publicip.id
private_ip_address_allocation = "Dynamic"
subnet_id = azurerm_subnet.gateway_subnet.id
}
tags = {
Purpose = "Resource"
ResorceOwner ="CCTeam"
}
}
我尝试使用数据块和条件,但结果出现错误
错误:计数参数无效 │ │ 在 main.tf 第 75 行,资源“azurerm_virtual_network_gateway”“example”中: │ 75: 计数=data.azurerm_express_route_ Circuit.expressr_status.service_provider_provisioning_state ==“已配置”? 1:0 │ │ “计数”值取决于资源属性,这些属性在应用之前无法确定,因此 Terraform 无法预测如何 │ 将创建许多实例。要解决此问题,请使用 -target 参数首先仅应用 │ 计数取决于。
请就此提出建议。
使用 terraform 基于快速路由电路提供状态自动化快速路由电路网关
您共享的配置检查虚拟网络网关中配置状态的可用性,这会导致配置中出现错误,我们可能需要本地执行程序来验证快速路由的状态
由于需要几天时间才能提供,我只是根据要求提到了结构变化
演示配置:
resource "azurerm_express_route_circuit" "expressr" {
name = var.express_route_name
resource_group_name = azurerm_resource_group.expressrg.name
location = azurerm_resource_group.expressrg.location
service_provider_name = "Equinix"
peering_location = "Silicon Valley"
bandwidth_in_mbps = 50
sku {
tier = "Standard"
family = "MeteredData"
}
}
data "azurerm_express_route_circuit" "expressr_status" {
name = azurerm_express_route_circuit.expressr.name
resource_group_name = azurerm_resource_group.expressrg.name
}
resource "null_resource" "check_provision_status" {
provisioner "local-exec" {
interpreter = ["pwsh", "-Command"]
command = <<EOT
$status = "${data.azurerm_express_route_circuit.expressr_status.service_provider_provisioning_state}"
if ($status -ne "Provisioned") {
Write-Output "ExpressRoute circuit is not provisioned. Skipping gateway creation."
exit 1
} else {
Write-Output "ExpressRoute circuit is provisioned. Proceeding with gateway creation."
exit 0
}
EOT
}
triggers = {
status = data.azurerm_express_route_circuit.expressr_status.service_provider_provisioning_state
}
}
resource "azurerm_virtual_network_gateway" "example" {
depends_on = [null_resource.check_provision_status]
name = "tesvksgw"
location = azurerm_resource_group.example-express-rg.location
resource_group_name = azurerm_resource_group.example-express-rg.name
type = "ExpressRoute"
vpn_type = "PolicyBased"
sku = "Standard"
ip_configuration {
name = "vnetvkGatewayConfig"
public_ip_address_id = azurerm_public_ip.publicip.id
private_ip_address_allocation = "Dynamic"
subnet_id = azurerm_subnet.gateway_subnet.id
}
}
提供配置后,输出可能如下所示
部署:
这里状态仍然显示为未配置,因为它是最近创建的,我们需要等待提供者端的配置状态发生变化
如果资源已配置,则空资源将验证资源的状态并继续其余配置。
参考:
https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource
https://build5nines.com/terraform-deploy-azure-expressroute- Circuit-with-vnet-gateway/