no identity-based policy allows the cognito-idp:ListUsers action
エラーの状況
Amplify とnextjsとcognitoを使ってのプロジェクトを作成。
cognitoのアカウントでログイン実行後、ユーザ一覧を取得しようとして以下のエラーが発生しました。
User: arn:aws:sts::XXXXXXXXXXXX:assumed-role/YYYYYYYYYY-staging-ZZZZZ-authRole/CognitoIdentityCredentials is not authorized to perform: cognito-idp:ListUsers on resource: arn:aws:cognito-idp:ap-northeast-1:XXXXXXXXXXXX:userpool/ap-northeast-1_AAAAAAAAA because no identity-based policy allows the cognito-idp:ListUsers action
原因
IAMロールにcognito-idp:ListUsersの実行権限がなかったことでした。
解決方法
AWS のIAMから、ロールの画面に行き、検索ボックスで上記エラーで表示されたYYYYYYYYYY-staging-ZZZZZ-authRole で検索をします。
出てきたロールを選択して、アクセス権限 > ポリシーの編集 をクリックします。
JSONの画面を選択してから以下の内容を追加します。
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "cognito-idp:ListUsers",
"Resource": "*"
}
意味としては、当該ロールにcognito-idp:ListUsersの操作を許可するという権限を付与しています。
ポリシーの全体としては以下のようになります。
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "appsync:GraphQL",
"Resource": [
~~省略~~
],
"Effect": "Allow"
},
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "cognito-idp:ListUsers",
"Resource": "*"
}
]
}
保存後、再度同じように実行すると、cognitoのユーザ一覧が取得できています。めでたし、めでたし。
cognito で admin グループ等を作成した場合
adminグループのロールが適用されますので、adminでログインした場合のロールを同じように修正する必要があります。