
Terraform 1.13で追加された terraform stacks コマンド使ってみた
Terraform 1.13 が GA(General Availability)になりました。今回はアップグレード内容のひとつ、terraform stacks
コマンドをご紹介します。
terraform stacks
コマンド
Stack操作用のコマンド群です。
Stackについてまだご存じない方は以下をご確認ください。ざっくり言うと複数のworkspaceをまとめる機能で、apply適用を一括で行えたり、workspace間の依存関係を定義したりできる機能です。
今回そのStack操作用のコマンドが誕生、ということで「お、ついに HCP Terraform(SaaS版)だけでなくコミュニティ版でもStack使えるようになったのか!?」と思ったのですが、そうではないようです。依然HCP Terraform独自の機能です。残念。
さらに、今回(version 1.13)でStack機能がGAになったわけでもないです。現在は依然Public Betaです。1.13版の公式ドキュメントに以下記述がありました。
Public Beta: Stacks are in public beta. Once we generally release them, we do not guarantee backward compatibility support for the public beta. All APIs, workflows, and HCL syntax for Stacks are subject to change.
とまぁ以上の前提のうえで、terraform stacks
コマンドがどんなものなのか見ていきましょう。
The available subcommands depend on the stacks plugin implementation
とあります。 stacks pluginってなんだ。
また、
Use
terraform stacks -usage
to see available commands.
とあるので、1.13.0をインストールした後にまずは実行してみましょう。
# インストール
% tfenv install 1.13.0 # 私は Terraformのversion管理に tfenvを使っています。
% tfenv use 1.13.0
% terraform stacks -usage
Usage: terraform stacks [global options] <command> [args]
The available commands for execution are listed below.
Primary Commands:
init Prepare the configuration directory for further commands
providers-lock Write out dependency locks for the configured providers
validate Check whether the configuration is valid
version Show the current Stacks Plugin version
fmt Reformat your Terraform Stacks configuration to a canonical format
Sub-commands:
Global options (use these before the subcommand, if any):
-chdir=DIR Switch to a different working directory before executing the
given subcommand.
-plugin-cache-dir=DIR Override the default directory where the stack plugin binary is cached.
-stacks-version An alias for the "version" subcommand.
-no-color=BOOL Disable color output. Must be explicitly set to true or false.
Usage help:
-usage Show this usage output or the usage for a specified subcommand.
Use after the command: terraform stacks <command> -usage
うーん Sub-commands:
以下が空になっているように見えますが、これは私が「stacks plugin」を何も入れてないからなんでしょうか。
Primary Commands
に関しては、どうやら既存の tfstacks
(terraform-stacks-cli) コマンド、これは terraform
コマンドとは別物として提供されているStack操作用のコマンド群があったのですが、これと同等+アルファのコマンド群って感じのようです。 tfstacks
のサブコマンド群に同じようなものが並んでいるので。
というわけで各サブコマンドを触ってみます。
terraform stacks init
コマンド
以下のように .terraform
ディレクトリと ロックファイル(.terraform.lock.hcl
)を作るコマンドのようです。イメージはstack版のterraform init
ですね。
% ls -a
. ..
% terraform stacks init
Success! Configuration has been initialized and more commands can now be executed.
% ls -a
. .. .terraform .terraform.lock.hcl
ロックファイルの中身は以下になっていました。
# This file is maintained automatically by "terraform stacks providers lock".
# Manual edits may be lost in future updates.
また、以前 Stackのチュートリアルをやった際のコードが私のGitHubリポジトリ上に残っていたのでそれをgit clone
してからコマンドを実行すると、以下の様に providerのダウンロードも実行されました。
% terraform stacks init
Provider "hashicorp/random" already locked at "3.5.1".
Provider "hashicorp/archive" already locked at "2.4.2".
Provider "hashicorp/local" already locked at "2.4.1".
Provider "hashicorp/aws" already locked at "5.72.1".
Downloading "hashicorp/archive"@"2.4.2"....
Downloading "hashicorp/aws"@"5.72.1"....
Downloading "hashicorp/local"@"2.4.1"....
Downloading "hashicorp/random"@"3.5.1"....
Success! Configuration has been initialized and more commands can now be executed.
terraform stacks providers-lock
コマンド
terraform stacks init
コマンド項で少し出てきましたが、ロックファイルを生成、更新するコマンドのようです。 terraform providers lock
のStack版ですね。
% terraform stacks providers-lock
Provider "hashicorp/random" already locked at "3.5.1".
Provider "hashicorp/archive" already locked at "2.4.2".
Provider "hashicorp/local" already locked at "2.4.1".
Provider "hashicorp/aws" already locked at "5.72.1".
Downloading "hashicorp/archive"@"2.4.2"....
Downloading "hashicorp/aws"@"5.72.1"....
Downloading "hashicorp/local"@"2.4.1"....
Downloading "hashicorp/random"@"3.5.1"....
Success! .terraform.lock.hcl has been created and/or updated.
terraform providers lock
同様 -platform
オプションも使えました。
% terraform stacks providers-lock -platform=linux_arm64
Provider "hashicorp/local" already locked at "2.4.1".
Provider "hashicorp/aws" already locked at "5.72.1".
Provider "hashicorp/random" already locked at "3.5.1".
Provider "hashicorp/archive" already locked at "2.4.2".
Downloading "hashicorp/archive"@"2.4.2"....
Downloading "hashicorp/aws"@"5.72.1"....
Downloading "hashicorp/local"@"2.4.1"....
Downloading "hashicorp/random"@"3.5.1"....
Success! .terraform.lock.hcl has been created and/or updated.
terraform stacks version
コマンド
% terraform stacks version
Terraform Stacks Plugin version: v1.0.0
on darwin_arm64
stacks pluginのversionは v1.0.0 だそうです。terraform 1.13.0のバイナリに内蔵されているんですかね。
少し前の項にて「Sub-commands:
以下が空になっているように見えますが、これは私が「stacks plugin」を何も入れてないからなんでしょうか。」と書きましたが、pluginは入っているんですね。v1.0.0時点では sub commandsが無いということのようです。
terraform stacks validate
コマンド
これも terraform validate
のStack版ですね。
先ほどと同様、以前Stackのチュートリアルで使用したコードに対して実行しました。
% terraform stacks validate
╷
│ Warning: Deprecated filename usage
│
│ This configuration is using the deprecated .tfstack.hcl or .tfstack.json file extensions. This will not be supported in a future version of Terraform, please update your files to use the latest .tfcomponent.hcl or .tfcomponent.json file extensions.
╵
Success! Terraform Stacks configuration is valid and ready for use within HCP Terraform.
Some warnings were produced by the validation process, and are rendered above.
ほー、ファイル名変わるんですね。
また、validateできるか確認するために、必須項目のcomponentブロックを全削除した状態で terraform stacks validate
を実行してみました。
% terraform stacks validate
╷
│ Warning: Deprecated filename usage
│
│ This configuration is using the deprecated .tfstack.hcl or .tfstack.json file extensions. This will not be supported in a future version of Terraform, please update your files to use the latest .tfcomponent.hcl or .tfcomponent.json file extensions.
╵
╷
│ Error: Reference to undeclared component
│
│ on ./outputs.tfstack.hcl line 4, in output "lambda_urls":
│ 4: value = [ for x in component.api_gateway: x.hello_url ]
│
│ There is no component "api_gateway" block declared in this stack.
╵
Failure! Terraform Stacks configuration is not valid, please check and fix the errors printed above.
狙い通り Failを返しましたが、「必須項目のcomponentブロックが無い」エラーではなく「componentのoutputを参照している部分があるが、そのcomponentが無い」なんですね…
ためしに「componentのoutputを参照している部分」も削除したらvalidate成功しました。うーむ。
terraform stacks fmt
コマンド
これも terraform fmt
のStack版ですね。コードをベストプラクティスにそってフォーマット(整形)してくれます。
% terraform stacks fmt
components.tfstack.hcl
deployments.tfdeploy.hcl
outputs.tfstack.hcl
providers.tfstack.hcl
variables.tfstack.hcl
例えば以下のようにインデントを正してくれました。(outputs.tfstack.hcl
)
output "lambda_urls" {
- type = list(string)
+ type = list(string)
description = "URLs to invoke lambda functions"
- value = [ for x in component.api_gateway: x.hello_url ]
+ value = [for x in component.api_gateway : x.hello_url]
}