跳至內容

我該如何疑難排解 API Gateway 的 Lambda 授權者錯誤?

2 分的閱讀內容
0

我將 AWS Lambda 授權者附加到 Amazon API Gateway API。我想疑難排解在 API Gateway 調用請求中收到的 Lambda 授權者錯誤。

解決方法

確認您的調用請求會到達 Lambda 函式

使用 Amazon CloudWatch 檢視您的 Invocations metric (調用指標)。如果該指標顯示有值,則您的調用請求會到達 Lambda 函式。

如果 Invocations (調用) 指標沒有顯示調用,請檢查 Throttles (限流) 指標,確認是否有調用請求遭到限流。若要疑難排解 Lambda 限流,請參閱 我該如何疑難排解 Lambda 函式「Rate exceeded」與「TooManyRequestsException」限流錯誤?

檢視 API Gateway 執行日誌以識別您的錯誤

若要識別 Lambda 授權者錯誤,請開啟 CloudWatch API logging (CloudWatch API 記錄),並檢視日誌。接著,依您收到的錯誤訊息疑難排解問題。

解決「Invalid permissions on Lambda function」錯誤

如果您收到以下錯誤訊息,則 API Gateway API 沒有權限調用 Lambda 函式:

「Execution failed due to configuration error: Invalid permissions on Lambda function」

當您在 API Gateway 主控台建立 Lambda 授權者時,API Gateway 會自動將必要權限新增到 Lambda 函式的資源型政策。如果您使用基礎結構即程式碼 (IaC) 工具 (例如 AWS CloudFormation 或 AWS Cloud Development Kit (AWS CDK)) 設定 Lambda 授權者,則您必須手動新增權限。

將以下權限新增到 Lambda 函式的資源型政策:

{
  "Version": "2012-10-17",
  "Id": "default",
  "Statement": [
    {
      "Sid": "xyz",
      "Effect": "Allow",
      "Principal": {
        "Service": "apigateway.amazonaws.com"
      },
      "Action": "lambda:InvokeFunction",
      "Resource": "arn:aws:lambda:your-region:accountID:function:function-name",
      "Condition": {
        "ArnLike": {
          "AWS:SourceArn": "arn:aws:execute-api:your-region:accountID:API_ID/authorizers/AuthorizerID"
        }
      }
    }
  ]
}

**注意事項:**將 your-region 替換為您的 AWS 區域,將 accountID 替換為您的 AWS 帳戶 ID,並將 function-name 替換為您的 Lambda 函式名稱。此外,將 API_ID 替換為您的 API Gateway API ID,並將 AuthorizerID 替換為您的 Lambda 授權者 ID。

若要設定跨帳戶 Lambda 授權者,請參閱設定跨帳戶 API Gateway Lambda 授權者。

解決「Unable to parse given policy」組態錯誤

如果您收到以下錯誤訊息,表示您的授權 Lambda 函式有問題:

「Execution failed due to configuration error: Unable to parse given policy」

若要解決此問題,請完成以下步驟:

  1. 開啟 Lambda console (Lambda 主控台)。
  2. 在導覽窗格中,選擇 Functions (函式),然後選取您的授權 Lambda 函式。
  3. 檢視您函式的 Code source (程式碼來源)。確認您的程式碼正確無誤。<br id=hardline_break/> **注意事項:**如需有效範例,請參閱 REQUEST 授權者 Lambda 函式範例與 TOKEN 授權者 Lambda 函式範例。
  4. 若要確認您的 Lambda 函式可以正常運作,請測試函式。

疑難排解「AuthorizationFailureException」錯誤

如果您收到以下錯誤訊息,表示您的 Lambda 授權者可能有問題:

「Execution failed due to an authorizer error Tue Jan 01 04:59:40 UTC 2020 : AuthorizerFailureException」

若要疑難排解此問題,請檢查 CloudWatch 日誌,查看 Lambda 函式中的授權失敗、程式碼語法錯誤或逾時問題。

疑難排解「500 Internal Server Error」錯誤

如果您在調用 API Gateway HTTP API 時收到「500 Internal Server Error」錯誤訊息,請參閱疑難排解 Lambda 授權者。

**注意事項:**如果您的 Lambda 授權者在 10 秒內沒有回應,您也會收到此錯誤訊息。

AWS 官方已更新 1 年前