Manage a Firewall and WAF with Terraform
Declare a Firewall, its rules and their order, and a WAF rule set with the Azion Terraform Provider, then attach the Firewall to a Workload.
You can declare a Firewall, its rules, the order of those rules, and a Web Application Firewall (WAF) rule set with the Azion Terraform Provider. A Firewall inspects the requests received by a Workload once that Workload’s deployment sets that Firewall. The examples deny every path that starts with /admin and apply the WAF rule set in blocking mode to every other path.
Prerequisites
- Terraform 1.17.2 or later. To install it, refer to Install Terraform.
- A personal token in the
AZION_API_TOKENenvironment variable. To set it, refer to Provide your personal token. - A Terraform configuration that manages a Workload, its Application, and its
azion_workload_deployment. To write one, refer to Manage an Application with Terraform. The examples use the resource names of that page:hellofor the Workload and the Application, andrun_functionfor its rule.
Declare the Firewall, its rules, and the WAF rule set
The Firewall side of the configuration declares five resources of four types:
azion_wafis the WAF rule set: the threat families it scores and the sensitivity of each. The provider’sazion_waf_rule_setresource manages exceptions, not this rule set.azion_firewall_main_settingis the Firewall, with WAF turned on in itsmodules.- Each
azion_firewall_rule_engineis one rule of the Firewall.Deny /adminanswers403to every path that starts with/admin.Apply storefront-wafruns the WAF rule set in blocking mode on every other path. azion_firewall_rule_engine_orderlists the rule IDs in the order the Firewall evaluates them.
In the directory of your configuration, create a firewall.tf file:
engine_version, engine_settings.type, and rulesets each accept one value, the one in the example. The example declares two of the eight threat families in thresholds, and the rule set stores only the families it declares. For every threat and sensitivity, refer to Rule sets.
$${request_uri} writes the literal ${request_uri} variable: the double $ stops Terraform from reading it as an interpolation.
To change the order later, reorder the IDs in the order list and run terraform apply. The plan changes one resource.
The configuration declares a Firewall that evaluates Deny /admin first and Apply storefront-waf second.
Attach the Firewall to the Workload
A Firewall inspects the requests received by a Workload once that Workload’s deployment sets that Firewall, in the firewall attribute next to application.
To attach the Firewall:
-
Change the
azion_workload_deploymentresource of your configuration to match this block:HCLThe block sets
firewallinstrategyand adds the rule order todepends_on. Terraform then changes the deployment only after the rules exist in their order. -
Check the configuration:
-
Apply the configuration, and enter
yeswhen Terraform asks. The plan creates the five Firewall resources and changes the deployment in place:Text
The Workload’s deployment sets the Application and the Firewall. The Firewall holds two rules, in the order Deny /admin, Apply storefront-waf.
Check the rules
Deny /admin answers 403 to every path that starts with /admin. The WAF rule set in blocking mode answers 400 with a Bad Request page when a threat family’s score reaches its threshold. A request that no rule stops reaches the Application.
To check the rules, send three requests to the domain of your Workload. Replace <your-workload-domain> with that domain:
The Application answers /, and Deny /admin refuses /admin. The WAF rule set blocks the request whose id query string carries a SQL injection pattern.
A Firewall added to a Workload that already serves requests can take about 10 minutes to take effect. Until then, /admin and the SQL injection request can alternate between 200 from the Application and the rules’ codes. Repeat the requests every minute until each one returns its code three times in a row.