次の方法で共有


Terraform を使用して Azure にハブ仮想ネットワーク アプライアンスを作成する

Terraform を使用すると、クラウド インフラストラクチャの定義、プレビュー、デプロイが可能になります。 Terraform を使用して、 HCL 構文を使用して構成ファイルを作成します。 HCL 構文を使用すると、クラウド プロバイダー (Azure など) とクラウド インフラストラクチャを構成する要素を指定できます。 構成ファイルを作成したら、インフラストラクチャの変更をデプロイする前にプレビューできる 実行プラン を作成します。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。

VPN デバイスは、オンプレミス ネットワークへの外部接続を提供するデバイスです。 VPN デバイスは、ハードウェア デバイスまたはソフトウェア ソリューションである場合があります。 ソフトウェア ソリューションの 1 つの例として、Windows Server 2012 のルーティングとリモート アクセス サービス (RRAS) があります。 VPN アプライアンスの詳細については、「 サイト間 VPN Gateway 接続の VPN デバイスについて」を参照してください。

Azure では、選択するさまざまなネットワーク仮想アプライアンスがサポートされています。 この記事では、Ubuntu イメージを使用します。 Azure でサポートされているさまざまなデバイス ソリューションの詳細については、 ネットワーク アプライアンスのホーム ページを参照してください。

この記事では、次の方法について説明します。

  • ハブスポーク トポロジにハブ VNet を実装する
  • アプライアンスとして機能するハブ ネットワーク仮想マシンを作成する
  • CustomScript 拡張機能を使用してルートを有効にする
  • ハブ アンド スポーク ゲートウェイ ルート テーブルを作成する

1. 環境を構成する

  • Azure サブスクリプション: Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。
  • Terraform の構成: まだ構成していない場合は、次のいずれかのオプションを使用して Terraform を構成します。

