APGWの検証環境をaz cliでバサッと作る

# はじめに
githubにリポジトリ作って保管するほどでもないと思ったのでここに全部おいていく

# スクリプト

```:powershell
# Define variables
$resource_group_name = "pro-nabehiro-001"
$location = "japaneast"
$vnet_name = "vnet-nabehiro-001"
$subnet_name1 = "subnet-nabehiro-001"
$subnet_name2 = "subnet-nabehiro-002"
$app_gateway_name = "nbfr151x"
$vm_names = @("nbfr151v", "nbfr152v", "nbfr153v")
$username = "azureuser"
$ssh_key_name = "ssh-nabehiro-001"
$vm_size = "Standard_B1ls"
$vm_image = "CentOS"

az login

write-host "# Create a resource group"
# Create a resource group
az group create --name $resource_group_name --location $location

Write-Host "# Create a virtual network and subnet"
# Create a virtual network and subnet
Write-Host "# Create a "$vnet_name
az network vnet create --name $vnet_name --resource-group $resource_group_name --location $location --address-prefix 10.0.0.0/16
Write-Host "# Create a "$subnet_name1
az network vnet subnet create --resource-group $resource_group_name --vnet-name $vnet_name --name $subnet_name1 --address-prefixes 10.0.1.0/24
Write-Host "# Create a "$subnet_name2
az network vnet subnet create --resource-group $resource_group_name --vnet-name $vnet_name --name $subnet_name2 --address-prefixes 10.0.2.0/24


Write-Host "# Create a ssh-key"
# Create a ssh-key
Write-Host "# Create a "$ssh_key_name
az sshkey create --name $ssh_key_name --resource-group $resource_group_name

Write-Host "# Create virtual machines"
# Create virtual machines
foreach ($vm_name in $vm_names)
{
Write-Host "# Create "$vm_name
$subnet_name -Image "UbuntuLTS"
az vm create --name $vm_name --resource-group $resource_group_name --location $location --vnet-name $vnet_name --subnet $subnet_name1 --size $vm_size --admin-username $username --ssh-key-name $ssh_key_name --image $vm_image
}

Write-Host "# Get the virtual machine IP addresses"
# Get the virtual machine IP addresses
$vm_ips = @()
foreach ($vm_name in $vm_names)
{
  Write-Host "add "$vm_name" ipaddress"
  $vm_ips+=($(az vm list-ip-addresses --name $vm_name --resource-group $resource_group_name --query "[].virtualMachine.network.privateIpAddresses" -o tsv))
}
Write-Host "show ip-lists:"$vm_ips

Write-Host "# Create an application gateway"
# Create an application gateway
Capacity 2 -VirtualNetworkName $vnet_name -SubnetName $subnet_nameaz network application-gateway create --name $app_gateway_name --resource-group $resource_group_name --location $location --sku WAF_v2 --capacity 2 --vnet-name $vnet_name --subnet $subnet_name
write-host "Create "$app_gateway_name
az network application-gateway create --name $app_gateway_name --resource-group $resource_group_name --location $location --sku "Standard_Small" --capacity 2 --vnet-name $vnet_name --subnet $subnet_name2

Write-Host "# Create a address pool"
# Create a address pool
$resource_group_name
az network application-gateway address-pool create --resource-group $resource_group_name --gateway-name $app_gateway_name --name appGatewayBackendPool --server $vm_ips

Write-Host "Finished!"
```

# 記念にログも残す

