moduleとは?
インフラ構成管理ツールのTerraformで共通処理を記述するファイル群
モジュールを呼び出すコード
module "リソースの名前" { source = "モジュールを定義したフォルダのパス" パラメーター名 = "パラメーター値" }
moduleの構造
- 入力:variable
- 本体:resource
- 出力:output
実際のファイルは以下
sample.tf
variable "content" { } variable "filename" { } resource "local_file" "helloworld" { content = var.content filename = var.filename } output "debug_print" { value = "${var.content} to ${var.filename}" }
パラメータを利用する
resource内の変数 content、filenameは、入力パラメータとしてvariableに定義したものを設定している。
moduleを呼び出す親ファイルmain.tfなどで、この入力パラメータを設定する。
resource "local_file" "helloworld" { content = var.content filename = var.filename }
moduleの結果を出力する
contentに指定された内容をfilenameのファイルに出力する
output "debug_print" { value = "${var.content} to ${var.filename}" }
terraformの実行
以下で構成ファイルを実行、実際の環境に適用することが可能
terraform plan terraform apply