配列を渡しているのに怒られた!?
結論から言うと、受け取り側の変数で型指定
variable "hoge" { type = list(string) }
等が必要となる。
「terragrunt→terraform」の段階で型情報が失われる事から、配列(list, set)や連想配列(map)を受け取っても判断出来ないとのこと。
プラクティス
ディレクトリ構成
.
├── envs
│ ├── dev
│ │ ├── app
│ │ │ ├── 〇〇.tf
│ │ │ └── terragrunt.hcl
│ │ ├── ecs
│ │ ├── log
│ │ ├── network
│ │ ├── security_group
│ │ └── terragrunt.hcl
│ └── prod
├── modules
│ ├── app
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
envs/app/terragrunt.hcl
include {
path = find_in_parent_folders()
}
dependency "network" {
config_path = "../network"
}
inputs = {
subnet_ids = [
dependency.network.outputs.private_subnet_a_id,
dependency.network.outputs.private_subnet_c_id
]
}
modules/app/variables.tf ※ダメな例
variable subnet_ids {}
こんな具合で、渡したら以下のようなエラーが出てきます
consoleの結果
│ Error: Incorrect attribute value type
│
│ on main.tf line 99, in resource "aws_db_subnet_group" "db_subnet_group":
│ 99: subnet_ids = var.subnet_ids
│ ├────────────────
│ │ var.subnet_ids is "[\"subnet-xxxxxx\",\"subnet-xxxxxx\"]"
│
│ Inappropriate value for attribute "subnet_ids": set of string required.
「いやいや、配列じゃん!」ってなりますよね、なお、ダブルクオーテーションでくくった形subnet_ids = ["${dependency.network.outputs.private_subnet_a_id}"]にしても無駄です。同様のエラーになります。
modules/app/variables.tf ※良い例
variable subnet_ids { type = set(string) }
受け取り側で明示的に型定義をすることで無事、通ります。連想配列のときはmap(string)などを使いましょう。
terraformの型に関しては以下の記事がよくまとまっているので、参考にしてください。