1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

terragruntで配列や連想配列をinputしているのに「Inappropriate value for attribute "変数名": set of string required.」と怒られてしまう場合の対処

1
Last updated at Posted at 2024-04-24

配列を渡しているのに怒られた!?

結論から言うと、受け取り側の変数で型指定
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の型に関しては以下の記事がよくまとまっているので、参考にしてください。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?