LoginSignup
1

More than 3 years have passed since last update.

CognitoをAWS SDK for JavaScriptから使ったらドハマリした

Posted at

やりたかったこと

EC2をブラウザから起動・停止したかった。

Cognitoでアクセス権限を生成し、AWS SDK for JavaScriptを使ってできると思っていた。

実際にやったこと

公式ドキュメントにあった、ブラウザから非認証ユーザーでPollyを使うチュートリアルを行った。

その後Pollyにアクセスする部分をEC2に変えてとりあえずdescribeInstancesを実行してみた。

起こったこと(ハマったこと)

UnauthorizedOperationが発生。

"UnauthorizedOperation: You are not authorized to perform this operation.

権限を過大に付けても駄目。

実行に権限が不要なsts:getCallerIdentityを実行するもこちらは普通に動作。

チュートリアルのPollyに変えてみても普通に動作。この状態でPollyの権限を外すともちろんエラー。

訳が分からず小一時間ほど格闘…。

原因

公式ドキュメント(日本語版は訳がわかりにくいので英語版)のアクセスポリシーの項目に次のようにありました。

In other words, this policy allows an identity using these credentials with access to only the following services:

AWS AppSync
Amazon Comprehend
GameLift
Amazon Lex
Amazon Polly
Amazon SimpleDB
Amazon Transcribe
CloudWatch
DynamoDB
AWS IoT
Amazon Machine Learning
Amazon Rekognition
Amazon SES
Amazon Translate
Amazon Cognito Identity
Amazon Kinesis Data Firehose
AWS KMS Amazon Mobile Analytics
Amazon Sumerian
Amazon SNS
Amazon Cognito Sync
Amazon Kinesis Data Streams
AWS Lambda
Amazon Pinpoint
Amazon S3
Amazon SQS

さらにこうあります。

If you need access to something other than these services for your unauthenticated users, you must use the basic authentication flow. If you are getting NotAuthorizedException and you have enabled access to the service in your unauthenticated role policy, this is likely the reason.

…つまり上記以外のサービスにアクセスしたい時は未認証ユーザーではなく何かで認証しないといけないとのこと。

ゆくゆくは認証処理も入れようと思っていましたが、開発中だしとりあえず未認証のまま進めようとしたばっかりにドハマリしてしまった。

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