メンチカツには醤油でしょ!!

AWS/Java/Node.js/Spreadsheets/Docker/Jenkins/コミュニティ・勉強会レポを主とした技術系ブログ

AWS API Gatewayにカスタムオーソライザーを設定してみたら、判りにくい事があった

1. オーソライザーの設定方法

f:id:ryoichi0102:20180218203159p:plain

この位置…なんかわかりにくい…

2. Execution failed due to configuration error: Invalid JSON in response

戻りのJSONが良くないのですが、どう良くないかって話ですよね。

https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/use-custom-authorizer.html#api-gateway-custom-authorizer-output

https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html#api-gateway-calling-api-permissions

とりあえず動かす場合は、

callback(null, {
  "principalId": "ユーザーを特定できる文字列なら何でも良し",
  "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "execute-api:*",
        "Effect": "Allow",
        // "Resource": "arn:aws:execute-api:ap-northeast-1:000000000000:yourApiGatewayId:*"
        "Resource": "*"
      }
    ]
  },
  "context": {
    "stringKey": "stringval",
    "numberKey": 123,
    "booleanKey": true
  }
});
}

contextにDynamo.getの戻り値(data)をセットするとうまくいかないです!!

"context": data

これハマりました。
(多分だけどJSON.stringify(data)すればいけるんだろうな…と後で思った)

policyDocumentのstatementにはIAMの指定とかではなく、IAMポリシーを作成してそれを指定するわけでもないんですね。

3. カスタムオーソライザーが呼び出されない

コンソール上のテストでは呼び出されるのに、Postmanやcurlだと呼び出されない。

⇒サイドメニュー"リソース" クリック
⇒カスタムオーソライザーを適用したいメソッドをクリック
⇒認証にて、作成したカスタムオーソライザーを指定

全部に自動的に効くわけじゃないんですね。。。面倒。

4. Invalid authorizer ID specified. Setting the authorization type to CUSTOM or COGNITO_USER_POOLS requires a valid authorizer.

画面リフレッシュしたら設定できることもあるかも知れません。
自分の場合は、オーソライザーを同じ名前で作り直したので、裏でそれが更新されていなかったのかも。

5. "Message": "User is not authorized to access this resource" が返ってくる

policyDocumentはちゃんと返してると思ったんですが、

"Resource": "*"

にしたら通りました。
少しづつ狭めて確認していきましょう。