TerraformでCloudfrontにWAFアタッチ時にエラー
TerraformでWAFをCloudfrontにアタッチしようとした際にエラーが発生しました。
WAFとCloudfrontは公式モジュールを利用して作成しています。
- エラーログ
╷
│ Error: updating CloudFront Distribution (E3SVU6W6XV9FRF): InvalidWebACLId: Web ACL is not accessible by the requester.
│ status code: 400, request id: ac4db5f0-b3ac-4c1f-8c70-b099df011216
│
│ with module.cloudfront.aws_cloudfront_distribution.this[0],
│ on .terraform/modules/cloudfront/main.tf line 15, in resource "aws_cloudfront_distribution" "this":
│ 15: resource "aws_cloudfront_distribution" "this" {
│
対処法
WAFv2の場合はWebACLのARNを指定する必要がある
マニュアルに書いてありましたw
なので以下のように変更しました。
< web_acl_id = module.waf.web_acl_id # WAFのWebACL IDを指定
---
> web_acl_id = module.waf.web_acl_arn # WAFのWebACL ARNを指定
設定項目が web_acl_idなのでそのままweb_acl_idでいいだろうと思ってしまったのが
敗因ですね。
自身の固定概念にとらわれず、ちゃんとマニュアルを読みましょうということで。
確認
- WAF適用前
curl -I https://d2XXXXXXXXXXX.cloudfront.net
HTTP/2 200
content-type: text/html; charset=UTF-8
server: nginx
- WAF適用後
curl -I https://d2XXXXXXXXXXX.cloudfront.net
HTTP/2 301
server: CloudFront
date: Mon, 17 Jun 2024 11:57:26 GMT
WAF設定詳細は省きますが、カスタムレスポンスで明示的に別のページを表示させる仕様になります。
なのでステータスコードが301になっていれば想定している動作となり稼働確認OKです。