
terraform-aws-iam-identity-centerモジュールを利用している際のInvalid indexエラーの対処方法
はじめに
TerraformfでIAM Identity Centerを構築する際、terraform-aws-iam-identity-centerモジュールを利用することで、冗長的な記述を減らし、より簡潔にIAM Identity Centerのリソースを定義することができます。
terraform-aws-iam-identity-centerモジュールについては次のブログで実際に使ってみた内容が紹介されています。
このモジュールを利用している際、 Error: Invalid index
というエラーによく出会いました。
今回はこのエラーの再現と対処法を紹介します。
エラーの再現
まずはエラーを再現してみます。
以下はユーザーとグループ作成部分を抜粋したコードです。
次のコードでIAM Identity Centerのユーザーとグループを定義します。
sso_users = {
"user1" : {
group_membership = ["Group1",]
user_name = "So.Yonamine"
given_name = "So"
family_name = "Yonamine"
email = "user1@example.com"
}
}
## グループ
sso_groups = {
Group1 : {
group_name = "Group1"
}
上記のコードでterraform plan
を実行すると次のエラーが出力されます。
$ terraform plan
...
│ Error: Invalid index
│
│ on .terraform/modules/aws-iam-identity-center/main.tf line 140, in resource "aws_identitystore_group_membership" "sso_group_membership":
│ 140: member_id = (contains(local.this_users, each.value.user_name) ? aws_identitystore_user.sso_users[each.value.user_name].user_id : data.aws_identitystore_user.existing_sso_users[each.value.user_name].user_id)
│ ├────────────────
│ │ aws_identitystore_user.sso_users is object with 3 attributes
│ │ each.value.user_name is "So.Yonamine"
│
│ The given key does not identify an element in this collection value.
指定したキーがコレクションの中にない。と書かれていますが、この内容だけだと何が原因なのか分かりませんね。
調べてみると、terraform-aws-iam-identity-centerモジュールのドキュメントに注意書きがありました。
- Ensure that the name of your object(s) match the name of your principal(s) (e.g. user name or group name). See the following example with object/principal names 'Admin' and 'nuzumaki':
簡単にいうと「オブジェクト名」と「プリンシパル名」は同じにする必要がある。ということが書かれています。
オブジェクト名とプリンシパル名とはどの部分でしょうか?
先ほどのコードだと以下の部分です。
## ユーザー
sso_users = {
"user_A" : { # オブジェクト名
group_membership = ["Group1", ]
user_name = "Yonamine_So" # プリンシパル名
...
つまりオブジェクト名はuser_Aの部分で、プリンシパル名はuser_nameの部分です。
この二つは一致させる必要があるようです。
それではオブジェクト名とプリンシパル名を同じ値に設定します。
+ "Yonamine_So" : {
group_membership = ["Group1", ]
user_name = "Yonamine_So"
given_name = "So"
family_name = "Yonamine"
email = "userA@example.com"
}
}
## グループ
sso_groups = {
Group1 : {
group_name = "Group1"
}
これでterraform plan
とterraform apply
を実行します。
$ terraform apply
...
# module.aws-iam-identity-center.aws_identitystore_group_membership.sso_group_membership["Yonamine_So_Group1"] will be created
+ resource "aws_identitystore_group_membership" "sso_group_membership" {
...
# module.aws-iam-identity-center.aws_identitystore_user.sso_users["Yonamine_So"] will be created
+ resource "aws_identitystore_user" "sso_users" {
...
}
Plan: 2 to add, 0 to change, 0 to destroy.
...
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
エラーなくデプロイできることを確認できました。
他のリソースも試してみる
上記のエラーはユーザー以外のリソースでも似たエラーが出力されます。
グループのオブジェクト名とプリンシパル名が異なったコードを用意します。
sso_groups = {
Group1 : { # オブジェクト名
group_name = "group1" # プリンシパル名(頭文字を小文字に設定)
}
terraform plan
を実行します。
╷
│ Error: Invalid index
│
│ on .terraform/modules/aws-iam-identity-center/main.tf line 139, in resource "aws_identitystore_group_membership" "sso_group_membership":
│ 139: group_id = (contains(local.this_groups, each.value.group_name) ? aws_identitystore_group.sso_groups[each.value.group_name].group_id : data.aws_identitystore_group.existing_sso_groups[each.value.group_name].group_id)
│ ├────────────────
│ │ data.aws_identitystore_group.existing_sso_groups is object with no attributes
│ │ each.value.group_name is "Group1"
│
│ The given key does not identify an element in this collection value.
同様のエラーが出力されます。
先ほどと同じようにオブジェクト名とプリンシパル名一致させることでデプロイができます。
最後にアカウント割り当て(account_assignments)でも同様のエラーが出ることを確認します。
(account_assignments
はアカウントとグループ、許可セットを結び付けるリソースです。)
account_assignments = {
Demo_Assignment : { # オブジェクト名
principal_name = "assignment" # プリンシパル名
principal_type = "GROUP"
principal_idp = "INTERNAL"
permission_sets = ["AdministratorAccess"]
account_ids = local.admin
}
}
terraform plan
を実行します。
│ Error: Invalid index
│
│ on .terraform/modules/aws-iam-identity-center/main.tf line 244, in resource "aws_ssoadmin_account_assignment" "account_assignment":
│ 244: principal_id = each.value.principal_type == "GROUP" && each.value.principal_idp == "INTERNAL" ? aws_identitystore_group.sso_groups[each.value.principal_name].group_id : (each.value.principal_type == "USER" && each.value.principal_idp == "INTERNAL" ? aws_identitystore_user.sso_users[each.value.principal_name].user_id : (each.value.principal_type == "GROUP" && each.value.principal_idp == "EXTERNAL" ? data.aws_identitystore_group.existing_sso_groups[each.value.principal_name].group_id : (each.value.principal_type == "USER" && each.value.principal_idp == "EXTERNAL" ? data.aws_identitystore_user.existing_sso_users[each.value.principal_name].user_id : (each.value.principal_type == "USER" && each.value.principal_idp == "GOOGLE") ? data.aws_identitystore_user.existing_google_sso_users[each.value.principal_name].user_id : null)))
│ ├────────────────
│ │ aws_identitystore_group.sso_groups is object with 6 attributes
│ │ each.value.principal_name is "Demo_Assignment"
│
│ The given key does not identify an element in this collection value.
同様のエラーが出力されます。
こちらも先ほどと同じようにオブジェクト名とプリンシパル名一致させることでデプロイができます。
まとめ
terraform-aws-iam-identity-center モジュールで Invalid index
エラーに遭遇した際の対処法を紹介しました。
ドキュメントを読むと「重要」という項目に太文字で注意書きがされていましたが、こちらを見落としていて、なんだこのエラーは!?となりました。
モジュールを利用すると便利な反面、エラーが発生した時のトラブルシューティングが難しくなります。まずは落ち着いてドキュメントに目を通すように心がけたいと思います。