HTTP API で Lambda オーソライザーを設定した際の問題発生時に確認すべきログを教えてください

2023.10.16

困っていた内容

API Gateway の HTTP API で、Lambda オーソライザーによる認証を設定しました。
テストリクエストを行ったところ{"message":"Internal Server Error"}が発生したので、トラブルシューティングを実施したいです。 まず確認すべきログなどを教えてください。

どう対応すればいいの?

API Gateway のログを確認してください。

Lambda オーソライザーを設定して{"message":"Internal Server Error"}が発生した場合、次の原因が考えられます。

  • Lambda 関数のリソースベースポリシー不足
  • Lambda 関数が所定のレスポンス形式を返却していない

より具体的なエラーの原因は API Gateway のログ($context.authorizer.error)に記載されるため、必要に応じて API Gateway のログを有効にして、ログを確認してください。

エラーの例

# Lambda 関数のリソースベースポリシー不足の場合
The IAM role configured on the authorizer or API Gateway doesn't have permissions to call the authorizer. Check the permissions and try again.

# Lambda 関数が所定のレスポンス形式を返却していない場合
The response from the Lambda Authorizer function doesn't match the format that API Gateway expects. Simple response did not include 'isAuthorized'

ログを設定してみた

前提

API Gateway コンソールを開き、対象の API をクリック

Monitor の Logging を選択

設定するステージを選択して「選択」をクリック

「アクセスのログ」をクリック

次の値を指定して「保存」をクリック

  • ログの送信先:CloudWatch Logs の ARN(例:arn:aws:logs:ap-northeast-1:123456789012:log-group:hato-apigateway-httpapi-log
  • ログの形式:$context.identity.sourceIp - - [$context.requestTime] "$context.httpMethod $context.routeKey $context.protocol" $context.status $context.responseLength $context.requestId $context.authorizer.error

ログの形式は修正可。ただし$context.authorizer.errorを忘れないようにご注意ください。
設定できるパラメータ:HTTP API アクセスログのカスタマイズ - Amazon API Gateway

ログが正常に有効化されたことを確認します。

ログを設定した状態で、リクエストを行うと CloudWatch Logs にトラブルシューティングに必要なログが記録されます。

ログの例

203.0.113.0 - - [10/Oct/2023:01:01:01 +0000] "GET ANY / HTTP/1.1" 500 35 abcdefghijklmno= - Internal Server Error AUTHORIZER_CONFIGURATION_ERROR,The response from the Lambda Authorizer function doesn't match the format that API Gateway expects. Simple response did not include 'isAuthorized'

参考資料

API Gateway が Lambda オーソライザーを呼び出せない場合、または Lambda オーソライザーが無効な形式のレスポンスを返す場合、クライアントは 500 Internal Server Error を受け取ります。