The Time I Restricted Access to "Japan Only" with Geo Restriction and It Broke Even Search and Uptime Monitoring: Adding Exception IPs to AWS WAF Country-Based Rules

The Time I Restricted Access to "Japan Only" with Geo Restriction and It Broke Even Search and Uptime Monitoring: Adding Exception IPs to AWS WAF Country-Based Rules

Country-based rules in AWS WAF also blocked search engine crawlers and Route 53 health checkers. This article shows how to exempt only the overseas IPs you need, and how to auto-update them from public lists.
2026.11.17

This page has been translated by machine translation. View original

Hello, I'm Yotsuyan from Classmethod Security.
While working on WAF-related development for CyberNEO, our WAF operations automation service, and for new services, I got some feedback from end users that seemed useful for operations, so I'd like to share the details.

Attacks have been intense recently, and we're getting more requests like "I want to block all access from outside Japan." As one risk mitigation measure, such a requirement is understandable for a service limited to Japan.
With AWS WAF, you only need to write a single Geo match rule, so the configuration itself is easy.

However, once you actually operate it, various issues come up.

  • "My site disappeared from Google search results"
  • "Route 53 health checks keep failing, and the site was judged to be down"

Both were caused by blocking access from overseas IPs that are necessary for operating the site. This article summarizes how to build the rules when excluding only those source IPs from a country-based rule, and where I get the IPs from and how I manage the differences.

What Was Happening

Search engine crawlers from overseas

Crawlers such as Googlebot and Bingbot mostly come from US IPs. If you "block everything except Japan," crawlers can't read your pages. After a while, the pages gradually disappear from search results.

Even for a site aimed at Japan, crawlers don't necessarily come from Japan. This is easy to overlook.

Route 53 health checkers around the world

Route 53 health checks are performed simultaneously by checkers in multiple regions around the world. And Route 53 judges a target healthy only when more than 18% of checkers report it as "healthy" (AWS official documentation "How Amazon Route 53 determines whether a health check is healthy").

If you allow only Japan, only the checker in the Tokyo region can reach you. If there are 8 checker regions, one alone is 12.5%, which doesn't reach 18%. As a result, the target is judged "unhealthy," DNS failover switches, and alarms keep going off.

💡 Before publishing, please check that the list of health checker regions and the 18% threshold match the latest documentation.

What I Considered First but Didn't Adopt

Option 1: Register them as an IP exception (Allow rule)

The easiest way is to put the crawler and health checker IPs into an "Allow" rule and place it before the country rule. However, this had two problems.

  1. Allow lets requests bypass all subsequent inspection. In AWS WAF, evaluation ends as soon as a request matches Allow. So requests from those IPs are not subject to SQL injection checks or rate limiting. You can't say for sure "it's a Google IP, so it's fine" (cloud IPs can also be shared). The risk as an IP may be low, but I thought it wasn't a very good rule configuration.
  2. IPs change. Both search engines and AWS update their address ranges from time to time. An allow list registered by hand becomes outdated before you know it.

Option 2: Judge by User-Agent

"Let it through if the User-Agent contains Googlebot" is also a commonly seen method, but anyone can claim any User-Agent. An attacker could get past the country restriction just by calling themselves Googlebot, so I didn't adopt this.

Google itself also advises verifying whether a request is a genuine Googlebot via reverse DNS or the public IP lists. Since DNS can't be queried within WAF rules, I decided to use the public IP lists.

How to Build the Rule: Block if "the country matches and the IP is not an excluded IP"

In conclusion, I added the condition "except for these IPs" to the country rule itself.

Block if:
  AND(
    GeoMatch(日本以外),          ← 国の条件(日本「以外」なので NOT GeoMatch(JP) で書く)
    NOT IPSet(ヘルスチェッカー v4),
    NOT IPSet(ヘルスチェッカー v6),
    NOT IPSet(検索エンジン v4),
    NOT IPSet(検索エンジン v6)
  )

In AWS WAF rule JSON, it looks like this (example of "block everything except Japan"; ARNs are omitted).

