LoginSignup
1
1

More than 1 year has passed since last update.

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

Posted at

はじめに

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

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

構成

  • Load Balancer x1
  • Virtual Machine x1

ソースコード

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

メインの部分抜粋

重要なところはコメントで補足しています。

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
}
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
}

最後に

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1