DevOps Hive

Patterns / Protect

Add AWS WAF to an App Runner service

Put a web ACL in front of an App Runner service so common attack requests and floods from a single IP address are turned away before they reach your container.

  1. Browser
  2. WAF web ACL
  3. App Runner
  4. Your app

How it works

  1. What reaches a public app

    Any app with a public address gets a steady stream of automated requests looking for admin pages, old exploits and login forms to guess at. Each one costs you compute, and one of them may find a real hole. A web ACL filters requests before App Runner hands them to your code.

  2. Create the web ACL

    The web ACL is regional and must be in the same region as the App Runner service. Its default action is allow, so the rules only remove requests and a request that matches nothing goes through as before.

  3. Add the AWS managed rule groups

    The common rule set covers SQL injection, cross site scripting and similar request patterns. The known bad inputs set covers patterns tied to published exploits. AWS maintains both, so you do not write or update the rules yourself.

  4. Add a rate limit

    One IP address may make 1000 requests in five minutes before it is blocked until it slows down. That is far above what a person does and well below what a script does. Lower it if your app is small, and raise it if many of your users share one office address.

  5. Start in count mode

    The mistake people make is turning blocking on straight away. The common rule set rejects request bodies over 8 KB, which breaks file uploads and large form or JSON posts. Apply with count_only set to true for a few days, look at the sampled requests in the WAF console, then set it to false.

  6. Attach it to the service

    Pass the App Runner service ARN and apply. The association takes effect within a minute or so and needs no redeploy of the app.

What you set

name
Name for the web ACL For example web-app
app_runner_service_arn
The App Runner service to protect For example arn:aws:apprunner:us-east-1:111122223333:service/web-app/0123456789abcdef0123456789abcdef
rate_limit
Requests per IP address in five minutes For example 1000
count_only
Record matches without blocking while you test For example true

What it costs to run

About $8 a month plus traffic: $5 for the web ACL, $1 for each of the three rules, and $0.60 per million requests inspected. Estimate.

The configuration

# AWS WAF in front of an App Runner service.
#
# What this creates:
#   - a regional web ACL that allows requests by default
#   - two AWS managed rule groups: common web attacks and known bad inputs
#   - a rate limit per client IP address
#   - the association between the web ACL and your App Runner service

terraform {
  required_version = ">= 1.6.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# The web ACL must be in the same region as the App Runner service.
provider "aws" {
  region = var.region
}

variable "region" {
  description = "AWS region of the App Runner service."
  type        = string
  default     = "us-east-1"
}

variable "name" {
  description = "Name for the web ACL."
  type        = string
}

variable "app_runner_service_arn" {
  description = "ARN of the App Runner service to protect."
  type        = string
}

variable "rate_limit" {
  description = "Requests one IP address may make in five minutes before it is blocked."
  type        = number
  default     = 1000
}

variable "count_only" {
  description = "When true, the managed rule groups record matches without blocking. Use it for the first few days."
  type        = bool
  default     = false
}

resource "aws_wafv2_web_acl" "main" {
  name        = var.name
  description = "Web ACL for ${var.name}"
  scope       = "REGIONAL"

  # Anything no rule matches is let through. The rules below only take
  # requests out.
  default_action {
    allow {}
  }

  # Checked first, so a flood is turned away before the heavier rules run.
  rule {
    name     = "rate-limit-per-ip"
    priority = 0

    action {
      block {}
    }

    statement {
      rate_based_statement {
        limit              = var.rate_limit
        aggregate_key_type = "IP"
      }
    }

    visibility_config {
      cloudwatch_metrics_enabled = true
      metric_name                = "${var.name}-rate-limit"
      sampled_requests_enabled   = true
    }
  }

  # SQL injection, cross site scripting, oversized bodies and similar.
  rule {
    name     = "aws-common"
    priority = 1

    # Managed groups carry their own block actions. "none" keeps them,
    # "count" turns every match into a record without blocking.
    override_action {
      dynamic "count" {
        for_each = var.count_only ? [1] : []
        content {}
      }

      dynamic "none" {
        for_each = var.count_only ? [] : [1]
        content {}
      }
    }

    statement {
      managed_rule_group_statement {
        vendor_name = "AWS"
        name        = "AWSManagedRulesCommonRuleSet"
      }
    }

    visibility_config {
      cloudwatch_metrics_enabled = true
      metric_name                = "${var.name}-common"
      sampled_requests_enabled   = true
    }
  }

  # Request patterns tied to known exploits, such as Log4j lookups.
  rule {
    name     = "aws-known-bad-inputs"
    priority = 2

    override_action {
      dynamic "count" {
        for_each = var.count_only ? [1] : []
        content {}
      }

      dynamic "none" {
        for_each = var.count_only ? [] : [1]
        content {}
      }
    }

    statement {
      managed_rule_group_statement {
        vendor_name = "AWS"
        name        = "AWSManagedRulesKnownBadInputsRuleSet"
      }
    }

    visibility_config {
      cloudwatch_metrics_enabled = true
      metric_name                = "${var.name}-known-bad-inputs"
      sampled_requests_enabled   = true
    }
  }

  # Sampled requests let you see in the WAF console which rule matched a
  # request, which is how you find a rule that blocks real users.
  visibility_config {
    cloudwatch_metrics_enabled = true
    metric_name                = var.name
    sampled_requests_enabled   = true
  }
}

resource "aws_wafv2_web_acl_association" "app_runner" {
  resource_arn = var.app_runner_service_arn
  web_acl_arn  = aws_wafv2_web_acl.main.arn
}

output "web_acl_arn" {
  description = "ARN of the web ACL."
  value       = aws_wafv2_web_acl.main.arn
}

Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.

Written by DevOps Hive. Last updated 2026-11-27.

More in protect