Skip to content

Commit 11fbfc6

Browse files
committed
updates1
1 parent 6c68853 commit 11fbfc6

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
title: 'Quickstart: Use Terraform to configure Azure Web Application Firewall on Azure Application Gateway'
3+
description: In this quickstart, you use Terraform to create an Azure Application Gateway with an Azure Web Application Firewall (WAF) policy. A virtual network with a subnet, a static public IP address, an Azure Web Application Firewall policy with custom rules, and Azure Application Gateway with autoscaling work together to block specific IP addresses.
4+
ms.topic: quickstart
5+
ms.date: 03/20/2025
6+
ms.custom: devx-track-terraform
7+
ms.service: azure-application-gateway
8+
author: halkazwini
9+
ms.author: halkazwini
10+
#customer intent: As a Terraform user, I want to learn how to configure Azure Web Application Firewall on Azure Application Gateway.
11+
content_well_notification:
12+
- AI-contribution
13+
---
14+
15+
# 'Quickstart: Use Terraform to configure Azure Web Application Firewall on Azure Application Gateway'
16+
17+
In this quickstart, you use Terraform to create an Azure Application Gateway with an Azure Web Application Firewall (WAF) policy. A key component of creating scalable, reliable, and secure web front ends in Azure, Application Gateway is a web traffic load balancer that helps you to manage traffic to your web applications. Application Gateway bases how it routes traffic on factors that include round-robin, cookie-based sessions, and more. In addition to an Application Gateway, this code also creates a resource group, virtual network, subnet within the virtual network, public IP address, and a WAF policy with custom rules to block traffic from a specific IP address.
18+
19+
[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)]
20+
21+
> [!div class="checklist"]
22+
> * Define the IP address that the WAF custom rule should block.
23+
> * Create an Azure resource group with a unique name.
24+
> * Establish a virtual network with a specific name and address.
25+
> * Generate a random name for the subnet, and create a subnet in the virtual network.
26+
> * Generate a public IP address.
27+
> * Create a WAF policy.
28+
> * Configure settings and define managed rules for the WAF policy.
29+
> * Create a custom rule to block traffic from a specific IP address.
30+
> * Set up the Application Gateway.
31+
> * Configure the SKU and capacity of the Application Gateway.
32+
> * Enable autoscaling for the Application Gateway.
33+
> * Configure the gateway's IP settings.
34+
> * Set up the front-end IP configuration, and define the front-end port.
35+
> * Define the back-end address pool with IP addresses, and configure back-end HTTP settings.
36+
> * Define the HTTP listener.
37+
> * Define the request routing rule.
38+
> * Associate the WAF policy with the Application Gateway.
39+
> * Output the resource group name, public IP address, Application Gateway ID, WAF policy ID, and Application Gateway.
40+
41+
## Prerequisites
42+
43+
- Create an Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
44+
45+
- [Install and configure Terraform.](/azure/developer/terraform/quickstart-configure)
46+
47+
## Implement the Terraform code
48+
49+
> [!NOTE]
50+
> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-web-application-firewall). 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-web-application-firewall/TestRecord.md).
51+
>
52+
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform).
53+
54+
1. Create a directory in which to test and run the sample Terraform code, and make it the current directory.
55+
56+
1. Create a file named `main.tf`, and insert the following code:
57+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-web-application-firewall/main.tf":::
58+
59+
1. Create a file named `outputs.tf`, and insert the following code:
60+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-web-application-firewall/outputs.tf":::
61+
62+
1. Create a file named `providers.tf`, and insert the following code:
63+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-web-application-firewall/providers.tf":::
64+
65+
1. Create a file named `variables.tf`, and insert the following code:
66+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-web-application-firewall/variables.tf":::
67+
68+
## Initialize Terraform
69+
70+
[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)]
71+
72+
## Create a Terraform execution plan
73+
74+
[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)]
75+
76+
## Apply a Terraform execution plan
77+
78+
[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)]
79+
80+
## Verify the results
81+
82+
### [Azure CLI](#tab/azure-cli)
83+
84+
1. Get the Azure resource group name.
85+
86+
```console
87+
resource_group_name=$(terraform output -raw resource_group_name)
88+
```
89+
90+
1. Get the public IP address.
91+
92+
```console
93+
public_ip_address=$(terraform output -raw public_ip_address)
94+
```
95+
96+
1. Get the WAF policy ID.
97+
98+
```console
99+
web_application_firewall_policy_id=$(terraform output -raw web_application_firewall_policy_id)
100+
```
101+
102+
1. Get the Application Gateway ID.
103+
104+
```console
105+
application_gateway_id=$(terraform output -raw application_gateway_id)
106+
```
107+
108+
1. Run `az network application-gateway show` to view the Application Gateway.
109+
110+
```azurecli
111+
az appservice ase show --name $application_gateway_name --resource-group $resource_group_name
112+
```
113+
114+
### [Azure PowerShell](#tab/azure-powershell)
115+
116+
1. Get the Azure resource group name.
117+
118+
```console
119+
$resource_group_name=$(terraform output -raw resource_group_name)
120+
```
121+
122+
1. Get the public IP address.
123+
124+
```console
125+
$public_ip_address=$(terraform output -public_ip_address)
126+
```
127+
128+
1. Get the WAF policy ID.
129+
130+
```console
131+
$web_application_firewall_policy_id=$(terraform output -web_application_firewall_policy_id)
132+
```
133+
134+
1. Get the Application Gateway ID.
135+
136+
```console
137+
$application_gateway_id=$(terraform output -application_gateway_id)
138+
```
139+
140+
1. Run `Get-AzAppServiceEnvironment` to view the Application Gateway.
141+
142+
```azurepowershell
143+
Get-AzApplicationGateway -Name $application_gateway_name -ResourceGroupName $resource_group_name
144+
```
145+
146+
---
147+
148+
## Clean up resources
149+
150+
[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)]
151+
152+
## Troubleshoot Terraform on Azure
153+
154+
[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot).
155+
156+
## Next steps
157+
158+
> [!div class="nextstepaction"]
159+
> [See more articles about Application Gateway.](/search/?terms=Azure%20application%20gateway%20and%20terraform)

0 commit comments

Comments
 (0)