# Protect an application load balancer with a web ACL

Attach a web ACL so requests to an application load balancer pass the web application firewall before they reach your targets.

The web ACLs are the CDN's: the Deutsche Cloud Core managed rules, IP and country lists, match and rate
rules and the browser challenge. One ACL can protect several load balancers and distributions. See
[the web application firewall](/docs/cdn#the-web-application-firewall) for the rules themselves.

Only `application` load balancers run a web ACL. For `lb11`–`lb31`, put a CDN distribution with
the ACL in front instead.

## Prerequisites

- An `application` load balancer and a web ACL of your organization.
- Permission for `lb:SetWebAcl` on the load balancer, and `waf:GetWebAcl` on the ACL.
- In the console, also `lb:DescribeLoadBalancers` on the load balancer (without it its page
  answers 404) and `waf:ListWebAcls` to choose an ACL from the list; without either of
  `lb:SetWebAcl` or `waf:ListWebAcls` the section only shows the current ACL.

## Attach or detach the web ACL

**Console**
1. Open **Load balancers**, then open the load balancer. The **Overview** tab opens.
2. Under **Web application firewall**, for **Web ACL**, choose the ACL, or **None** to detach it.

The choice is saved at once; there is no separate save step.

**CLI**
```sh
dcloud lb load-balancers update lb-id --web-acl-id acl-id
```

**API**
```http
PATCH /lb/api/load-balancers/lb-id
{ "webAclId": null }
```

`null` detaches the ACL; an ACL's id attaches it.

**Terraform**
```hcl
resource "dcloud_lb" "web" {
  name       = "web-lb"
  type       = "application"
  region_id  = "de1"
  web_acl_id = dcloud_waf_web_acl.shop.id
}
```

`web_acl_id` changes in place. Setting it when the load balancer is created also needs
`lb:SetWebAcl`.

## Result

`200` with the load balancer and its `webAclId`. The fleet runs the ACL within seconds. A request
on any name of the load balancer passes it after TLS, before the HTTP-to-HTTPS redirect and before
it is forwarded to a target, so a blocked request never reaches a target. Rate rules count
exactly, because one fleet machine serves at a time.

Requests the load balancer refuses for its own limits are answered before the ACL:

- `421` for a name on a connection opened for another name;
- `429` over the load balancer's request rate;
- `503` at its connection limit.

Matches are in the access log and under `dcloud waf web-acls logs` (needs `waf:GetWafLogs`;
narrow it to this load balancer with `--load-balancer-id`); the **Live** tab shows the ACL's
counters. See [Monitoring](monitoring.md).

There is no charge beyond the web ACL itself.

## When it is refused

- `400` on any type but `application`.
- `409` for a load balancer another service owns (a database's or a Kubernetes Service's).
- `403` `Attaching web ACL … needs waf:GetWebAcl on it`.
- `404` `Web ACL … not found` for an ACL that is not your organization's.
- `404` `Load balancer not found`.

## Related

- [Route requests by host and path](route-by-host-and-path.md)
- [Monitoring](monitoring.md)
- [Security](security.md)
