azure - 允许应用程序网关后端池指向特定 VM

标签 azure terraform terraform-provider-azure

如何使用 terraform 代码将虚拟机添加为应用程序网关的后端池中的目标?

enter image description here

我应该在以下代码中使用什么属性来指向特定的虚拟机?

    resource "azurerm_application_gateway" "network" {
  name                = "example-appgateway"
  resource_group_name = "${azurerm_resource_group.test.name}"
  location            = "${azurerm_resource_group.test.location}"

  sku {
    name     = "Standard_Small"
    tier     = "Standard"
    capacity = 2
  }

  gateway_ip_configuration {
    name      = "my-gateway-ip-configuration"
    subnet_id = "${azurerm_subnet.frontend.id}"
  }

  frontend_port {
    name = "${local.frontend_port_name}"
    port = 80
  }

  frontend_ip_configuration {
    name                 = "${local.frontend_ip_configuration_name}"
    public_ip_address_id = "${azurerm_public_ip.test.id}"
  }

  backend_address_pool {
    name = "${local.backend_address_pool_name}"
  }

  backend_http_settings {
    name                  = "${local.http_setting_name}"
    cookie_based_affinity = "Disabled"
    port                  = 80
    protocol              = "Http"
    request_timeout       = 1
  }

  http_listener {
    name                           = "${local.listener_name}"
    frontend_ip_configuration_name = "${local.frontend_ip_configuration_name}"
    frontend_port_name             = "${local.frontend_port_name}"
    protocol                       = "Http"
  }

  request_routing_rule {
    name                       = "${local.request_routing_rule_name}"
    rule_type                  = "Basic"
    http_listener_name         = "${local.listener_name}"
    backend_address_pool_name  = "${local.backend_address_pool_name}"
    backend_http_settings_name = "${local.http_setting_name}"
  }
}

backend_address_pool block 支持:

  • 名称 -(必填)用户为后端地址池定义的名称。
  • ip_addresses -(可选)公共(public) IPA 地址或内部 IP 列表 后端地址池中的地址。
  • fqdns -(可选)后端地址池中的 FQDN 列表

最佳答案

Terraform 在这方面有点愚蠢,您必须使用单独的代理资源(Azure 中甚至不存在)来配置它:

resource "azurerm_resource_group" "test" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "test" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "frontend" {
  name                 = "frontend"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_network_name = "${azurerm_virtual_network.test.name}"
  address_prefix       = "10.254.0.0/24"
}

resource "azurerm_subnet" "backend" {
  name                 = "backend"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_network_name = "${azurerm_virtual_network.test.name}"
  address_prefix       = "10.254.2.0/24"
}

resource "azurerm_public_ip" "test" {
  name                = "example-pip"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  allocation_method   = "Dynamic"
}

# since these variables are re-used - a locals block makes this more maintainable
locals {
  backend_address_pool_name      = "${azurerm_virtual_network.test.name}-beap"
  frontend_port_name             = "${azurerm_virtual_network.test.name}-feport"
  frontend_ip_configuration_name = "${azurerm_virtual_network.test.name}-feip"
  http_setting_name              = "${azurerm_virtual_network.test.name}-be-htst"
  listener_name                  = "${azurerm_virtual_network.test.name}-httplstn"
  request_routing_rule_name      = "${azurerm_virtual_network.test.name}-rqrt"
}

resource "azurerm_application_gateway" "network" {
  name                = "example-appgateway"
  resource_group_name = "${azurerm_resource_group.test.name}"
  location            = "${azurerm_resource_group.test.location}"

  removed for brievity, check full example at the link below
}

# binding happens here
resource "azurerm_network_interface_application_gateway_backend_address_pool_association" "test" {
  network_interface_id    = "${azurerm_network_interface.test.id}"
  ip_configuration_name   = "testconfiguration1"
  backend_address_pool_id = "${azurerm_application_gateway.test.backend_address_pool.0.id}"
}

阅读:
https://www.terraform.io/docs/providers/azurerm/r/network_interface_application_gateway_backend_address_pool_association.html

关于azure - 允许应用程序网关后端池指向特定 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54979017/

相关文章:

AzureFunctions 绑定(bind)到 SendGrid

基于 'Errors' 的 Azure 自动化警报

azure - Cosmos DB 分页提供倍增的页面记录

azure - 从 ARM 模板中的 Azure key 保管库获取最新版本的证书

git - 如何通过 ssh 获取托管在 Azure DevOps Repo 中的 Terraform 模块

sql-server - 停止/启动 Azure SQL Server 托管实例

azure - 诊断设置 - Master”已经存在 - 要通过 Terraform 进行管理,需要将此资源导入到状态中

terraform - 计划错误 : Cloud Resource Manager API has not been used

amazon-web-services - 如何使用 terraform 将运动流与流水线传输流连接起来

azure - 如何使用 Azure Terraform 忽略带有空格的特定标签名称的更改