LoginSignup
3
0

More than 3 years have passed since last update.

TerraformのModuleを理解するための雑なメモ

Posted at

これはなんですか

  • Terraform の Module を調べたときに理解しづらかったことを垂れ流しておきます。

実行ディレクトリの .tf はルートモジュール

実は、普段 terraform plan, terraform apply を実行するときに作る .tf 一式も ルートモジュール というモジュールである。モジュールだからこそ、通常のモジュールと同様に variableoutput で入出力を持たせることができる。これはQiitaやブログだけ見てTerraformを設定していると見逃しがちである。

Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory.

ルートモジュールは他の子モジュールとは以下の点が異なる。
- variable の値を terraform コマンドやtfvarsファイルで渡せる
- output の値を terraform applyterraform refresh で表示できる

variable は値渡しのための記法

モジュールを作るとき、variable ブロックでは default 属性を設定せず、値を空のままにすることが多い。

variable "name" {} # this is the input parameter of the module

普段何気なくルートモジュールで variable を書くときは default 属性で値を埋め込んでいる。しかし、Terraformの実行ファイルをモジュールの集まりと考えれば、「 variable の値は空で問題なく、default は入力が空の場合のデフォルト値を入れるための設定」という考え方が自然になる。ルートモジュールでわざわざ default を書くのも、コマンドから値を渡すのは面倒だからあらかじめデフォルト値を設定している、と考えられる。

ドキュメントにも、variable が「Input Variables」すなわち入力変数であることが明記されている。

Input variables serve as parameters for a Terraform module, allowing aspects of the module to be customized without altering the module's own source code, and allowing modules to be shared between different configurations.

入力変数はTerraformモジュールのパラメーターとして機能し、モジュールのソースコードを変更せずにモジュールの側面をカスタマイズしたり、異なる構成間でモジュールを共有したりできます。(Google翻訳)

これらのことから、 「variable はモジュールへの値渡しのために用意された要素」であり、「ルートモジュールにおける variable の書き方は (極端な話) 特別な例」であると言える。

3
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
3
0