如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

huangapple go评论97阅读模式
英文:

How can I reference Firewall Private IP address in Terraform while creating Route in Azure

问题

我正在尝试通过Terraform为Azure创建路由,并希望将防火墙的私有IP地址作为下一跳地址。但是其中的代码都没有起作用。

  1. resource "azurerm_firewall" "Fireall-variable" {
  2. name = "Main-Firewall"
  3. location = azurerm_resource_group.East-rg-variable.location
  4. resource_group_name = azurerm_resource_group.East-rg-variable.name
  5. sku_name = "AZFW_VNet"
  6. sku_tier = "Standard"
  7. ip_configuration {
  8. name = "configuration"
  9. subnet_id = azurerm_subnet.subnet2.id
  10. public_ip_address_id = azurerm_public_ip.Firewallip-variable.id
  11. }
  12. }
  1. resource "azurerm_route_table" "westroute" {
  2. name = "West-route-table"
  3. location = azurerm_resource_group.East-rg-variable.location
  4. resource_group_name = azurerm_resource_group.East-rg-variable.name
  5. disable_bgp_route_propagation = false
  6. route {
  7. name = "route1"
  8. address_prefix = "0.0.0.0/0"
  9. next_hop_type = "VirtualAppliance"
  10. next_hop_in_ip_address = "10.0.1.4"
  11. }
  12. }
英文:

I am trying to create Route for Azure through Terraform and and wants to next Firewall's private IP address as next hop address. But none of the coding is working.

  1. resource "azurerm_firewall" "Fireall-variable" {
  2. name = "Main-Firewall"
  3. location = azurerm_resource_group.East-rg-variable.location
  4. resource_group_name = azurerm_resource_group.East-rg-variable.name
  5. sku_name = "AZFW_VNet"
  6. sku_tier = "Standard"
  7. ip_configuration {
  8. name = "configuration"
  9. subnet_id = azurerm_subnet.subnet2.id
  10. public_ip_address_id = azurerm_public_ip.Firewallip-variable.id
  11. }
  12. }
  1. resource "azurerm_route_table" "westroute" {
  2. name = "West-route-table"
  3. location = azurerm_resource_group.East-rg-variable.location
  4. resource_group_name = azurerm_resource_group.East-rg-variable.name
  5. disable_bgp_route_propagation = false
  6. route {
  7. name = "route1"
  8. address_prefix = "0.0.0.0/0"
  9. next_hop_type = "VirtualAppliance"
  10. next_hop_in_ip_address = "10.0.1.4"
  11. }

答案1

得分: 1

我在我的环境中进行了复制,并获得了如下所示的预期结果:

以下是我使用创建带有路由表的Azure防火墙的代码,并且我遵循了Document1Document2

  1. provider "azurerm" {
  2. features {}
  3. }
  4. resource "azurerm_resource_group" "emo-rg" {
  5. name = "emo-resources"
  6. location = "West Europe"
  7. }
  8. resource "azurerm_public_ip" "example" {
  9. name = "testpip"
  10. location = azurerm_resource_group.emo-rg.location
  11. resource_group_name = azurerm_resource_group.emo-rg.name
  12. allocation_method = "Static"
  13. sku = "Standard"
  14. }
  15. resource "azurerm_virtual_network" "vnet" {
  16. name = "ritwik-vnet"
  17. address_space = ["10.0.0.0/16"]
  18. location = azurerm_resource_group.emo-rg.location
  19. resource_group_name = azurerm_resource_group.emo-rg.name
  20. }
  21. resource "azurerm_subnet" "subnet" {
  22. name = "AzureFirewallSubnet"
  23. resource_group_name = azurerm_resource_group.emo-rg.name
  24. virtual_network_name = azurerm_virtual_network.vnet.name
  25. address_prefixes = ["10.0.1.0/24"]
  26. }
  27. resource "azurerm_firewall" "firewall" {
  28. name = "testfirewall"
  29. location = azurerm_resource_group.emo-rg.location
  30. resource_group_name = azurerm_resource_group.emo-rg.name
  31. sku_name = "AZFW_VNet"
  32. sku_tier = "Premium"
  33. ip_configuration {
  34. name = "configuration"
  35. subnet_id = azurerm_subnet.subnet.id
  36. public_ip_address_id = azurerm_public_ip.example.id
  37. }
  38. }
  39. resource "azurerm_route_table" "westroute" {
  40. name = "West-route-table"
  41. location = azurerm_resource_group.emo-rg.location
  42. resource_group_name = azurerm_resource_group.emo-rg.name
  43. disable_bgp_route_propagation = false
  44. route {
  45. name = "route1"
  46. address_prefix = "0.0.0.0/0"
  47. next_hop_type = "VirtualAppliance"
  48. next_hop_in_ip_address = azurerm_firewall.firewall.ip_configuration[0].private_ip_address
  49. }
  50. }

输出:

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

如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

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

如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

现在在防火墙中:

如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

英文:

I have reproduced in my environment and got expected results as below:

Here is the code with which I created Azure Firewall with route table and I followed Document1 and Document2:

  1. provider "azurerm" {
  2. features {}
  3. }
  4. resource "azurerm_resource_group" "emo-rg" {
  5. name = "emo-resources"
  6. location = "West Europe"
  7. }
  8. resource "azurerm_public_ip" "example" {
  9. name = "testpip"
  10. location = azurerm_resource_group.emo-rg.location
  11. resource_group_name = azurerm_resource_group.emo-rg.name
  12. allocation_method = "Static"
  13. sku = "Standard"
  14. }
  15. resource "azurerm_virtual_network" "vnet" {
  16. name = "ritwik-vnet"
  17. address_space = ["10.0.0.0/16"]
  18. location = azurerm_resource_group.emo-rg.location
  19. resource_group_name = azurerm_resource_group.emo-rg.name
  20. }
  21. resource "azurerm_subnet" "subnet" {
  22. name = "AzureFirewallSubnet"
  23. resource_group_name = azurerm_resource_group.emo-rg.name
  24. virtual_network_name = azurerm_virtual_network.vnet.name
  25. address_prefixes = ["10.0.1.0/24"]
  26. }
  27. resource "azurerm_firewall" "firewall" {
  28. name = "testfirewall"
  29. location = azurerm_resource_group.emo-rg.location
  30. resource_group_name = azurerm_resource_group.emo-rg.name
  31. sku_name = "AZFW_VNet"
  32. sku_tier = "Premium"
  33. ip_configuration {
  34. name = "configuration"
  35. subnet_id = azurerm_subnet.subnet.id
  36. public_ip_address_id = azurerm_public_ip.example.id
  37. }
  38. }
  39. resource "azurerm_route_table" "westroute" {
  40. name = "West-route-table"
  41. location = azurerm_resource_group.emo-rg.location
  42. resource_group_name = azurerm_resource_group.emo-rg.name
  43. disable_bgp_route_propagation = false
  44. route {
  45. name = "route1"
  46. address_prefix = "0.0.0.0/0"
  47. next_hop_type = "VirtualAppliance"
  48. next_hop_in_ip_address = azurerm_firewall.firewall.ip_configuration[0].private_ip_address
  49. }
  50. }

如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

Output:

Resources created after executing terraform code:

如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

After running the above code successfully, Route table is created with the below IP Address:

如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

Now in Firewall:

如何在创建Azure路由时在Terraform中引用防火墙私有IP地址。

huangapple
  • 本文由 发表于 2023年4月19日 22:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定