

# Add a custom firewall to your app

**Welcome to the new AWS Amplify**  
Amplify now puts hosting, a Postgres database, and sign-in in one AWS account. Deploy from your terminal or from Git, check every change in a preview first, and roll back in one step if something breaks.  
Looking for Amplify Hosting? See the [Amplify Hosting User Guide](https://docs.aws.amazon.com/amplify/hosting/userguide/welcome.html).

AWS WAF (AWS Web Application Firewall) is a web application firewall that helps secure your web applications and APIs by blocking requests before they reach your servers. The Amplify managed firewall protects every app, and its rules are sufficient for most apps. Add a custom firewall only when you need rules that the Amplify managed firewall does not provide.

A custom firewall is an AWS WAF protection pack that you own and manage in your AWS account. A protection pack is an AWS WAF web access control list (web ACL).

When you use a custom firewall, you can do the following in AWS WAF:
+ Add rules that the Amplify managed firewall does not provide, such as SQL injection protection, IP address or country restrictions, CAPTCHA, or custom rate limits.
+ Manage your rules, rate limits, IP sets, and regex pattern sets.
+ View sampled requests, filtered by the Amazon Resource Name (ARN) of your app.
+ View traffic metrics in the `AWS/WAFV2` CloudWatch namespace.
+ Turn on logging to a CloudWatch Logs log group from the **Security** page.

**Firewall logs for a custom firewall**  
A custom firewall does not deliver firewall logs until you turn on logging for its protection pack. Until then, the **Security** page shows a sample of requests from the last 3 hours.

## Requirements
<a name="customize-your-app-firewall-custom-requirements"></a>

Before you add a custom firewall, review the following requirements:
+  **Amplify plan**: A custom firewall requires the Pro plan or higher. The Basic plan does not include a custom firewall. Your plan sets how many protection packs (web ACLs) you can use and how many rules each protection pack can contain. A protection pack that your plan covers is dedicated to a single app and cannot be associated with another app or resource type. Some rule types, such as regex matching, JavaScript challenge, and bot control, require the Amplify Pro Plus plan or higher. In the console, a protection pack that your plan does not support shows a **Requires** badge that names the lowest plan that supports it. For the available plans and their web ACL allowances, see [Pricing and spend limits](pricing.md).
+  **Scope and Region**: Create the protection pack with `CLOUDFRONT` (Global) scope in the US East (N. Virginia) Region (`us-east-1`). This requirement applies regardless of the AWS Region of your app. To find your protection pack on the AWS WAF console, choose the **Global (CloudFront)** scope. Your account might already have Regional protection packs, but Amplify does not support them.
+  **Whole app**: The firewall applies to your entire app, including all environments and custom domains.
+  **Permissions**: The AWS Identity and Access Management (IAM) identity that adds or removes a custom firewall needs specific Amplify and AWS WAF permissions. For the list, see [Permissions and the web ACL resource policy](#customize-your-app-firewall-custom-permissions).

### Permissions and the web ACL resource policy
<a name="customize-your-app-firewall-custom-permissions"></a>

When you associate a protection pack with your app, Amplify attaches a resource policy to the protection pack. This policy lets Amplify use the protection pack. Amplify creates this resource policy for you. For more information, see [GetPermissionPolicy](https://docs.aws.amazon.com/waf/latest/APIReference/API_GetPermissionPolicy.html) in the *AWS WAF API Reference*. To view the policy, run the following command:

```bash
aws wafv2 get-permission-policy --resource-arn <web-acl-arn> --region us-east-1
```

The following table lists the permissions that your IAM identity needs for each custom firewall task in the Amplify console.


| Task | Permissions | 
| --- | --- | 
| List and inspect protection packs |  `amplifygen3:ListResourcesForWebACL`, `wafv2:GetWebACL`, `wafv2:ListResourcesForWebACL`, and `wafv2:ListWebACLs`  | 
| Create a protection pack with pre-loaded rules |  `wafv2:CreateWebACL`  | 
| Add a custom firewall to your app |  `amplifygen3:AssociateWebACL`, `wafv2:AssociateWebACL`, `wafv2:GetPermissionPolicy`, and `wafv2:PutPermissionPolicy`  | 
| Remove a custom firewall from your app |  `amplifygen3:DisassociateWebACL` and `wafv2:DisassociateWebACL`  | 
| Check which protection pack an app uses |  `amplifygen3:GetWebACLForResource` and `wafv2:GetWebACLForResource`  | 
| Change or delete the rules in a protection pack in the AWS WAF console |  `wafv2:DeleteWebACL` and `wafv2:UpdateWebACL`  | 

To limit who can add or remove a custom firewall, scope `amplifygen3:AssociateWebACL` and `amplifygen3:DisassociateWebACL` to your app ARN, in the format `arn:aws:amplifygen3:region:111122223333:app/app-name `. Do not scope `amplifygen3:GetWebACLForResource` or `amplifygen3:ListResourcesForWebACL` to an app ARN. Amplify checks these two actions against the protection pack ARN, so an app ARN causes an `AccessDeniedException` error.

For the permissions to view metrics and logs, see [Permissions to monitor your firewall](customize-your-app-firewall-monitor.md#customize-your-app-firewall-monitor-permissions).

For more information about AWS WAF permissions, see [Using IAM with AWS WAF](https://docs.aws.amazon.com/waf/latest/developerguide/security-iam.html) in the *AWS WAF Developer Guide*.

You cannot delete a protection pack while it is associated with an app. Remove the custom firewall from your app first, and then delete the protection pack.

## Add a custom firewall in the console
<a name="customize-your-app-firewall-custom-console"></a>

In the Amplify console, you can create a new protection pack with pre-loaded rules. You can also use a protection pack that you already created on the AWS WAF console.

Use the following procedure to add a custom firewall to an existing app in the Amplify console:

1. Sign in to the AWS Management Console and open the Amplify console.

1. On the apps page, choose the name of the app to add the firewall to.

1. In the navigation pane, choose **Security**.

1. Choose **Add custom firewall**.

1. For **Protection type**, choose **Customer managed protection**.

1. For **Customization method**, do one of the following:
   + To create a new protection pack, choose **Create protection with pre-loaded rules**. Then enter a **Protection pack name**. You cannot change the name later.
   + To use a protection pack that you created on the AWS WAF console, choose **Use existing protection pack**. Then choose the protection pack from the list. The list shows only protection packs with `CLOUDFRONT` scope.

1. Select the check box to confirm that you understand that rule changes take several minutes to propagate. Until deployment completes, previous rules might evaluate some of your traffic.

1. Choose **Add custom firewall**.

A new protection pack starts with the same three AWS Managed Rules groups as the Amplify managed firewall, at their AWS WAF default settings. You can change these rules later on the AWS WAF console. For the rules of the Amplify managed firewall, see [Amplify managed firewall](WAF-integration.md#customize-your-app-firewall-sbd). Before you use the protection pack in production, test large saves and file uploads in your app.

The **Security** page shows that Amplify is associating the custom firewall. Associating the firewall takes several minutes, depending on the size of your app. Until it finishes, the Amplify managed firewall still protects your app. The **Security** page shows your custom firewall when association completes. Choose **View details** to see the protection pack.

If the association fails, the Amplify managed firewall still protects your app. To try again, choose **Retry**. To remove the failed configuration, choose **Clear firewall configuration**. Amplify might create a new protection pack but fail to associate it with your app. In that case, your Amplify plan does not cover the protection pack, and standard AWS WAF pricing applies. If you do not plan to retry, delete the protection pack in AWS WAF.

### Rules that your plan includes
<a name="customize-your-app-firewall-custom-plan-rules"></a>

When you choose **Use existing protection pack**, the console checks each protection pack in the list against your plan. A protection pack that your plan does not support shows a **Requires** badge. The badge names the lowest plan that supports all of its rules. A protection pack with more rules than any plan allows shows **Too many rules**.

Some premium AWS Managed Rules groups, such as Targeted Bot Control and account takeover prevention, are add-ons. AWS WAF bills them in addition to your Amplify plan. The console marks a protection pack that uses them with **Additional charges**. For plan prices, rule allowances, and add-on fees, see [Pricing and spend limits](pricing.md).

## Add a custom firewall with the CLI
<a name="customize-your-app-firewall-custom-cli"></a>

You can also create a protection pack in AWS WAF and then associate it with your app by using the AWS Command Line Interface (AWS CLI). For instructions on creating a protection pack, see [Creating a protection pack (web ACL) in AWS WAF](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-creating.html) or [Getting started with AWS WAF using the new console experience](https://docs.aws.amazon.com/waf/latest/developerguide/setup-iap-console.html#getting-started-create-protection-pack) in the *AWS WAF Developer Guide*. For the Amplify command that associates a protection pack with your app, see [CLI reference](aws-cli-support-chapter.md).

Associating the firewall takes several minutes. Until it finishes, the Amplify managed firewall still protects your app.

## Why an association fails
<a name="customize-your-app-firewall-custom-fails"></a>

An association fails in the following cases:
+ Your plan does not include a custom firewall.
+ The protection pack has more rules than your plan allows, or uses a rule type that your plan does not include.
+ The protection pack is already associated with another app. This includes apps in Amplify Hosting.
+ The protection pack does not have `CLOUDFRONT` scope.

## Roll out new rules safely
<a name="customize-your-app-firewall-custom-count"></a>

Unless you turn on logging, a custom firewall keeps only a 3-hour sample of requests, so you cannot review all blocked requests after the fact. A new rule in Block mode can block real users right away, so do not create a protection pack with new rules in Block mode. Instead, roll out new rules in the following order:

1. Add each managed rule group in Count mode (`"OverrideAction": { "Count": {} }`), and each rate-based rule with `"Action": { "Count": {} }`. Keep the default action of the protection pack as `Allow`.

1. Observe traffic for a few hours. Use sampled requests and the `AWS/WAFV2` CloudWatch metrics in `us-east-1` to check which requests each rule matches.

1. After you confirm that a rule matches only unwanted traffic, switch the rule to Block.

To see which rule matched a request, view sampled requests on the **Security** page. For a full checklist for testing and tuning rules, see [Testing and tuning high-level steps](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-testing-high-level.html) in the *AWS WAF Developer Guide*.

## Remove a custom firewall
<a name="customize-your-app-firewall-custom-remove"></a>

When you remove a custom firewall, your app reverts to the Amplify managed firewall and stays protected by it. Amplify does not delete your AWS WAF resources.

Your Amplify plan covers a protection pack only while it is associated with your Amplify app. After you remove a protection pack from your app, it is no longer part of your Amplify plan, and standard AWS WAF pricing applies to it. If you no longer need the protection pack, delete it. For more information, see [AWS WAF pricing](https://aws.amazon.com/waf/pricing/).

### Remove a custom firewall in the console
<a name="customize-your-app-firewall-custom-remove-console"></a>

Use the following procedure to remove a custom firewall in the Amplify console:

1. Sign in to the AWS Management Console and open the Amplify console.

1. On the apps page, choose the name of the app to remove the firewall from.

1. In the navigation pane, choose **Security**.

1. Choose **Remove custom firewall**.

1. In the **Revert to Secured by default** dialog box, choose **Confirm revert**.

Reverting takes several minutes, depending on the size of your app. Until it finishes, your custom firewall still protects your app.

### Remove a custom firewall with the CLI
<a name="customize-your-app-firewall-custom-remove-cli"></a>

To remove a custom firewall by using the AWS CLI, see [CLI reference](aws-cli-support-chapter.md). Removing the firewall takes several minutes. Until it finishes, your custom firewall still protects your app.

### Delete the protection pack
<a name="customize-your-app-firewall-custom-delete"></a>

A protection pack that you removed from your app is no longer part of your Amplify plan, and standard AWS WAF pricing applies to it. Delete a protection pack that you no longer need. Your app is already protected by the Amplify managed firewall, so deleting the protection pack does not leave your app unprotected.

Before you delete a protection pack, remove it from your app and wait until the removal finishes. AWS WAF does not let you delete a protection pack that is still associated with an app.

**Irreversible action**  
You cannot undo deleting a protection pack. AWS WAF deletes all of its rules. If you need the rules again, you must create and configure a new protection pack.

For instructions on deleting a protection pack, see [Getting started with AWS WAF using the new console experience](https://docs.aws.amazon.com/waf/latest/developerguide/setup-iap-console.html#getting-started-clean-up) in the *AWS WAF Developer Guide*.