Créer une appliance de réseau virtuel hub dans Azure avec Terraform

Terraform permet la définition, l’aperçu et le déploiement d’une infrastructure cloud. Terraform vous permet de créer des fichiers de configuration à l’aide de la syntaxe HCL. La syntaxe HCL vous permet de spécifier un fournisseur de services cloud, tel qu’Azure, et les éléments qui composent votre infrastructure cloud. Après avoir créé vos fichiers de configuration, vous créez un plan d’exécution qui vous permet d’afficher un aperçu de vos modifications d’infrastructure avant leur déploiement. Une fois que vous avez vérifié les modifications, vous appliquez le plan d’exécution pour déployer l’infrastructure.

Un appareil VPN est un appareil qui fournit la connectivité externe à un réseau local. L’appareil VPN peut être un équipement matériel ou une solution logicielle. Le service de routage et d’accès à distance (RRAS) dans Windows Server 2012 est un exemple de solution logicielle. Pour plus d’informations sur les appliances VPN, voir À propos des appareils VPN pour les connexions de la passerelle VPN de site à site.

Azure prend en charge un large éventail d’appliances virtuelles réseau à partir desquelles faire son choix. Pour cet article, une image Ubuntu est utilisée. Pour en savoir plus sur la grande variété des solutions d’appareils prises en charge dans Azure, consultez la page d’accueil Appareils réseau.

Dans cet article, vous apprendrez comment :

  • Implémenter le réseau virtuel du hub dans la topologie hub-and-spoke
  • Créer une machine virtuelle de réseau hub devant servir d’appliance
  • Activer des routes à l’aide des extensions CustomScript
  • Créer des tables de routage de passerelle hub-and-spoke

1. Configurer votre environnement

  • Abonnement Azure : Si vous n’avez pas d’abonnement Azure, créez un compte gratuit avant de commencer.

2. Implémenter le code Terraform

  1. Faites de l’exemple de répertoire créé dans le premier article de cette série le répertoire actif.

  2. Créez un fichier nommé hub-nva.tf et insérez le code suivant :

    locals {
        prefix-hub-nva         = "hub-nva"
        hub-nva-location       = "eastus"
        hub-nva-resource-group = "hub-nva-rg"
    }
    
    resource "azurerm_resource_group" "hub-nva-rg" {
        name     = "${local.prefix-hub-nva}-rg"
        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 = var.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/mspnp/reference-architectures/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]
    }
    

Résoudre les problèmes liés à Terraform sur Azure

Résoudre les problèmes courants liés à l’utilisation de Terraform sur Azure

Étapes suivantes