|
| 1 | +--- |
| 2 | +title: Deploy an Azure Application Gateway v2 using Terraform to direct web traffic |
| 3 | +description: Learn how to use Terraform to create an Azure Application Gateway that directs web traffic to virtual machines in a backend pool. |
| 4 | +services: application-gateway |
| 5 | +author: greg-lindsay |
| 6 | +ms.service: application-gateway |
| 7 | +ms.topic: how-to |
| 8 | +ms.author: greglin |
| 9 | +ms.date: 09/25/2023 |
| 10 | +ms.custom: devx-track-terraform |
| 11 | +content_well_notification: |
| 12 | + - AI-contribution |
| 13 | +--- |
| 14 | + |
| 15 | +# Deploy an Azure Application Gateway v2 using Terraform to direct web traffic |
| 16 | + |
| 17 | +In this article, you use Terraform to deploy an Azure Application Gateway v2 and two Windows Server 2019 Datacenter test servers for the backend pool. |
| 18 | + |
| 19 | +> [!div class="checklist"] |
| 20 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) |
| 21 | +> * Create an Azure Virtual Network using [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) |
| 22 | +> * Create an Azure subnet using [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) |
| 23 | +> * Create an Azure public IP using [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) |
| 24 | +> * Create an Azure Application Gateway using [azurerm_application_gateway](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/application_gateway) |
| 25 | +> * Create an Azure network interface using [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) |
| 26 | +> * Create an Azure network interface application gateway backend address pool association using [azurerm_network_interface_application_gateway_backend_address_pool_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_application_gateway_backend_address_pool_association) |
| 27 | +> * Create an Azure Windows Virtual Machine using [azurerm_windows_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/windows_virtual_machine) |
| 28 | +> * Create an Azure Virtual Machine Extension using [azurerm_virtual_machine_extension](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension) |
| 29 | +
|
| 30 | +## Prerequisites |
| 31 | + |
| 32 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 33 | + |
| 34 | +## Implement the Terraform code |
| 35 | + |
| 36 | +> [!NOTE] |
| 37 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-application-gateway). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-application-gateway/TestRecord.md). |
| 38 | +> |
| 39 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 40 | +
|
| 41 | +1. Create a directory in which to test the sample Terraform code and make it the current directory. |
| 42 | + |
| 43 | +1. Create a file named `providers.tf` and insert the following code: |
| 44 | + |
| 45 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-application-gateway/main.tf"::: |
| 46 | + |
| 47 | +1. Create a file named `main.tf` and insert the following code: |
| 48 | + |
| 49 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-application-gateway/main.tf"::: |
| 50 | + |
| 51 | +1. Create a file named `variables.tf` and insert the following code: |
| 52 | + |
| 53 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-application-gateway/variables.tf"::: |
| 54 | + |
| 55 | +1. Create a file named `outputs.tf` and insert the following code: |
| 56 | + |
| 57 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-application-gateway/outputs.tf"::: |
| 58 | + |
| 59 | +## Initialize Terraform |
| 60 | + |
| 61 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 62 | + |
| 63 | +## Create a Terraform execution plan |
| 64 | + |
| 65 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 66 | + |
| 67 | +## Apply a Terraform execution plan |
| 68 | + |
| 69 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 70 | + |
| 71 | +## Verify the results |
| 72 | + |
| 73 | +1. When apply the execution plan, Terraform will output the frontend public IP address. If you've cleared the screen, you can retrieve that value with the following Terraform command: |
| 74 | + |
| 75 | + ```hcl |
| 76 | + echo $(terraform output -raw gateway_frontend_ip) |
| 77 | + ``` |
| 78 | +
|
| 79 | +1. Paste the public IP address into the address bar of your web browser. Refresh the browser to see the name of the virtual machine. A valid response verifies the application gateway is successfully created and can connect with the backend. |
| 80 | +
|
| 81 | +## Clean up resources |
| 82 | +
|
| 83 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 84 | +
|
| 85 | +## Troubleshoot Terraform on Azure |
| 86 | +
|
| 87 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 88 | +
|
| 89 | +## Next steps |
| 90 | +
|
| 91 | +> [!div class="nextstepaction"] |
| 92 | +> [Learn more about using Application Gateway](/azure/application-gateway/overview) |
0 commit comments