AWS announces preview of AWS Interconnect - multicloud
AWS announces AWS Interconnect – multicloud (preview), providing simple, resilient, high-speed private connections to other cloud service providers. AWS Interconnect - multicloud is easy to configure and provides high-speed, resilient connectivity with dedicated bandwidth, enabling customers to interconnect AWS networking services such as AWS Transit Gateway, AWS Cloud WAN, and Amazon VPC to other cloud service providers with ease.
如何在我的 CloudWatch Logs 中尋找 API Gateway REST API 錯誤?
我想在 Amazon CloudWatch Logs 中尋找特定的 Amazon API Gateway REST API 請求失敗。我該如何操作?
解決方案
CloudWatch 提供兩種類型的 API 日誌記錄:存取日誌記錄和執行日誌記錄。如需有關不同類型 API 日誌記錄的詳細資訊,請參閱適用於 API Gateway 的 CloudWatch 日誌格式。
若使用 CloudWatch Logs 尋找特定的 API Gateway REST API 請求失敗,請執行以下動作。
**注意:**如果您在執行 AWS Command Line Interface (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 命令。對 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 回應」的程序進行操作。
