AWS Network Firewall: Stateful Traffic Inspection and IDS/IPS for Your VPC

Security groups and NACLs protect your AWS resources at the network perimeter, but they operate at a level of abstraction that leaves significant attack surface uncovered. They cannot block an EC2 instance from connecting to a known command-and-control domain. They cannot detect SQL injection in HTTP traffic. They cannot enforce TLS version requirements at the network layer. AWS Network Firewall fills these gaps with stateful deep packet inspection inside your VPC.

In this article I will walk through deploying AWS Network Firewall in a centralized hub-and-spoke architecture with Terraform, building rule groups for domain filtering and Suricata IPS rules, and the routing configuration that routes all internet-bound and inter-VPC traffic through the firewall for inspection.

Architecture: Centralized Inspection VPC

The standard pattern is a dedicated inspection VPC that hosts the firewall. All internet-bound traffic from spoke VPCs routes through Transit Gateway to the inspection VPC, through the firewall, and then out to the internet. Return traffic takes the same path in reverse. This centralizes inspection at a single chokepoint regardless of how many spoke VPCs you have.

resource "aws_networkfirewall_firewall" "main" {
  name                = "production-firewall"
  firewall_policy_arn = aws_networkfirewall_firewall_policy.main.arn
  vpc_id              = aws_vpc.inspection.id

  subnet_mapping {
    subnet_id = aws_subnet.firewall_az1.id
  }

  subnet_mapping {
    subnet_id = aws_subnet.firewall_az2.id
  }

  subnet_mapping {
    subnet_id = aws_subnet.firewall_az3.id
  }

  firewall_policy_change_protection = true
  subnet_change_protection          = true

  tags = {
    Environment = "production"
    ManagedBy   = "terraform"
  }
}

resource "aws_networkfirewall_firewall_policy" "main" {
  name = "production-firewall-policy"

  firewall_policy {
    stateless_default_actions          = ["aws:forward_to_sfe"]
    stateless_fragment_default_actions = ["aws:forward_to_sfe"]

    stateful_rule_group_reference {
      resource_arn = aws_networkfirewall_rule_group.domain_allowlist.arn
      priority     = 1
    }

    stateful_rule_group_reference {
      resource_arn = aws_networkfirewall_rule_group.threat_signatures.arn
      priority     = 2
    }

    stateful_rule_group_reference {
      resource_arn = aws_networkfirewall_rule_group.strict_order.arn
      priority     = 3
    }

    stateful_engine_options {
      rule_order = "STRICT_ORDER"
    }
  }
}

stateless_default_actions = aws:forward_to_sfe means all traffic that does not match a stateless rule passes to the stateful rule engine for further inspection. This is the correct default. The stateless rules are where you drop obviously malicious traffic quickly before the more expensive stateful inspection. The stateful rules engine handles protocol detection, domain filtering, and IPS signature matching.

STRICT_ORDER rule evaluation processes rules in the order they are defined, stopping at the first match. This is the more predictable model compared to the default action order. Use it when you need to ensure that an allow rule evaluated before a deny rule takes effect.

Domain Filtering Rule Group

resource "aws_networkfirewall_rule_group" "domain_allowlist" {
  capacity = 100
  name     = "domain-allowlist"
  type     = "STATEFUL"

  rule_group {
    rule_variables {
      ip_sets {
        key = "HOME_NET"
        ip_set {
          definition = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
        }
      }
    }

    rules_source {
      rules_source_list {
        generated_rules_type = "DENYLIST"
        target_types         = ["HTTP_HOST", "TLS_SNI"]
        targets = [
          ".torproject.org",
          ".pastebin.com",
          ".ngrok.io",
          ".requestbin.com"
        ]
      }
    }
  }

  tags = { ManagedBy = "terraform" }
}