2. Terraform コードを実装する

  1. このシリーズの最初の記事で作成したディレクトリの例を現在のディレクトリにします。

  2. hub-nva.tf という名前のファイルを作成し、次のコードを挿入します。

    locals {
      prefix-hub-nva         = "hub-nva"
      hub-nva-location       = "eastus"
      hub-nva-resource-group = "hub-nva-rg"
    }
    
    resource "random_string" "suffix" {
      length  = 5
      special = false
      upper   = false
    }
    
    resource "azurerm_resource_group" "hub-nva-rg" {
      name     = "${local.prefix-hub-nva}-rg-${random_string.suffix.result}"
      location = local.hub-nva-location
    
      tags = {
        environment = local.prefix-hub-nva
      }
    }
    
    resource "azurerm_network_interface" "hub-nva-nic" {
      name                 = "${local.prefix-hub-nva}-nic"
      location             = azurerm_resource_group.hub-nva-rg.location
      resource_group_name  = azurerm_resource_group.hub-nva-rg.name
      enable_ip_forwarding = true
    
      ip_configuration {
        name                          = local.prefix-hub-nva
        subnet_id                     = azurerm_subnet.hub-dmz.id
        private_ip_address_allocation = "Static"
        private_ip_address            = "10.0.0.36"
      }
    
      tags = {
        environment = local.prefix-hub-nva
      }
    }
    
    resource "azurerm_virtual_machine" "hub-nva-vm" {
      name                  = "${local.prefix-hub-nva}-vm"
      location              = azurerm_resource_group.hub-nva-rg.location
      resource_group_name   = azurerm_resource_group.hub-nva-rg.name
      network_interface_ids = [azurerm_network_interface.hub-nva-nic.id]
      vm_size               = var.vmsize
    
      storage_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "16.04-LTS"
        version   = "latest"
      }
    
      storage_os_disk {
        name              = "myosdisk1"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
      }
    
      os_profile {
        computer_name  = "${local.prefix-hub-nva}-vm"
        admin_username = var.username
        admin_password = local.password
      }
    
      os_profile_linux_config {
        disable_password_authentication = false
      }
    
      tags = {
        environment = local.prefix-hub-nva
      }
    }
    
    resource "azurerm_virtual_machine_extension" "enable-routes" {
      name                 = "enable-iptables-routes"
      virtual_machine_id   = azurerm_virtual_machine.hub-nva-vm.id
      publisher            = "Microsoft.Azure.Extensions"
      type                 = "CustomScript"
      type_handler_version = "2.0"
    
    
      settings = <<SETTINGS
        {
            "fileUris": [
            "https://raw.githubusercontent.com/lonegunmanb/reference-architectures/refs/heads/master/scripts/linux/enable-ip-forwarding.sh"
            ],
            "commandToExecute": "bash enable-ip-forwarding.sh"
        }
    SETTINGS
    
      tags = {
        environment = local.prefix-hub-nva
      }
    }
    
    resource "azurerm_route_table" "hub-gateway-rt" {
      name                          = "hub-gateway-rt"
      location                      = azurerm_resource_group.hub-nva-rg.location
      resource_group_name           = azurerm_resource_group.hub-nva-rg.name
      disable_bgp_route_propagation = false
    
      route {
        name           = "toHub"
        address_prefix = "10.0.0.0/16"
        next_hop_type  = "VnetLocal"
      }
    
      route {
        name                   = "toSpoke1"
        address_prefix         = "10.1.0.0/16"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.0.0.36"
      }
    
      route {
        name                   = "toSpoke2"
        address_prefix         = "10.2.0.0/16"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.0.0.36"
      }
    
      tags = {
        environment = local.prefix-hub-nva
      }
    }
    
    resource "azurerm_subnet_route_table_association" "hub-gateway-rt-hub-vnet-gateway-subnet" {
      subnet_id      = azurerm_subnet.hub-gateway-subnet.id
      route_table_id = azurerm_route_table.hub-gateway-rt.id
      depends_on     = [azurerm_subnet.hub-gateway-subnet]
    }
    
    resource "azurerm_route_table" "spoke1-rt" {
      name                          = "spoke1-rt"
      location                      = azurerm_resource_group.hub-nva-rg.location
      resource_group_name           = azurerm_resource_group.hub-nva-rg.name
      disable_bgp_route_propagation = false
    
      route {
        name                   = "toSpoke2"
        address_prefix         = "10.2.0.0/16"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.0.0.36"
      }
    
      route {
        name           = "default"
        address_prefix = "0.0.0.0/0"
        next_hop_type  = "VnetLocal"
      }
    
      tags = {
        environment = local.prefix-hub-nva
      }
    }
    
    resource "azurerm_subnet_route_table_association" "spoke1-rt-spoke1-vnet-mgmt" {
      subnet_id      = azurerm_subnet.spoke1-mgmt.id
      route_table_id = azurerm_route_table.spoke1-rt.id
      depends_on     = [azurerm_subnet.spoke1-mgmt]
    }
    
    resource "azurerm_subnet_route_table_association" "spoke1-rt-spoke1-vnet-workload" {
      subnet_id      = azurerm_subnet.spoke1-workload.id
      route_table_id = azurerm_route_table.spoke1-rt.id
      depends_on     = [azurerm_subnet.spoke1-workload]
    }
    
    resource "azurerm_route_table" "spoke2-rt" {
      name                          = "spoke2-rt"
      location                      = azurerm_resource_group.hub-nva-rg.location
      resource_group_name           = azurerm_resource_group.hub-nva-rg.name
      disable_bgp_route_propagation = false
    
      route {
        name                   = "toSpoke1"
        address_prefix         = "10.1.0.0/16"
        next_hop_in_ip_address = "10.0.0.36"
        next_hop_type          = "VirtualAppliance"
      }
    
      route {
        name           = "default"
        address_prefix = "0.0.0.0/0"
        next_hop_type  = "VnetLocal"
      }
    
      tags = {
        environment = local.prefix-hub-nva
      }
    }
    
    resource "azurerm_subnet_route_table_association" "spoke2-rt-spoke2-vnet-mgmt" {
      subnet_id      = azurerm_subnet.spoke2-mgmt.id
      route_table_id = azurerm_route_table.spoke2-rt.id
      depends_on     = [azurerm_subnet.spoke2-mgmt]
    }
    
    resource "azurerm_subnet_route_table_association" "spoke2-rt-spoke2-vnet-workload" {
      subnet_id      = azurerm_subnet.spoke2-workload.id
      route_table_id = azurerm_route_table.spoke2-rt.id
      depends_on     = [azurerm_subnet.spoke2-workload]
    }
    

Azure での Terraform のトラブルシューティング

Azure で Terraform を使用するときの一般的な問題のトラブルシューティング

次のステップ