TerraformでAzure Load BalancerからVM(HTTP Server)にルーティングするシンプルな構成

# はじめに
タイトルにある通りTerraformで「Azure Load BalancerからVM(HTTPサーバー)にルーティングしてWebページを表示」するシンプルな構成のサンプルを作成したので書いていきます。

Terraformの基本的な操作は前回の記事にも書いていますのでぜひ。

https://qiita.com/tkhs1121/items/b68273bcb6dda3e18a27

# 構成
- Load Balancer x1
- Virtual Machine x1

## ソースコード
やっていることはシンプルなんですが、結構コード量が多いです！

https://github.com/tkhs1121/terraform-azure-lb-to-vm

### メインの部分抜粋
重要なところはコメントで補足しています。

```tf:main.tf
resource "azurerm_resource_group" "test" {
  name     = "test-loadbalancer-rg"
  location = "japaneast"
}

resource "azurerm_lb" "test" {
  name                = "test-loadbalancer"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  sku                 = "Standard"

  frontend_ip_configuration {
    name                  = "test-public-ip-address"
    public_ip_address_id  = azurerm_public_ip.lb.id
  }
}

resource "azurerm_lb_backend_address_pool" "test" {
  name            = "test-lb-backend-address-pool"
  loadbalancer_id = azurerm_lb.test.id
  
}
# ここでVMのNICとLBのバックエンドアドレスプールを関連づけている
resource "azurerm_network_interface_backend_address_pool_association" "test" {
  network_interface_id    = azurerm_network_interface.test.id
  ip_configuration_name   = "test-ip-config"
  backend_address_pool_id = azurerm_lb_backend_address_pool.test.id

}
# HTTPサーバーにルーティングする場合は、ここで設定する
resource "azurerm_lb_probe" "test" {
  name            = "test-probe"
  protocol        = "Http"
  request_path    = "/"
  port            = 80
  loadbalancer_id = azurerm_lb.test.id
}
# ルーティングの設定
resource "azurerm_lb_rule" "test" {
  name                           = "test_lb_rule"
  protocol                       = "Tcp"
  frontend_port                  = 80
  backend_port                   = 80
  frontend_ip_configuration_name = azurerm_lb.test.frontend_ip_configuration[0].name
  backend_address_pool_ids       = [ azurerm_lb_backend_address_pool.test.id ]
  probe_id                       = azurerm_lb_probe.test.id
  loadbalancer_id                = azurerm_lb.test.id
}
```

```tf:virtualmachine.tf
resource "azurerm_linux_virtual_machine" "test" {
  name                = "test-machine"
  resource_group_name = azurerm_resource_group.test.name
  location            = azurerm_resource_group.test.location
  size                = "Standard_DS1_v2"
  admin_username      = "adminuser"
  network_interface_ids = [ azurerm_network_interface.test.id ]

  admin_ssh_key {
    username   = "adminuser"
    public_key = tls_private_key.keygen.public_key_openssh
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
  # Ubuntu 22.04LTSのインストール
  source_image_reference {
    offer     = "0001-com-ubuntu-server-jammy"
    publisher = "canonical"
    sku       = "22_04-lts-gen2"
    version   = "latest"
  }
}
#VMにnginxのインストール
resource "azurerm_virtual_machine_extension" "test" {
  name                 = "test-vm-nginx"
  virtual_machine_id   = azurerm_linux_virtual_machine.test.id
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"

  settings = <<INIT
  {
    "commandToExecute": "apt-get update && apt-get install -y nginx"
  }
INIT
}

resource "azurerm_network_security_group" "test" {
  name                = "test-nsg"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_subnet_network_security_group_association" "test" {
  subnet_id                 = azurerm_subnet.test.id
  network_security_group_id = azurerm_network_security_group.test.id
}

resource "azurerm_network_security_rule" "test" {
  name                        = "HTTP"
  access                      = "Allow"
  destination_address_prefix  = "*"
  destination_port_range      = "80"
  direction                   = "Inbound"
  priority                    = 100
  protocol                    = "Tcp"
  source_port_range           = "*"
  source_address_prefix       = "*"
  resource_group_name         = azurerm_resource_group.test.name
  network_security_group_name = azurerm_network_security_group.test.name
}

```

# 最後に
誰かの参考になれば嬉しいです。