如何在Terraform中引用防火墙专用IP地址,同时在Azure中创建路由

uemypmqf  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(98)

我试图通过Terraform为Azure创建路由,并希望下一个防火墙的私有IP地址作为下一跳地址。但没有编码工作。

resource "azurerm_firewall" "Fireall-variable" {
  name                = "Main-Firewall"
location =  azurerm_resource_group.East-rg-variable.location
 resource_group_name = azurerm_resource_group.East-rg-variable.name
  sku_name            = "AZFW_VNet"
  sku_tier            = "Standard"

ip_configuration {
    name                 = "configuration"
    subnet_id            = azurerm_subnet.subnet2.id
    public_ip_address_id = azurerm_public_ip.Firewallip-variable.id
  }
}
resource "azurerm_route_table" "westroute" {
  name                          = "West-route-table"
  location                      = azurerm_resource_group.East-rg-variable.location
  resource_group_name           = azurerm_resource_group.East-rg-variable.name
  disable_bgp_route_propagation = false

  route {
    name           = "route1"
   address_prefix = "0.0.0.0/0"
   next_hop_type  = "VirtualAppliance"
   next_hop_in_ip_address = "10.0.1.4"
 }
wfypjpf4

wfypjpf41#

我已经在我的环境中复制了,并得到了预期的结果如下:
以下是我使用路由表创建Azure防火墙的代码,并遵循Document1Document2

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "emo-rg" {
  name     = "emo-resources"
  location = "West Europe"
}

resource "azurerm_public_ip" "example" {
  name                = "testpip"
  location            = azurerm_resource_group.emo-rg.location
  resource_group_name = azurerm_resource_group.emo-rg.name
  allocation_method   = "Static"
  sku                 = "Standard"
}

resource "azurerm_virtual_network" "vnet" {
  name                = "ritwik-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.emo-rg.location
  resource_group_name = azurerm_resource_group.emo-rg.name
}

resource "azurerm_subnet" "subnet" {
  name                 = "AzureFirewallSubnet"
  resource_group_name  = azurerm_resource_group.emo-rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_firewall" "firewall" {
  name                = "testfirewall"
  location            = azurerm_resource_group.emo-rg.location
  resource_group_name = azurerm_resource_group.emo-rg.name
  sku_name            = "AZFW_VNet"
  sku_tier            = "Premium"

  ip_configuration {
    name                 = "configuration"
    subnet_id            = azurerm_subnet.subnet.id
    public_ip_address_id = azurerm_public_ip.example.id
  }
}

resource "azurerm_route_table" "westroute" {
  name                          = "West-route-table"
  location                      = azurerm_resource_group.emo-rg.location
  resource_group_name           = azurerm_resource_group.emo-rg.name
  disable_bgp_route_propagation = false

  route {
    name                 = "route1"
    address_prefix       = "0.0.0.0/0"
    next_hop_type        = "VirtualAppliance"
    next_hop_in_ip_address = azurerm_firewall.firewall.ip_configuration[0].private_ip_address
  }
}

输出:

执行terraform代码后创建的资源:

成功运行上述代码后,将使用以下IP地址创建路由表:

在Firewall中:

相关问题