Handling AWS WAF Capacity Limits with Terraform
Tags: terraform, aws, webacl, waf
Published: 2024-06-09 18:28:02

When managing AWS WAF resources using Terraform, one common issue is exceeding the Web ACL Capacity Units (WCUs) limit, which can lead to deployment failures. This blog post will guide you through understanding WCUs, checking capacity using AWS CLI, and integrating this process into your Terraform workflow to ensure smooth deployments.

#### Understanding WCUs in AWS WAF

AWS WAF uses Web ACL Capacity Units (WCUs) to measure the resources required to run rules, rule groups, and Web ACLs. Each rule type has a different capacity requirement based on its complexity and processing needs. The capacity limits help AWS manage the performance and cost of running WAF rules efficiently.

- **Simple Rules**: Require fewer WCUs (e.g., size constraint rules).
- **Complex Rules**: Require more WCUs (e.g., regex pattern sets).

For detailed WCU calculations, refer to the [AWS WAF documentation](https://docs.aws.amazon.com/waf/latest/developerguide/aws-waf-capacity-units.html) [oai_citation:1,AWS WAF web ACL capacity units (WCUs) - AWS WAF, AWS Firewall Manager, and AWS Shield Advanced](https://docs.aws.amazon.com/waf/latest/developerguide/aws-waf-capacity-units.html).

#### Common Error

When you exceed the WCU limits, you may encounter the following error:
```
WAFInvalidParameterException: Error reason: You exceeded the capacity limit for a rule group or web ACL.
```

#### Checking WCU Requirements with AWS CLI

Terraform lacks built-in functionality to check WCUs before applying configurations. However, AWS CLI provides a `check-capacity` command to verify the WCU requirements. Here’s how you can use it:


1. **Define WAF Rules in JSON**:
   Create a JSON file (`rules.json`) with the rules you want to include:
   ```json
   {
     "Rules": [
       {
         "Name": "Rule1",
         "Priority": 1,
         "Statement": {
           "ByteMatchStatement": {
             "SearchString": "BadBot",
             "FieldToMatch": {
               "UriPath": {}
             },
             "TextTransformations": [
               {
                 "Priority": 0,
                 "Type": "NONE"
               }
             ]
           }
         },
         "Action": {
           "Block": {}
         },
         "VisibilityConfig": {
           "SampledRequestsEnabled": true,
           "CloudWatchMetricsEnabled": true,
           "MetricName": "Rule1Metric"
         }
       }
     ]
   }
   ```

2. **Run the `check-capacity` Command**:
   Use the AWS CLI to check the capacity:
   ```bash
   aws wafv2 check-capacity --scope REGIONAL --rules file://rules.json
   ```

The output will look like this:
```json
{
    "Capacity": 15
}
```

This command returns the total WCU requirement for your specified rules. Ensure it doesn't exceed 5,000 WCUs for a single Web ACL.

#### Integrating Capacity Check with Terraform

To avoid deployment failures due to WCU limits, integrate the capacity check into your Terraform workflow using a shell script:

1. **Create a Shell Script**:
   Write a script (`check_capacity.sh`) to perform the capacity check:
   ```bash
   #!/bin/bash

   CAPACITY=$(aws wafv2 check-capacity --scope REGIONAL --rules file://rules.json | jq '.Capacity')

   if [ "$CAPACITY" -gt 5000 ]; then
     echo "Error: Capacity units exceeded. Current capacity: $CAPACITY"
     exit 1
   else
     echo "Capacity check passed. Current capacity: $CAPACITY"
     export TERRAFORM_CAPACITY_CHECK=passed
   fi
   ```

2. **Integrate with Terraform**:
   Modify your Terraform workflow to include the script:
   ```bash
   ./check_capacity.sh

   if [ "$TERRAFORM_CAPACITY_CHECK" == "passed" ]; then
     terraform apply
   else
     echo "Terraform apply aborted due to capacity issues."
   fi
   ```

By incorporating this script, you ensure that your Terraform deployments only proceed if the WCU requirements are within limits, preventing potential failures and downtime.

### Conclusion

Handling WCU limits is crucial for successful AWS WAF deployments using Terraform. By leveraging the `check-capacity` command in AWS CLI and integrating it into your Terraform workflow, you can proactively manage capacity and avoid deployment issues. This approach ensures a smoother, more reliable infrastructure management process especially until terraform starts to support a similar built-in functionality.

For further reading, check the official [AWS WAF documentation on WCUs](https://docs.aws.amazon.com/waf/latest/developerguide/aws-waf-capacity-units.html) and the [AWS CLI `check-capacity` command reference](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/wafv2/check-capacity.html).