```
PS C:\Users\watya\work\powershell\create_apgw> .\create_apgw.ps1
The default web browser has been opened at https://login.microsoftonline.com/common/oauth2/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
You have logged in. Now let us find all the subscriptions to which you have access...
The following tenants don't contain accessible subscriptions. Use 'az login --allow-no-subscriptions' to have tenant level access.
9e625ed5-dab8-4524-9bd5-32caaadc6486 'AdatumLab500-04'
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "237554c3-2b59-4af4-bfb3-f656ac941c21",
    "id": "2a7080ea-1981-457b-aac9-902be7c5f00d",
    "isDefault": true,
    "managedByTenants": [],
    "name": "従量課金",
    "state": "Enabled",
    "tenantId": "237554c3-2b59-4af4-bfb3-f656ac941c21",
    "user": {
      "name": "watyanabe164@gmail.com",
      "type": "user"
    }
  }
]
# Create a resource group
{
  "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001",
  "location": "japaneast",
  "managedBy": null,
  "name": "pro-nabehiro-001",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}
# Create a virtual network and subnet
# Create a  vnet-nabehiro-001
{
  "newVNet": {
    "addressSpace": {
      "addressPrefixes": [
        "10.0.0.0/16"
      ]
    },
    "bgpCommunities": null,
    "ddosProtectionPlan": null,
    "dhcpOptions": {
      "dnsServers": []
    },
    "enableDdosProtection": false,
    "enableVmProtection": null,
    "etag": "W/\"68fe36f1-ead5-4dfa-93fb-9a0967f7a226\"",
    "extendedLocation": null,
    "flowTimeoutInMinutes": null,
    "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/virtualNetworks/vnet-nabehiro-001",
    "ipAllocations": null,
    "location": "japaneast",
    "name": "vnet-nabehiro-001",
    "provisioningState": "Succeeded",
    "resourceGroup": "pro-nabehiro-001",
    "resourceGuid": "31f3d33c-9585-4ffa-8558-0fcf5440ecea",
    "subnets": [],
    "tags": {},
    "type": "Microsoft.Network/virtualNetworks",
    "virtualNetworkPeerings": []
  }
}
# Create a  subnet-nabehiro-001
{
  "addressPrefix": "10.0.1.0/24",
  "addressPrefixes": null,
  "applicationGatewayIpConfigurations": null,
  "delegations": [],
  "etag": "W/\"34f4621f-f2a8-44c6-a3d4-3fd7dbee42db\"",
  "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/virtualNetworks/vnet-nabehiro-001/subnets/subnet-nabehiro-001",
  "ipAllocations": null,
  "ipConfigurationProfiles": null,
  "ipConfigurations": null,
  "name": "subnet-nabehiro-001",
  "natGateway": null,
  "networkSecurityGroup": null,
  "privateEndpointNetworkPolicies": "Enabled",
  "privateEndpoints": null,
  "privateLinkServiceNetworkPolicies": "Enabled",
  "provisioningState": "Succeeded",
  "purpose": null,
  "resourceGroup": "pro-nabehiro-001",
  "resourceNavigationLinks": null,
  "routeTable": null,
  "serviceAssociationLinks": null,
  "serviceEndpointPolicies": null,
  "serviceEndpoints": null,
  "type": "Microsoft.Network/virtualNetworks/subnets"
}
# Create a  subnet-nabehiro-002
{
  "addressPrefix": "10.0.2.0/24",
  "addressPrefixes": null,
  "applicationGatewayIpConfigurations": null,
  "delegations": [],
  "etag": "W/\"8eb1725e-cc4f-4cda-876d-fda792b21e89\"",
  "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/virtualNetworks/vnet-nabehiro-001/subnets/subnet-nabehiro-002",
  "ipAllocations": null,
  "ipConfigurationProfiles": null,
  "ipConfigurations": null,
  "name": "subnet-nabehiro-002",
  "natGateway": null,
  "networkSecurityGroup": null,
  "privateEndpointNetworkPolicies": "Enabled",
  "privateEndpoints": null,
  "privateLinkServiceNetworkPolicies": "Enabled",
  "provisioningState": "Succeeded",
  "purpose": null,
  "resourceGroup": "pro-nabehiro-001",
  "resourceNavigationLinks": null,
  "routeTable": null,
  "serviceAssociationLinks": null,
  "serviceEndpointPolicies": null,
  "serviceEndpoints": null,
  "type": "Microsoft.Network/virtualNetworks/subnets"
}
# Create a ssh-key
# Create a  ssh-nabehiro-001
No public key is provided. A key pair is being generated for you.
Private key is saved to "C:\Users\watya\.ssh\1676035899_7546787".
Public key is saved to "C:\Users\watya\.ssh\1676035899_7546787.pub".
{
  "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/PRO-NABEHIRO-001/providers/Microsoft.Compute/sshPublicKeys/ssh-nabehiro-001",
  "location": "japaneast",
  "name": "ssh-nabehiro-001",
  "publicKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC8GIpisYDMvkfxbghmqv4J4ht/+BwHwnLsFEIneso00DaCIJVbj5yrkDY63NEyWHbtVs+Irhrd5s2vYYKuc9PfL6YW+k9kEbBYQOarN5KrhuHRrRVFodBmAIx3neQfr1xOdYBKUkIoqMP3p+2jP5ARS2ll/SF9BdVZvf5ee0IqjrAEi5iJwcT/JRVIetqCRMAi5UcOp0w5W8ouxOmuGiHpd5OqJvBHFsF6FV9clm3DY0Jcc2Tj+5WHnPqkG784BF6u5UiEDMuGTnWfPq1UXGdFnWrHLsoPhII3FeQFRAl2xRVqOCFXeYajvh6R036MsO9oTEA/pGwt27usLTn0j3riwLXD9w+LlTzY/H5xPtLb5w0izp1BYuoZUEakrIWtmYFbqlhIeCcOZhOUP4P/hU8/XpSnvAtDylCne1c1ZiS4e8V1Ab7Dt4BUEhvLtMXDJ1NP0p3KTE2S07puZXxEBYUX4fcGQU/EIeyAQ/YJkJ2Ew/tYnMmcsE2xKtULqh2tM/0= generated-by-azure",
  "resourceGroup": "PRO-NABEHIRO-001",
  "tags": null,
  "type": null
}
# Create virtual machines
# Create  nbfr151v
It is recommended to use parameter "--public-ip-sku Standard" to create new VM with Standard public IP. Please note that the default public IP used for VM creation will be changed from Basic to Standard in the future.
{
  "fqdns": "",
  "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Compute/virtualMachines/nbfr151v",
  "location": "japaneast",
  "macAddress": "60-45-BD-62-97-0B",
  "powerState": "VM running",
  "privateIpAddress": "10.0.1.4",
  "publicIpAddress": "40.115.215.210",
  "resourceGroup": "pro-nabehiro-001",
  "zones": ""
}
# Create  nbfr152v
It is recommended to use parameter "--public-ip-sku Standard" to create new VM with Standard public IP. Please note that the default public IP used for VM creation will be changed from Basic to Standard in the future.
{
  "fqdns": "",
  "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Compute/virtualMachines/nbfr152v",
  "location": "japaneast",
  "macAddress": "60-45-BD-65-D3-3A",
  "powerState": "VM running",
  "privateIpAddress": "10.0.1.5",
  "publicIpAddress": "20.222.59.99",
  "resourceGroup": "pro-nabehiro-001",
  "zones": ""
}
# Create  nbfr153v
It is recommended to use parameter "--public-ip-sku Standard" to create new VM with Standard public IP. Please note that the default public IP used for VM creation will be changed from Basic to Standard in the future.
{
  "fqdns": "",
  "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Compute/virtualMachines/nbfr153v",
  "location": "japaneast",
  "macAddress": "60-45-BD-66-C4-64",
  "powerState": "VM running",
  "privateIpAddress": "10.0.1.6",
  "publicIpAddress": "20.222.61.24",
  "resourceGroup": "pro-nabehiro-001",
  "zones": ""
}
# Get the virtual machine IP addresses
add  nbfr151v ipaddress
add  nbfr152v ipaddress
add  nbfr153v ipaddress
show ip-lists: 10.0.1.4 10.0.1.5 10.0.1.6
# Create an application gateway
Create  nbfr151x
{
  "applicationGateway": {
    "authenticationCertificates": [],
    "backendAddressPools": [
      {
        "etag": "W/\"cfc9bb12-e19d-4bd2-a711-d73074a298d3\"",
        "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/backendAddressPools/appGatewayBackendPool",
        "name": "appGatewayBackendPool",
        "properties": {
          "backendAddresses": [],
          "provisioningState": "Succeeded",
          "requestRoutingRules": [
            {
              "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/requestRoutingRules/rule1",
              "resourceGroup": "pro-nabehiro-001"
            }
          ]
        },
        "resourceGroup": "pro-nabehiro-001",
        "type": "Microsoft.Network/applicationGateways/backendAddressPools"
      }
    ],
    "backendHttpSettingsCollection": [
      {
        "etag": "W/\"cfc9bb12-e19d-4bd2-a711-d73074a298d3\"",
        "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/backendHttpSettingsCollection/appGatewayBackendHttpSettings",
        "name": "appGatewayBackendHttpSettings",
        "properties": {
          "connectionDraining": {
            "drainTimeoutInSec": 1,
            "enabled": false
          },
          "cookieBasedAffinity": "Disabled",
          "pickHostNameFromBackendAddress": false,
          "port": 80,
          "protocol": "Http",
          "provisioningState": "Succeeded",
          "requestRoutingRules": [
            {
              "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/requestRoutingRules/rule1",
              "resourceGroup": "pro-nabehiro-001"
            }
          ],
          "requestTimeout": 30
        },
        "resourceGroup": "pro-nabehiro-001",
        "type": "Microsoft.Network/applicationGateways/backendHttpSettingsCollection"
      }
    ],
    "frontendIPConfigurations": [
      {
        "etag": "W/\"cfc9bb12-e19d-4bd2-a711-d73074a298d3\"",
        "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/frontendIPConfigurations/appGatewayPrivateFrontendIP",
        "name": "appGatewayPrivateFrontendIP",
        "properties": {
          "httpListeners": [
            {
              "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/httpListeners/appGatewayHttpListener",
              "resourceGroup": "pro-nabehiro-001"
            }
          ],
          "privateIPAddress": "10.0.2.6",
          "privateIPAllocationMethod": "Dynamic",
          "provisioningState": "Succeeded",
          "subnet": {
            "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/virtualNetworks/vnet-nabehiro-001/subnets/subnet-nabehiro-002",
            "resourceGroup": "pro-nabehiro-001"
          }
        },
        "resourceGroup": "pro-nabehiro-001",
        "type": "Microsoft.Network/applicationGateways/frontendIPConfigurations"
      }
    ],
    "frontendPorts": [
      {
        "etag": "W/\"cfc9bb12-e19d-4bd2-a711-d73074a298d3\"",
        "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/frontendPorts/appGatewayFrontendPort",
        "name": "appGatewayFrontendPort",
        "properties": {
          "httpListeners": [
            {
              "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/httpListeners/appGatewayHttpListener",
              "resourceGroup": "pro-nabehiro-001"
            }
          ],
          "port": 80,
          "provisioningState": "Succeeded"
        },
        "resourceGroup": "pro-nabehiro-001",
        "type": "Microsoft.Network/applicationGateways/frontendPorts"
      }
    ],
    "gatewayIPConfigurations": [
      {
        "etag": "W/\"cfc9bb12-e19d-4bd2-a711-d73074a298d3\"",
        "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/gatewayIPConfigurations/appGatewayPrivateFrontendIP",
        "name": "appGatewayPrivateFrontendIP",
        "properties": {
          "provisioningState": "Succeeded",
          "subnet": {
            "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/virtualNetworks/vnet-nabehiro-001/subnets/subnet-nabehiro-002",
            "resourceGroup": "pro-nabehiro-001"
          }
        },
        "resourceGroup": "pro-nabehiro-001",
        "type": "Microsoft.Network/applicationGateways/gatewayIPConfigurations"
      }
    ],
    "httpListeners": [
      {
        "etag": "W/\"cfc9bb12-e19d-4bd2-a711-d73074a298d3\"",
        "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/httpListeners/appGatewayHttpListener",
        "name": "appGatewayHttpListener",
        "properties": {
          "frontendIPConfiguration": {
            "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/frontendIPConfigurations/appGatewayPrivateFrontendIP",
            "resourceGroup": "pro-nabehiro-001"
          },
          "frontendPort": {
            "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/frontendPorts/appGatewayFrontendPort",
            "resourceGroup": "pro-nabehiro-001"
          },
          "hostNames": [],
          "protocol": "Http",
          "provisioningState": "Succeeded",
          "requestRoutingRules": [
            {
              "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/requestRoutingRules/rule1",
              "resourceGroup": "pro-nabehiro-001"
            }
          ],
          "requireServerNameIndication": false
        },
        "resourceGroup": "pro-nabehiro-001",
        "type": "Microsoft.Network/applicationGateways/httpListeners"
      }
    ],
    "operationalState": "Running",
    "probes": [],
    "provisioningState": "Succeeded",
    "redirectConfigurations": [],
    "requestRoutingRules": [
      {
        "etag": "W/\"cfc9bb12-e19d-4bd2-a711-d73074a298d3\"",
        "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/requestRoutingRules/rule1",
        "name": "rule1",
        "properties": {
          "backendAddressPool": {
            "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/backendAddressPools/appGatewayBackendPool",
            "resourceGroup": "pro-nabehiro-001"
          },
          "backendHttpSettings": {
            "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/backendHttpSettingsCollection/appGatewayBackendHttpSettings",
            "resourceGroup": "pro-nabehiro-001"
          },
          "httpListener": {
            "id": "/subscriptions/2a7080ea-1981-457b-aac9-902be7c5f00d/resourceGroups/pro-nabehiro-001/providers/Microsoft.Network/applicationGateways/nbfr151x/httpListeners/appGatewayHttpListener",
            "resourceGroup": "pro-nabehiro-001"
          },
          "provisioningState": "Succeeded",
          "ruleType": "Basic"
        },
        "resourceGroup": "pro-nabehiro-001",
        "type": "Microsoft.Network/applicationGateways/requestRoutingRules"
      }
    ],
    "resourceGuid": "4615d0f3-2381-4f72-9bdb-69aff94da995",
    "rewriteRuleSets": [],
    "sku": {
      "capacity": 2,
      "name": "Standard_Small",
      "tier": "Standard"
    },
    "sslCertificates": [],
    "urlPathMaps": []
  }
}
# Create a address pool
```