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