0
0

Terraform WAFのmoduleをアップグレードしたらエラー

事象

WAFの管理をTerraformの公式モジュールを使って管理しているのですが、
以下のエラーが発生したのでその対処したときの記録になります。
(公式モジュールを積極的に利用していくとイキっておきながら定期的にハマることがあって恥ずかしいんすが。。)

  • エラー内容
 Error: Unsupported block type

   on .terraform/modules/waf/main.tf line 106, in resource "aws_wafv2_web_acl" "main":
  106:             dynamic "excluded_rule" {

 Blocks of type "excluded_rule" are not expected here.

発生条件

  • 利用しているWAFのmoduleは以下のものです。

元々はWAF管理用のGitHubActionsのymlで実行するようにしていたのですが、
terraform実行するための共通用ymlを利用するようにしたところ発生しました。

terraformの共通用ymlについては本記事との趣旨がずれるので割愛しますが
別途記事化するかもしれません。
参考: https://zenn.dev/stafes_blog/articles/ikkitang-a694b8afeb66f5

再現方法

ローカルでは再現していなかったので再現するためにupgradeを行いました。

terraform -chdir=environments/maintenance init -upgrade

Initializing the backend...
Upgrading modules...
Downloading registry.terraform.io/terraform-aws-modules/cloudfront/aws 2.9.3 for cloudfront...
- cloudfront in .terraform/modules/cloudfront
- step_function in ../../modules/step_functions
Downloading registry.terraform.io/umotif-public/waf-webaclv2/aws 4.0.1 for waf...
- waf in .terraform/modules/waf

Initializing provider plugins...
- Finding hashicorp/aws versions matching ">= 3.27.0, >= 3.64.0, >= 4.44.0"...
- Installing hashicorp/aws v5.54.1...
- Installed hashicorp/aws v5.54.1 (signed by HashiCorp)

Terraform has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
  • 改めてPlanを実行
terraform -chdir=environments/maintenance plan -parallelism=20
╷
│ Error: Unsupported block type
│
│   on .terraform/modules/waf/main.tf line 106, in resource "aws_wafv2_web_acl" "main":
│  106:             dynamic "excluded_rule" {
│
│ Blocks of type "excluded_rule" are not expected here.
╵
make: *** [allplan] Error 1

再現しましたね。

対処方法

結論からかくとWAFのmoduleのバージョンが古かったためでした。
おそらくawsのproviderのバージョンが上がったことで影響が出たのだと思われます。

なので以下のように変更した上で再度upgrade を行いエラーは解消しました。

 module "waf" {
   source  = "umotif-public/waf-webaclv2/aws"
-  version = "~> 4.0.0"
+  version = "~>5.0.0"
  • GitHubActions からの実行でも正常に稼働するか確認
gh workflow run maintenance_html_update.yml --ref bugfix/BranchName
✓ Created workflow_dispatch event for maintenance_html_update.yml at bugfix/BranchName

To see runs for this workflow, try: gh run list --workflow=maintenance_html_update.yml

余談ですがghコマンド便利ですね。

No changes. Your infrastructure matches the configuration.
と出力されることを確認できました。

学び

他の記事でCDKTFの場合はpackage.jsonの記載に注意を促していましたが、
terraformの場合はmoduleのバージョンとaws providerのバージョンにも注意が必要になるということを
学ぶことができました。

moduleのバージョンを指定するのであればawsのプロバイダーのバージョンも指定する。
指定しないのでれば両方共指定しないなど統一感を意識して設計運用していきたいですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0