CloudWatch Logs で API Gateway REST API エラーを見つけるにはどうすればよいですか?

所要時間3分
0

Amazon CloudWatch Logs で特定の Amazon API Gateway REST API リクエストの失敗を見つけたいと考えています。どうすればそれができますか?

解決方法

CloudWatch には、アクセスログと実行ログの 2 種類の API ログがあります。API ログの各タイプの詳細については、「API Gateway の CloudWatch Logs 形式」を参照してください。

CloudWatch Logs を使用して特定の API Gateway REST API リクエストの失敗を確認するには、次の手順を実行します。

注: AWS コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーが発生した場合は、AWS CLI の最新バージョンを使用していることを確認してください

CloudWatch アクセスログを使用して REST API リクエストの失敗を検出するには

1.    API ゲートウェイコンソールを使用して CloudWatch API アクセスログ記録を設定します

注: 次の例では、JSON CloudWatch Logs 形式を使用し、$context.error.message コンテキストのメッセージフィールドを含めます。

2.    任意の検索ユーティリティを使用して、API Gateway アクセスログで filter-log-events AWS CLI コマンドを実行します。

API Gateway アクセスログに「grep」検索ユーティリティを使用する filter-log-events コマンドの例

重要: log-group-name をアクセスロググループの名前に変更します。

aws logs filter-log-events --log-group-name 'API-Gateway-Access-Logs_2mg2xeej0a/test' | grep '17cb49b2-c061-11e9-bc30-f118c8b08d5f'

3.    filter-log-events コマンドに対する応答をチェックして、エラーを特定します。コマンドに対する応答は、マップしたすべての $context 変数を含むオブジェクトを返します。

filter-log-events コマンドに対する応答の例

{
    "requestId": "17cb49b2-c061-11e9-bc30-f118c8b08d5f",
    "ip": "1.2.3.4",
    "caller": "-",
    "user": "-",
    "requestTime": "28/Aug/2019:15:10:34 +0000",
    "httpMethod": "GET",
    "resourcePath": "/iam",
    "status": "403",
    "message": "Missing Authentication
            Token",
    "protocol": "HTTP/1.1",
    "responseLength": "42"
}

CloudWatch 実行ログを使用して REST API リクエストの失敗を検出するには

1.    API Gateway コンソールを使用して CloudWatch API 実行ログを設定します

重要: 機密情報を保存しないようにするには、本番環境に移行する前に、[リクエスト/レスポンスデータを完全にログ記録する] をオフにしてください。

2.    次の cURL コマンドを実行して、失敗した API リクエストをレプリケートします。

重要: abcd1234 をユーザーの API ID に置き換え、us-east-1 をお使いの API がある AWS リージョンに置き換えます。

curl -X GET https://abcd1234.execute-api.us-east-1.amazonaws.com/dev/myapi -v

3.    API レスポンスヘッダーの x-amzn-requestid 値をメモします。この値は、次のステップで必要になります。

API Gateway のレスポンスヘッダーの例

< content-type: application/json
< content-length: 41
< x-amzn-requestid: 17cb49b2-c061-11e9-bc30-f118c8b08d5f
< x-amz-apigw-id: eh7enHGIvHcFnPg=

4.    任意の検索ユーティリティを使用して、API Gateway 実行ログで filter-log-events AWS CLI コマンドを実行します。

API Gateway 実行ログに「grep」検索ユーティリティを使用する filter-log-events コマンドの例

重要: log-group-name をアクセスロググループの名前に変更します。

aws logs filter-log-events --log-group-name 'API-Gateway-Execution-Logs_2mg2xeej0a/test' | grep '17cb49b2-c061-11e9-bc30-f118c8b08d5f'

5.    filter-log-events コマンドに対する応答をチェックして、エラーを特定します。以下に、表示されるエラーメッセージのタイプの例を示します。

API Gateway のアクセス許可エラーの例

(b59c91c8-3386-4478-b2b4-c4d63191756e) Execution failed due to configuration error: Invalid permissions on Lambda function
(b59c91c8-3386-4478-b2b4-c4d63191756e) Gateway response type: DEFAULT_5XX with status code: 500
(b59c91c8-3386-4478-b2b4-c4d63191756e) Gateway response body: {"message": "Internal server error"}

API Gateway 統合タイムアウトエラーの例

(2a1db04d-ac7c-463f-b9bd-478285467d58) Execution failed due to a timeout error
(2a1db04d-ac7c-463f-b9bd-478285467d58) Gateway response type: DEFAULT_5XX with status code: 504
(2a1db04d-ac7c-463f-b9bd-478285467d58) Gateway response body: {"message": "Endpoint request timed out"}

API Gateway バックエンド統合エラーの例

(631ff793-2c5b-413d-a31d-f2cd3f75708b) Endpoint response body before transformations: {"errorMessage": "division by zero", "errorType": "ZeroDivisionError", "stackTrace": [" File \"/var/task/lambda_function.py\", line 7, in lambda_handler\n c=a/b\n"]}
(631ff793-2c5b-413d-a31d-f2cd3f75708b) Lambda execution failed with status 200 due to customer function error: division by zero. Lambda request id: 7f0d8fc1-33ce-4bf9-8685-df78e179da5c
(631ff793-2c5b-413d-a31d-f2cd3f75708b) Gateway response type: DEFAULT_5XX with status code: 502
(631ff793-2c5b-413d-a31d-f2cd3f75708b) Gateway response body: {"message": "Internal server error"}

リクエスト ID を取得できない場合

1.    クライアント側のアプリケーションログから HTTP ステータスコードまたは返されたエラーメッセージのいずれかを取得します。

2.    API Gateway アクセスログで filter-log-events AWS CLI コマンドを実行します。HTTP ステータスコードまたは返されたエラーメッセージに対して、「grep」検索ユーティリティを使用します。

HTTP ステータスコードに「grep」検索ユーティリティを使用する filter-log-events コマンドの例

aws logs filter-log-events --log-group-name 'API-Gateway-Access-Logs_2mg2xeej0a/test' | grep '504'

クライアント側から返されたエラーメッセージに対して「grep」検索ユーティリティを使用する filter-log-events コマンドの例

aws logs filter-log-events --log-group-name 'API-Gateway-Access-Logs_2mg2xeej0a/test' | grep 'Endpoint request timed out'

3.    アクセスログの結果でリクエスト ID を探します。

4.    この記事の実行ログで API 応答を追跡するセクションの手順に従います。


コメントはありません

関連するコンテンツ