{
  "Name": "country-block",
  "Priority": 0,
  "Action": { "Block": {} },
  "Statement": {
    "AndStatement": {
      "Statements": [
        { "NotStatement": { "Statement": { "GeoMatchStatement": { "CountryCodes": ["JP"] } } } },
        { "NotStatement": { "Statement": { "IPSetReferenceStatement": { "ARN": "arn:aws:wafv2:...:ipset/r53hc-v4/..." } } } },
        { "NotStatement": { "Statement": { "IPSetReferenceStatement": { "ARN": "arn:aws:wafv2:...:ipset/r53hc-v6/..." } } } },
        { "NotStatement": { "Statement": { "IPSetReferenceStatement": { "ARN": "arn:aws:wafv2:...:ipset/search-engine-v4/..." } } } },
        { "NotStatement": { "Statement": { "IPSetReferenceStatement": { "ARN": "arn:aws:wafv2:...:ipset/search-engine-v6/..." } } } }
      ]
    }
  },
  "VisibilityConfig": { "SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "country-block" }
}

The advantage of this rule is that the excluded IPs skip only the country rule. Since it's not Allow but merely "not blocked," the managed rules and rate limiting lined up after it continue to inspect crawler requests as before.

Here are the points I paid attention to when building it.

  • Separate IP sets for IPv4 and IPv6.
    An AWS WAF IP set can hold only one IP version.
  • Separate IP sets by type.
    This is because I want to turn them on and off independently, such as "health check exclusion on, search engine exclusion off." When turning one off, remove the two NOT conditions for that type from the rule.
  • Empty IP sets are fine.
    Referencing an empty IP set doesn't cause an error in the rule. There was no need to put in dummy IPs.
  • Delete an IP set only after the rule no longer references it.
    An IP set that is still referenced can't be deleted. When turning one off, follow the order "rewrite the rule → delete the IP set."

Estimating WCU

A Geo match costs 1 WCU, and each IP set reference costs 1 WCU. With the configuration above, that's 1 + 4 = 5 WCU. WCU doesn't change no matter how many entries an IP set contains, so you don't need to worry about it.

Where to Get the IPs

All of them are official lists published by the providers.

Type Source Contents
Route 53 health checkers https://ip-ranges.amazonaws.com/ip-ranges.json Rows where service is ROUTE53_HEALTHCHECKS (prefixes for IPv4, ipv6_prefixes for IPv6)
Google https://developers.google.com/static/crawling/ipranges/common-crawlers.json Common crawlers such as Googlebot
Google https://developers.google.com/static/crawling/ipranges/special-crawlers.json Special crawlers such as AdsBot
Bing https://www.bing.com/toolbox/bingbot.json Bingbot
Apple https://search.developer.apple.com/applebot.json Applebot

If you want to try it with commands, you can fetch them like this.

# Route 53 health checkers (IPv4)
curl -s https://ip-ranges.amazonaws.com/ip-ranges.json \
  | jq -r '.prefixes[] | select(.service=="ROUTE53_HEALTHCHECKS") | .ip_prefix'

# Googlebot (IPv4 / IPv6)
curl -s https://developers.google.com/static/crawling/ipranges/common-crawlers.json \
  | jq -r '.prefixes[] | .ipv4Prefix // .ipv6Prefix'

Rather than putting the fetched ranges into the IP set as they are, I merge adjacent ranges into one before putting them in. For example, 192.0.2.0/25 and 192.0.2.128/25 together can become a single entry, 192.0.2.0/24. In Python, ipaddress.collapse_addresses can merge adjacent ranges as well as ranges contained within other ranges.

These are the counts when fetched on November 16, 2026 ("original count → after merging").

Type IPv4 IPv6
Route 53 health checkers 31 → 27 26 → 23
Search engines (Google 2 types + Bing + Apple) 359 → 93 283 → 43

The search engine lists have many consecutive small ranges, so merging reduces them considerably. Note that there were no cases of exactly identical ranges being listed in duplicate this time.

An AWS WAF IP set can hold up to 10,000 entries, so there is still plenty of room.

Managing Differences: Fetch Daily and Write Only When Something Changes

IPs change, so you can't register them once and be done. So I set up a scheduled job that runs once a day to re-fetch the official lists and update the IP sets if anything has changed.

The two things I was careful about are "don't update the IP sets when nothing has changed" and "don't use lists with invalid content."

Don't update when nothing has changed

  • At the start of the job, the official lists are fetched only once per type. No matter how many Web ACLs there are, the number of fetches doesn't change.
  • After fetching, adjacent ranges are merged as described earlier and split into IPv4 and IPv6.
  • It compares them with the current IP sets, and if they're the same, nothing is written. On most days it ends with "no change."

I got stuck here once. AWS WAF returns IPv6 addresses in non-abbreviated form (even if you register 2001:4860:4801:2::/64, it comes back as 2001:4860:4801:0002:0000:0000:0000:0000/64). When compared as strings, they were judged "different" every time, and the IP sets were being rewritten every day. Now I normalize both as networks before comparing.

import ipaddress

def same_ranges(current, new):
    def norm(values):
        return {str(ipaddress.ip_network(v, strict=False)) for v in values}
    return norm(current) == norm(new)

Don't use lists with invalid content

If the exclusion list is broken, two kinds of accidents can occur. If it's too broad, things that should be blocked get through; if it's too narrow, crawlers and health checkers get blocked again. So I decided not to update in the following cases.

  • If even one fetch fails, that entire type is not updated. For example, if only Bing couldn't be fetched and we overwrote with the remaining three, Bingbot would be blocked again. Yesterday's list is safer than using an incomplete list.
  • Don't use a list that contains overly broad ranges. The ranges for crawlers and health checkers are small, so if anything broader than /16 for IPv4 or /32 for IPv6 is included, the list is considered broken.
  • Stop if the count drops below half. A legitimate update almost never reduces the list by half or more at once.
  • Notify Slack when stopped. If it stops silently, by the time you notice, it may have been stale for weeks.

Isn't immediate updating necessary?

AWS notifies via an SNS topic (AmazonIpSpaceChanged) when ip-ranges.json changes, so I also considered a design that updates immediately upon receiving it. However, search engines have no such notification. Also, even when ranges change, the old ranges don't disappear immediately, so I judged that one update per day is sufficient. If you want to track only Route 53 strictly, you could receive the SNS notification and call the same update process.

Summary

  • Country restrictions are highly effective, but that also means they block necessary access coming from overseas (such as search engine crawlers and Route 53 health checkers).
  • To let those through, rather than letting everything bypass inspection with an Allow rule, I recommend adding NOT IPSet to the country rule so that only the country rule is skipped.
  • Get the IPs from official lists and update them daily. If there's no difference, don't write; if a fetch fails or the count suddenly drops, stop the update and notify. This keeps things safe without chasing them by hand.

If, after introducing a country restriction, you notice things like "my search ranking seems to have dropped" or "the health check is red," first check the WAF logs to see whether Googlebot or Route 53 are mixed in among the blocked overseas IPs.

Share this article

AWSのお困り事はクラスメソッドへ