Techfirm Cloud Architect Blog

テックファーム株式会社クラウドインフラグループのブログ

TerraformでAWS Providerをバージョンアップ

はじめに

Terraformで環境の追加構築をしていたところ、AWS Providerのバージョンが原因でエラーになったので、バージョンアップを実施しました。

この記事では、そのエラー内容からバージョンアップ手順までを紹介します。

前提

すでにAWSの環境構築は行っており、そこにAWS Batchの環境を追加しようとしていました。
この環境では、1年ほど前にTerraformおよびAWS Providerのバージョンは設定しており、バージョンアップはしていない状態でした。

バージョンアップ前のterraform設定がこちらです。

terraform {
  required_version = ">= 1.1"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.9"
    }
  }
}

問題点

新たにAWS Batchと関連するリソースの作成コードを追加し、確認のためにterraform planを実行したところ、以下のエラーが発生しました。

│ Error: Unsupported block type
│
│   on test_batch.tf line 251, in resource "aws_batch_job_queue" "test_batch_queue":
│  251:   compute_environment_order {
│
│ Blocks of type "compute_environment_order" are not expected here.

Unsupported block typeとありますが、compute_environment_orderというブロックは公式ドキュメントに記載がありましたので、書き方が間違っているというわけでは無さそうでした。

原因

エラーの原因は、使っているAWS Providerのバージョンが古いことでした。

以前の環境構築時にterraform initを実行し、その時点で最新のAWS Providerを入れていましたが、それ以降更新していなかったため古くなっていたようです。

aws_batch_job_queuecompute_environment_orderブロックは、AWS Providerのバージョン5.40.0で追加されたことが、TerraformのCHANGELOGから分かりました。

~前略~
5.40.0 (March 7, 2024)
~中略~
ENHANCEMENTS:
~中略~
resource/aws_batch_job_queue: added parameter compute_environment_order which conflicts with compute_environments but aligns with AWS API. compute_environments has been deprecated. (#34750)

現在使用しているAWS Providerのバージョンは以下のコマンドで分かります。

# terraform version
Terraform v1.6.2
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v5.26.0

バージョン5.26.0を使っていたことが分かりました。まだ存在しないはずのブロックを書いてしまったため、エラーが出ていた、という事象でした。

解決策

解決策として、AWS Providerのバージョンアップを行います。
今回は指定バージョンへ上げる手順を確認したかったため、最新ではなく、バージョン5.40.0を指定してみます。

Terraformのバージョン設定部分を修正します。

terraform {
  required_version = ">= 1.1"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "= 5.40.0"
    }
  }
}

修正内容を保存し、terraformコマンドでバージョンアップします。(terraform init -upgrade

# terraform init -upgrade

Initializing the backend...

Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding hashicorp/aws versions matching "5.40.0"...
- Installing hashicorp/aws v5.40.0...
- Installed hashicorp/aws v5.40.0 (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.

無事にアップデート完了しましたので、再度バージョン確認してみます。

# terraform version
Terraform v1.6.2
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v5.40.0

AWS Providerのバージョンが5.40.0になっていることが確認できました。

最後にterraform planを実行して、エラーなく実行できることを確認しました。

今回のバージョンアップでは、テンプレートを変更しなくてもterraform planが問題なく実行できました。
プロバイダの変更点によっては、terraform planが意図した結果になるようにテンプレートの修正が必要になる場合もあります。

以上でバージョンアップは完了となります。

おわりに

AWS Providerを古いバージョンで使い続けていると、新しい機能を使うためのバージョンアップが必要というケースがありますので、この記事が役に立ちましたら幸いです。

参考

  1. Resource: aws_batch_job_queue(Terraformドキュメント) https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/batch_job_queue
  2. terraform-provider-aws/CHANGELOG.md https://github.com/hashicorp/terraform-provider-aws/blob/main/CHANGELOG.md
  3. Lock and upgrade provider versions(Terraformドキュメント) https://developer.hashicorp.com/terraform/tutorials/cli/provider-versioning