resource "aws_networkfirewall_rule_group" "threat_signatures" {
  capacity = 1000
  name     = "threat-signatures"
  type     = "STATEFUL"

  rule_group {
    rules_source {
      stateful_rule {
        action = "DROP"
        header {
          direction        = "ANY"
          protocol         = "TCP"
          source           = "ANY"
          source_port      = "ANY"
          destination      = "ANY"
          destination_port = "ANY"
        }
        rule_option {
          keyword  = "sid"
          settings = ["1000001"]
        }
        rule_option {
          keyword  = "msg"
          settings = ["Detected SQL injection attempt"]
        }
        rule_option {
          keyword  = "content"
          settings = ["|27 6F 72 27 20|"]
        }
        rule_option {
          keyword  = "http_uri"
          settings = [""]
        }
      }
    }
  }
}

resource "aws_networkfirewall_rule_group" "strict_order" {
  capacity    = 200
  name        = "managed-threat-signatures"
  type        = "STATEFUL"
  description = "AWS managed threat signature rules"

  rule_group {
    rules_source {
      rules_string = < $EXTERNAL_NET any (msg:"Outbound data exfiltration attempt"; flow:established,to_server; content:"Content-Type|3A| application/octet-stream"; http_header; threshold:type both,track by_src,count 10,seconds 60; sid:2000001; rev:1;)
        drop tls $EXTERNAL_NET any -> $HOME_NET any (msg:"Block TLS 1.0"; tls.version:1.0; sid:2000002; rev:1;)
        drop tls $EXTERNAL_NET any -> $HOME_NET any (msg:"Block TLS 1.1"; tls.version:1.1; sid:2000003; rev:1;)
      SURICATA
    }
  }
}

The DENYLIST domain rule blocks traffic to specific domains regardless of destination IP. This is how you block known data exfiltration destinations, anonymization networks, and developer testing services that should never be reachable from production infrastructure. The HTTP_HOST and TLS_SNI target types cover both plaintext HTTP and encrypted TLS traffic, since the SNI extension in the TLS handshake reveals the destination hostname even before the connection is established.

The Suricata rule blocking TLS 1.0 and 1.1 enforces minimum TLS version at the network layer. This catches connections from applications or legacy devices that have not been updated to require modern TLS, without requiring changes to each individual application.

Firewall Logging

resource "aws_networkfirewall_logging_configuration" "main" {
  firewall_arn = aws_networkfirewall_firewall.main.arn

  logging_configuration {
    log_destination_config {
      log_destination = {
        logGroup = aws_cloudwatch_log_group.nfw_flow.name
      }
      log_destination_type = "CloudWatchLogs"
      log_type             = "FLOW"
    }

    log_destination_config {
      log_destination = {
        logGroup = aws_cloudwatch_log_group.nfw_alert.name
      }
      log_destination_type = "CloudWatchLogs"
      log_type             = "ALERT"
    }
  }
}

FLOW logs capture all connections that pass through the firewall. ALERT logs capture connections that matched a rule with an alert or drop action. For cost management, send ALERT logs to CloudWatch for real-time alerting and FLOW logs to S3 for long-term retention and forensic analysis. FLOW logs are high volume for busy networks and CloudWatch costs more per GB than S3 for long-term storage.

Closing Thoughts

Network Firewall gives you the deep packet inspection layer that security groups and NACLs cannot provide. Domain filtering blocks known-bad destinations regardless of IP. Suricata IPS rules detect attack patterns in payload content. TLS version enforcement catches legacy clients at the network layer. Together they provide a defense-in-depth layer that meaningfully reduces your exposure surface.

Deploy it in a centralized inspection VPC so all internet-bound traffic routes through it regardless of which spoke VPC it originates from. Start with domain deny lists for obvious targets. Add managed threat signature rule groups. Enable STRICT_ORDER evaluation so your rule precedence is deterministic. The firewall is most valuable when you treat it as a policy enforcement point rather than a passive observer.

Enjoy the cloud.

Osama


#AWS #NetworkFirewall #CloudSecurity #NetworkEngineering #CloudArchitecture #Terraform #InfrastructureAsCode #AmazonWebServices #SolutionsArchitect #CloudComputing #IDS #IPS #DeepPacketInspection #TechBlog #CloudInfrastructure #Suricata #ZeroTrust #DefenseInDepth #VPC #DevSecOps

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.