LoginSignup
0
0

AppSync GraphQL APIでIAMユーザー権限による操作がエラーとなる件の対応

Last updated at Posted at 2024-04-01

概要

AppSync GraphQL APIのv2 TransformerでIAMユーザー権限による操作が Not Authorized to access XXX on type Mutation のようなエラーで弾かれてしまいます。その対応方法について記載します。

原因

AWS ConsoleのAppSyncの「関数」で [xxxx]auth0Function の中身を見るとわかるとおり、 #if( $util.authType() == "IAM Authorization" ) の場合、 #foreach( $adminRole in $ctx.stash.adminRoles ) の中で #if( $ctx.identity.userArn.contains($adminRole) && $ctx.identity.userArn != $ctx.stash.authRole && $ctx.identity.userArn != $ctx.stash.unauthRole ) の判定が行われています。
権限を持っているLambdaからのアクセスの場合、 $ctx.stash.adminRoles にLambdaの名前が入るため条件を通りますが、IAMユーザーによるアクセスの場合、この条件判定で弾かれるためアクセスできません。

対応方法

amplify/backend/api/[apiName]/custom-roles.json を作成し、中に

{
  "adminRoleNames": ["arn:aws:iam::[AccountId]:user"]
}

と書いてデプロイします。
このようにすることで、 $ctx.stash.adminRolesarn:aws:iam::[AccountId]:user を含めることができます。実際に設定された $ctx.stash.adminRoles はリゾルバのテンプレートを見ることで確認できます。下記のようになっているはずです。

$util.qr($ctx.stash.put("adminRoles", ["権限を持っているLambdaの名前","arn:aws:iam::[AccountId]:user"]))

これにより #if( $ctx.identity.userArn.contains($adminRole) ... ) 条件を通り、IAMユーザーでもAppSync GraphQL APIを実行することができるようになります。

参考

Clarity Request: Unexpected "Not Authorized" with IAM and Transformer v2

バージョン情報

% amplify -v
12.10.1
0
0
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
0
0