TerraformでWAFとCloudFrontを連携する際のエラー対応ログ
CloudFrontが既に作成されている前提で、以下のコードでterraform applyする
(WAF Web ACLとアソシエーションのresourceを作成する想定)
resource "aws_wafv2_web_acl" "main" {
provider = aws.north_virginia
name = var.waf.name
description = var.waf.description
scope = "CLOUDFRONT"
~
}
resource "aws_wafv2_web_acl_association" "main" {
web_acl_arn = aws_wafv2_web_acl.main.arn
resource_arn = var.waf.target_arn
}
以下のエラーが投げられる
Error: WAFInvalidParameterException: Error reason: The ARN isn't valid. A valid ARN begins with arn: and includes other information separated by colons or slashes., field: RESOURCE_ARN, parameter: arn:aws:wafv2:us-east-1:{AccountID}:global/webacl/{ResourceName}/{ResourceID}
│ {
│ RespMetadata: {
│ StatusCode: 400,
│ RequestID: "hoge"
│ },
│ Field: "RESOURCE_ARN",
│ Message_: "Error reason: The ARN isn't valid. A valid ARN begins with arn: and includes other information separated by colons or slashes., field: RESOURCE_ARN, parameter: arn:aws:wafv2:us-east-1:{AccountID}:global/webacl/{ResourceName}/{ResourceID}",
│ Parameter: "arn:aws:wafv2:us-east-1:{AccountID}:global/webacl/{ResourceName}/{ResourceID}",
│ Reason: "The ARN isn't valid. A valid ARN begins with arn: and includes other information separated by colons or slashes."
│ }
│
│ with module.waf_api.aws_wafv2_web_acl_association.main,
│ on ../../shared/modules/waf/acl.tf line 84, in resource "aws_wafv2_web_acl_association" "main":
│ 84: resource "aws_wafv2_web_acl_association" "main" {
wafv2_web_acl_associationリソースの公式ドキュメントを見てみると、以下のように記載がある

意訳すると「CloudFrontと関連づける場合は、aws_wafv2_web_acl_association 使わずにcloudfront_distributionリソースのweb_acl_idプロパティに定義してちょ。」とのこと。
素直に以下のようにコードを修正して再度terraform applyを実行
- 変更点
- acl.tfの
aws_wafv2_web_acl_associationリソースを削除 - distribution.tfの
aws_cloudfront_distributionリソースにweb_acl_idプロパティを追加-
var.web_acl_idはWeb ACLのID(aws_wafv2_web_acl.main.id)が入る想定
-
- acl.tfの
resource "aws_wafv2_web_acl" "main" {
provider = aws.north_virginia
name = var.waf.name
description = var.waf.description
scope = "CLOUDFRONT"
~
}
resource "aws_cloudfront_distribution" "main" {
aliases = var.aliases
comment = var.comment
web_acl_id = var.web_acl_id
またしてもエラーが投げられる。。。
エラーの内容は以下
│ Error: error updating CloudFront Distribution (hoge): InvalidWebACLId: Web ACL is not accessible by the requester.
│ status code: 400, request id: fuga
│
│ with module.cloudfront_front.aws_cloudfront_distribution.main,
│ on ../../shared/modules/cloudfront/distribution.tf line 6, in resource "aws_cloudfront_distribution" "main":
│ 6: resource "aws_cloudfront_distribution" "main" {
Web ACL is not accessible by the requester(リクエスタが Web ACL にアクセスできません)ってわかりにくいエラーですね、、
エラーをコピーしてググってみると、stackoverflowが投稿されていました!
解決済みの回答もありますね

When using WAFv2, you need to specify the the ARN not the ID to web_acl_id in aws_cloudfront_distribution.
See the note here https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#web_acl_id
意訳すると「WAFv2の場合は、Web ACLのIDじゃなくてARNをweb_acl_idプロパティに入れろ。公式ドキュメントちゃんと呼んでちょ。」とのこと。
aws_cloudfront_distributionリソースの公式ドキュメント見てみます

To specify a web ACL created using the latest version of AWS WAF (WAFv2), use the ACL ARN, for example aws_wafv2_web_acl.example.arn. To specify a web ACL created using AWS WAF Classic, use the ACL ID, for example aws_waf_web_acl.example.id
意訳すると「WAFv2の場合はARNをweb_acl_idプロパティに入れろ。WAF Classicの場合だけIDや。」とのこと。
いや、プロパティ名わかりにくすぎやろ。。。
以下のようにコードを修正して再度terraform applyを実行
- 変更点
- distribution.tfの
aws_cloudfront_distributionリソースにweb_acl_idプロパティを追加-
var.web_acl_idはWeb ACLのARN(aws_wafv2_web_acl.main.arn)が入る想定
-
- distribution.tfの
resource "aws_cloudfront_distribution" "main" {
aliases = var.aliases
comment = var.comment
web_acl_id = var.web_acl_id
Apply complete!

WAFを実装するだけで2時間ほどかかってしまいました、、
今日の教訓「公式ドキュメントは必ずちゃんと読みましょう」