以前こんな事を言っていたが、実際に調べたらnestしたobject型も機能したのでその訂正1。
terraformのmoduleを作成するようになって知った6つのことのメモ - podhmo's diary
moduleのvariableのtypeにはnestしたobjectのような複雑な型が書けること
これは、世に存在するmoduleの定義や例がstringやnumberやboolなどのプリミティブな型で受け取ることものがほとんどであったので、勝手にvariableで受け取る引数の型に制限があると思っていた
outputには制限があったはず
これが誤りだった。
詳細
実際に検証してみる。先に実行結果。configというoutputはnestしたobject
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
config = {
"person" = {
"age" = 20
"name" = "foo"
}
}
nestしたobjectを問題なくoutputに指定できている。
作ったのは以下の様なファイル
main.tf
locals {
person = {
name = "foo"
age = 20
}
}
output "config" {
value = {
person = local.person
}
}
こちらはなくても良いが。
default: terraform init terraform validate terraform apply -auto-approve
remote_state
ついでにremote_stateを使った場合も動くか調べる。めんどくさいのでlocal backendを使う。ファイルの階層は以下のような感じ。subディレクトリが増えただけ。
$ tree . |--Makefile |--main.tf |--sub | |--main.tf | |--terraform.tfstate |--terraform.tfstate
こちらも先に実行結果。先程指定したoutputを利用して "hello
$ cd sub $ terraform apply ... Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: message = "hello foo!!"
sub/main.tf
data "terraform_remote_state" "root" {
backend = "local"
config = {
path = "../terraform.tfstate"
}
}
output "message" {
value = "hello ${data.terraform_remote_state.root.outputs.config.person.name}!!"
}
gist
- root https://gist.github.com/podhmo/bad4112143ee95556edaf3b17d296ac2
- sub https://gist.github.com/podhmo/b4b4d5fcb2cca00eb43f81e6be4a8cf2
-
記憶が確かならprimitiveな値以外ではエラーになったはずなのだけれど、remote_stateでもそうでない場合でも問題なくnestしたobjectの値が使えた↩