AWS CloudFront・WAFでAIクローラーをブロックする設定方法
AWSでAIボットをブロックする2つのアプローチ
AWSを使ったWebサービスでAIクローラーをブロックするには主に2つのアプローチがあります。
- AWS WAF(Web Application Firewall):User-Agentベースのカスタムルールでブロック
- Lambda@Edge / CloudFront Functions:エッジでUser-Agentを検査してフィルタリング
多くのケースではWAFが最もシンプルで管理しやすい方法です。
方法① AWS WAFでカスタムルールを作成
マネジメントコンソールでの設定手順
- AWSコンソール → WAF & Shield → Web ACLs
- 「Create web ACL」をクリック
- Resource type: CloudFront distributions(またはRegional)
- 「Add rules」→「Add my own rules and rule groups」
- Rule type: Regular rule
ルール設定(マネジメントコンソール)
条件の設定:
- Field: Header → header name:
user-agent - Match type: Contains string または Matches regex pattern
- 文字列:
GPTBot、ClaudeBot、PerplexityBot等を個別に追加
アクション: Block
AWS WAF正規表現パターンセット(CLI)
# 正規表現パターンセットの作成
aws wafv2 create-regex-pattern-set --name "AI-Bot-Patterns" --scope CLOUDFRONT --regular-expression-pattern-strings "GPTBot" "OAI-SearchBot" "ClaudeBot" "anthropic-ai" "PerplexityBot" "Google-Extended" "Amazonbot" "Bytespider" "CCBot" "cohere-ai" "MistralBot" --region us-east-1
# WAFルールを含むWeb ACLの作成
aws wafv2 create-web-acl --name "block-ai-bots" --scope CLOUDFRONT --default-action Allow={} --rules '[
{
"Name": "BlockAIBots",
"Priority": 1,
"Statement": {
"RegexPatternSetReferenceStatement": {
"ARN": "arn:aws:wafv2:us-east-1:ACCOUNT_ID:global/regexpatternset/AI-Bot-Patterns/ID",
"FieldToMatch": {
"SingleHeader": {"Name": "user-agent"}
},
"TextTransformations": [{"Priority": 0, "Type": "LOWERCASE"}]
}
},
"Action": {"Block": {}},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "BlockAIBots"
}
}
]' --visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=block-ai-bots --region us-east-1
方法② CloudFront Functionsで軽量ブロック
CloudFront Functionsはエッジで動くJavaScript関数です。WAFより安価です(無料枠2M回/月)。
// CloudFront Function(Viewer Request)
function handler(event) {
var request = event.request;
var ua = (request.headers['user-agent'] || {value: ''}).value;
var aiBotPattern = /GPTBot|OAI-SearchBot|ClaudeBot|anthropic-ai|PerplexityBot|Google-Extended|Amazonbot|Bytespider|CCBot|cohere-ai/i;
if (aiBotPattern.test(ua)) {
return {
statusCode: 403,
statusDescription: 'Forbidden',
headers: {
'content-type': { value: 'text/plain' }
},
body: 'AI crawlers are not permitted'
};
}
return request;
}
CloudFront Functionsのデプロイ手順
# 関数の作成
aws cloudfront create-function --name block-ai-bots --function-config Comment="Block AI training crawlers",Runtime=cloudfront-js-2.0 --function-code fileb://block-ai-bots.js --region us-east-1
# 関数の公開
aws cloudfront publish-function --name block-ai-bots --if-match ETAG --region us-east-1
方法③ Terraform IaCでの構成
resource "aws_wafv2_regex_pattern_set" "ai_bots" {
name = "ai-bot-patterns"
scope = "CLOUDFRONT"
regular_expression {
regex_string = "GPTBot|OAI-SearchBot|ClaudeBot|anthropic-ai|PerplexityBot|Google-Extended|Amazonbot|Bytespider|CCBot|cohere-ai"
}
}
resource "aws_wafv2_web_acl" "block_ai_bots" {
name = "block-ai-bots"
scope = "CLOUDFRONT"
default_action {
allow {}
}
rule {
name = "BlockAIBots"
priority = 1
statement {
regex_pattern_set_reference_statement {
arn = aws_wafv2_regex_pattern_set.ai_bots.arn
field_to_match {
single_header {
name = "user-agent"
}
}
text_transformation {
priority = 0
type = "LOWERCASE"
}
}
}
action {
block {}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "BlockAIBots"
sampled_requests_enabled = true
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "block-ai-bots"
sampled_requests_enabled = true
}
}
resource "aws_cloudfront_distribution" "main" {
# ... 既存の設定 ...
web_acl_id = aws_wafv2_web_acl.block_ai_bots.arn
}
コスト試算
| 方法 | 月額費用(目安) | リクエスト数 |
|---|---|---|
| AWS WAF Web ACL | $5/月 + $0.60/百万リクエスト | 無制限 |
| CloudFront Functions | $0(無料枠2M/月) | 2M/月まで無料 |
| Lambda@Edge | $0.60/百万リクエスト | 無制限 |
月間リクエスト数が少ない場合はCloudFront Functions、ルールが複雑な場合はWAFが適しています。
AIアクセスの監視と組み合わせ
WAFでブロックした後も、AI Access Monitorでブロック済みを含むAIボットのアクセス全体を可視化できます。CloudWatchのWAFメトリクスと組み合わせることで、どのボットがどれだけブロックされているかを把握できます。