Terraform の Module Registry で使用しているモジュールのバージョンを更新する

言うまでもないかもですが一応ブログ化してみた
2021.11.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ちゃだいん(@chazuke4649)です。

Module Registry のモジュールを利用している環境で、モジュールのバージョン更新をする機会があったので紹介します。 (ドンピシャの記事を見つけられなかったので、書きました)

前提

モジュールレジストリからインストールされたモジュールを使用する場合は、予期しない変更や不要な変更を避けるために、可能なバージョンを明示的に指定することが推奨されています。 以下ドキュメントのサンプルです。

module "consul" {
  source  = "hashicorp/consul/aws"
  version = "0.0.5"

  servers = 3
}

Modules - Configuration Language - Terraform by HashiCorp

やってみた

以下VPCモジュールのバージョンを 3.7.0 から、現時点で最新である 3.10.0 に更新します。

terraform-aws-modules/vpc/aws | Terraform Registry

まず、現時点でModuleブロックは以下のような記述になっています。

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.7.0"

  name                 = "example"
}

この状態で terraform init をすると、特に何もおきません。

% terraform init
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.63.0

Terraform has been successfully initialized!

ちなみに、ここで terraform init -upgaradeを実行すると、すでにダウンロード済みのモジュールであっても再度ダウンロードされます。(例えば本来は新しい子モジュールを呼び出す場合に実行するようです)

% terraform init -upgrade
Upgrading modules...
Downloading terraform-aws-modules/vpc/aws 3.7.0 for vpc...
- vpc in .terraform/modules/vpc

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching ">= 3.38.0, 3.63.0"...
- Using previously-installed hashicorp/aws v3.63.0

Terraform has been successfully initialized!

Moduleブロックのバージョン指定を変更します。

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.10.0"

  name                 = "example"
}

次に terraform plan すると怒られます。

% terraform plan
╷
│ Error: Module version requirements have changed
│
│   on main.tf line 1, in module "vpc":
│    1:   source  = "terraform-aws-modules/vpc/aws"
│
│ The version requirements have changed since this module was installed and
│ the installed version (3.7.0) is no longer acceptable. Run "terraform init"
│ to install all modules required by this configuration.

言われた通りに terraform initを実行します。

% terraform init
Initializing modules...
Downloading terraform-aws-modules/vpc/aws 3.10.0 for vpc...
- vpc in .terraform/modules/vpc

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.63.0

Terraform has been successfully initialized!

これで最新バージョンのモジュールがダウンロードされました。

それではモジュールのバージョン変更によって、定義されるリソースに差異がないか確認します。

% terraform plan
module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-xxxxxxxxxxxxxx]
....

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

上記の通り変更はありませんでした。これによって最新バージョンでサポートされたパラメータや実装があれば、利用することができます。

余談:戻したい場合は?

上記でplanした場合に、万が一変更が多すぎてやっぱり戻したいとなったら、どうすれば良いでしょうか。

(根本解決にはなってないかもですが) 単純にModuleブロックのバージョンを元に戻して(例:3.10.0 -> 3.7.0)、terraform init すれば元の状態に戻れます。

終わりに

それにしても Module Registry便利ですね。感謝しながら活用していきたいです。

それではこの辺で。ちゃだいん(@chazuke4649)でした。