如何在我的 CloudWatch Logs 中查找 API Gateway REST API 错误?

3 分钟阅读
0

我想在我的 Amazon CloudWatch Logs 中找到特定的 Amazon API Gateway REST API 请求失败。该如何操作?

解决方法

CloudWatch 提供两种类型的 API 日志记录:访问日志记录和执行日志记录。有关不同类型的 API 日志记录的更多信息,请参阅 API Gateway 的 CloudWatch 日志格式

要使用 CloudWatch Logs 查找特定 API Gateway REST API 请求失败,请执行以下操作。

注意:如果您在运行 AWS 命令行界面 (AWS CLI) 命令时遇到错误,请确保您运行的是最新的 AWS CLI 版本

使用 CloudWatch 访问日志查找 REST API 请求失败

1.    使用 API Gateway 控制台设置 CloudWatch API 访问日志记录

**注意:**以下示例使用 JSON CloudWatch 日志格式,并包含 $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 命令。使用“grep”搜索实用工具查看 HTTP 状态码或返回的错误消息。

对 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 响应部分中的程序来操作。


AWS 官方
AWS 官方已更新 2 年前