|
| 1 | +--- |
| 2 | +title: Create an application gateway with a certificate - Azure CLI | Microsoft Docs |
| 3 | +description: Learn how to create an application gateway and add a certificate for SSL termination using the Azure CLI. |
| 4 | +services: application-gateway |
| 5 | +author: davidmu1 |
| 6 | +manager: timlt |
| 7 | +editor: tysonn |
| 8 | + |
| 9 | +ms.service: application-gateway |
| 10 | +ms.topic: article |
| 11 | +ms.workload: infrastructure-services |
| 12 | +ms.date: 01/23/2018 |
| 13 | +ms.author: davidmu |
| 14 | + |
| 15 | +--- |
| 16 | +# Create an application gateway with HTTP to HTTPS redirection using the Azure CLI |
| 17 | + |
| 18 | +You can use the Azure CLI to create an [application gateway](application-gateway-introduction.md) with a certificate for SSL termination. A routing rule is used to redirect HTTP traffic to the HTTPS port in your application gateway. In this example, you also create a [virtual machine scale set](../virtual-machine-scale-sets/virtual-machine-scale-sets-overview.md) for the backend pool of the application gateway that contains two virtual machine instances. |
| 19 | + |
| 20 | +In this article, you learn how to: |
| 21 | + |
| 22 | +> [!div class="checklist"] |
| 23 | +> * Create a self-signed certificate |
| 24 | +> * Set up a network |
| 25 | +> * Create an application gateway with the certificate |
| 26 | +> * Add a listener and redirection rule |
| 27 | +> * Create a virtual machine scale set with the default backend pool |
| 28 | +
|
| 29 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 30 | + |
| 31 | +[!INCLUDE [cloud-shell-try-it.md](../../includes/cloud-shell-try-it.md)] |
| 32 | + |
| 33 | +If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.4 or later. To find the version, run `az --version`. If you need to install or upgrade, see [Install Azure CLI 2.0](/cli/azure/install-azure-cli). |
| 34 | + |
| 35 | +## Create a self-signed certificate |
| 36 | + |
| 37 | +For production use, you should import a valid certificate signed by a trusted provider. For this tutorial, you create a self-signed certificate and pfx file using the openssl command. |
| 38 | + |
| 39 | +```azurecli-interactive |
| 40 | +openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out appgwcert.crt |
| 41 | +``` |
| 42 | + |
| 43 | +Enter values that make sense for your certificate. You can accept the default values. |
| 44 | + |
| 45 | +```azurecli-interactive |
| 46 | +openssl pkcs12 -export -out appgwcert.pfx -inkey privateKey.key -in appgwcert.crt |
| 47 | +``` |
| 48 | + |
| 49 | +Enter the password for the certificate. In this example, *Azure123456!* is being used. |
| 50 | + |
| 51 | +## Create a resource group |
| 52 | + |
| 53 | +A resource group is a logical container into which Azure resources are deployed and managed. Create a resource group using [az group create](/cli/azure/group#create). |
| 54 | + |
| 55 | +The following example creates a resource group named *myResourceGroupAG* in the *eastus* location. |
| 56 | + |
| 57 | +```azurecli-interactive |
| 58 | +az group create --name myResourceGroupAG --location eastus |
| 59 | +``` |
| 60 | + |
| 61 | +## Create network resources |
| 62 | + |
| 63 | +Create the virtual network named *myVNet* and the subnet named *myAGSubnet* using [az network vnet create](/cli/azure/network/vnet#az_net). You can then add the subnet named *myBackendSubnet* that's needed by the backend servers using [az network vnet subnet create](/cli/azure/network/vnet/subnet#az_network_vnet_subnet_create). Create the public IP address named *myAGPublicIPAddress* using [az network public-ip create](/cli/azure/public-ip#az_network_public_ip_create). |
| 64 | + |
| 65 | +```azurecli-interactive |
| 66 | +az network vnet create \ |
| 67 | + --name myVNet \ |
| 68 | + --resource-group myResourceGroupAG \ |
| 69 | + --location eastus \ |
| 70 | + --address-prefix 10.0.0.0/16 \ |
| 71 | + --subnet-name myAGSubnet \ |
| 72 | + --subnet-prefix 10.0.1.0/24 |
| 73 | +az network vnet subnet create \ |
| 74 | + --name myBackendSubnet \ |
| 75 | + --resource-group myResourceGroupAG \ |
| 76 | + --vnet-name myVNet \ |
| 77 | + --address-prefix 10.0.2.0/24 |
| 78 | +az network public-ip create \ |
| 79 | + --resource-group myResourceGroupAG \ |
| 80 | + --name myAGPublicIPAddress |
| 81 | +``` |
| 82 | + |
| 83 | +## Create the application gateway |
| 84 | + |
| 85 | +You can use [az network application-gateway create](/cli/azure/network/application-gateway#az_network_application_gateway_create) to create the application gateway named *myAppGateway*. When you create an application gateway using the Azure CLI, you specify configuration information, such as capacity, sku, and HTTP settings. |
| 86 | + |
| 87 | +The application gateway is assigned to *myAGSubnet* and *myAGPublicIPAddress* that you previously created. In this example, you associate the certificate that you created and its password when you create the application gateway. |
| 88 | + |
| 89 | +```azurecli-interactive |
| 90 | +az network application-gateway create \ |
| 91 | + --name myAppGateway \ |
| 92 | + --location eastus \ |
| 93 | + --resource-group myResourceGroupAG \ |
| 94 | + --vnet-name myVNet \ |
| 95 | + --subnet myAGsubnet \ |
| 96 | + --capacity 2 \ |
| 97 | + --sku Standard_Medium \ |
| 98 | + --http-settings-cookie-based-affinity Disabled \ |
| 99 | + --frontend-port 443 \ |
| 100 | + --http-settings-port 80 \ |
| 101 | + --http-settings-protocol Http \ |
| 102 | + --public-ip-address myAGPublicIPAddress \ |
| 103 | + --cert-file appgwcert.pfx \ |
| 104 | + --cert-password "Azure123456!" |
| 105 | +
|
| 106 | +``` |
| 107 | + |
| 108 | + It may take several minutes for the application gateway to be created. After the application gateway is created, you can see these new features of it: |
| 109 | + |
| 110 | +- *appGatewayBackendPool* - An application gateway must have at least one backend address pool. |
| 111 | +- *appGatewayBackendHttpSettings* - Specifies that port 80 and an HTTP protocol is used for communication. |
| 112 | +- *appGatewayHttpListener* - The default listener associated with *appGatewayBackendPool*. |
| 113 | +- *appGatewayFrontendIP* - Assigns *myAGPublicIPAddress* to *appGatewayHttpListener*. |
| 114 | +- *rule1* - The default routing rule that is associated with *appGatewayHttpListener*. |
| 115 | + |
| 116 | +## Add a listener and redirection rule |
| 117 | + |
| 118 | +### Add the HTTP port |
| 119 | + |
| 120 | +You can use [az network application-gateway frontend-port create](/cli/azure/network/application-gateway/frontend-port#az_network_application_gateway_frontend_port_create) to add the HTTP port to the application gateway. |
| 121 | + |
| 122 | +```azurecli-interactive |
| 123 | +az network application-gateway frontend-port create \ |
| 124 | + --port 80 \ |
| 125 | + --gateway-name myAppGateway \ |
| 126 | + --resource-group myResourceGroupAG \ |
| 127 | + --name httpPort |
| 128 | +``` |
| 129 | + |
| 130 | +### Add the HTTP listener |
| 131 | + |
| 132 | +You can use [az network application-gateway http-listener create](/cli/azure/network/application-gateway/http-listener#az_network_application_gateway_http_listener_create) to add the listener named *myListener* to the application gateway. |
| 133 | + |
| 134 | +```azurecli-interactive |
| 135 | +az network application-gateway http-listener create \ |
| 136 | + --name myListener \ |
| 137 | + --frontend-ip appGatewayFrontendIP \ |
| 138 | + --frontend-port httpPort \ |
| 139 | + --resource-group myResourceGroupAG \ |
| 140 | + --gateway-name myAppGateway |
| 141 | +``` |
| 142 | + |
| 143 | +### Add the redirection configuration |
| 144 | + |
| 145 | +Add the HTTP to HTTPS redirection configuration to the application gateway using [az network application-gateway redirect-config create](/cli/azure/network/application-gateway/redirect-config#az_network_application_gateway_redirect_config_create). |
| 146 | + |
| 147 | +```azurecli-interactive |
| 148 | +az network application-gateway redirect-config create \ |
| 149 | + --name httpToHttps \ |
| 150 | + --gateway-name myAppGateway \ |
| 151 | + --resource-group myResourceGroupAG \ |
| 152 | + --type Permanent \ |
| 153 | + --target-listener appGatewayHttpListener \ |
| 154 | + --include-path true \ |
| 155 | + --include-query-string true |
| 156 | +``` |
| 157 | + |
| 158 | +### Add the routing rule |
| 159 | + |
| 160 | +Add the routing rule named *rule2* with the redirection configuration to the application gateway using [az network application-gateway rule create](/cli/azure/network/application-gateway/rule#az_network_application_gateway_rule_create). |
| 161 | + |
| 162 | +```azurecli-interactive |
| 163 | +az network application-gateway rule create \ |
| 164 | + --gateway-name myAppGateway \ |
| 165 | + --name rule2 \ |
| 166 | + --resource-group myResourceGroupAG \ |
| 167 | + --http-listener myListener \ |
| 168 | + --rule-type Basic \ |
| 169 | + --redirect-config httpToHttps |
| 170 | +``` |
| 171 | + |
| 172 | +## Create a virtual machine scale set |
| 173 | + |
| 174 | +In this example, you create a virtual machine scale set named *myvmss* that provides servers for the backend pool in the application gateway. The virtual machines in the scale set are associated with *myBackendSubnet* and *appGatewayBackendPool*. To create the scale set, you can use [az vmss create](/cli/azure/vmss#az_vmss_create). |
| 175 | + |
| 176 | +```azurecli-interactive |
| 177 | +az vmss create \ |
| 178 | + --name myvmss \ |
| 179 | + --resource-group myResourceGroupAG \ |
| 180 | + --image UbuntuLTS \ |
| 181 | + --admin-username azureuser \ |
| 182 | + --admin-password Azure123456! \ |
| 183 | + --instance-count 2 \ |
| 184 | + --vnet-name myVNet \ |
| 185 | + --subnet myBackendSubnet \ |
| 186 | + --vm-sku Standard_DS2 \ |
| 187 | + --upgrade-policy-mode Automatic \ |
| 188 | + --app-gateway myAppGateway \ |
| 189 | + --backend-pool-name appGatewayBackendPool |
| 190 | +``` |
| 191 | + |
| 192 | +### Install NGINX |
| 193 | + |
| 194 | +You can use any editor you wish to create the file in the Cloud Shell. Enter `sensible-editor cloudConfig.json` to see a list of available editors to create the file. In your current shell, create a file named customConfig.json and paste the following configuration: |
| 195 | + |
| 196 | +```json |
| 197 | +{ |
| 198 | + "fileUris": ["https://raw.githubusercontent.com/davidmu1/samplescripts/master/install_nginx.sh"], |
| 199 | + "commandToExecute": "./install_nginx.sh" |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +Run this command in the shell window: |
| 204 | + |
| 205 | +```azurecli-interactive |
| 206 | +az vmss extension set \ |
| 207 | + --publisher Microsoft.Azure.Extensions \ |
| 208 | + --version 2.0 \ |
| 209 | + --name CustomScript \ |
| 210 | + --resource-group myResourceGroupAG \ |
| 211 | + --vmss-name myvmss \ |
| 212 | + --settings @cloudConfig.json |
| 213 | +``` |
| 214 | + |
| 215 | +## Test the application gateway |
| 216 | + |
| 217 | +To get the public IP address of the application gateway, you can use [az network public-ip show](/cli/azure/network/public-ip#az_network_public_ip_show). Copy the public IP address, and then paste it into the address bar of your browser. |
| 218 | + |
| 219 | +```azurepowershell-interactive |
| 220 | +az network public-ip show \ |
| 221 | + --resource-group myResourceGroupAG \ |
| 222 | + --name myAGPublicIPAddress \ |
| 223 | + --query [ipAddress] \ |
| 224 | + --output tsv |
| 225 | +``` |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | +To accept the security warning if you used a self-signed certificate, select **Details** and then **Go on to the webpage**. Your secured NGINX site is then displayed as in the following example: |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +## Next steps |
| 234 | + |
| 235 | +In this tutorial, you learned how to: |
| 236 | + |
| 237 | +> [!div class="checklist"] |
| 238 | +> * Create a self-signed certificate |
| 239 | +> * Set up a network |
| 240 | +> * Create an application gateway with the certificate |
| 241 | +> * Add a listener and redirection rule |
| 242 | +> * Create a virtual machine scale set with the default backend pool |
| 243 | +
|
| 244 | +To learn more about application gateways and their associated resources, continue to the how-to articles. |
0 commit comments