為什麼在使用 CloudFormation 範本或 OpenAPI 定義建立具有 Lambda 整合的 REST API 後,會從 API Gateway 收到 “Execution failed due to configuration(由於設定錯誤,執行失敗)” 的錯誤訊息?

3 分的閱讀內容
0

我使用 AWS CloudFormation 範本 (或 OpenAPI 定義) 來建立具 AWS Lambda 整合的 Amazon API Gateway REST API。當我嘗試使用 API 方法調用 Lambda 函數時,出現以下錯誤訊息和 500 狀態碼: “Execution failed due to configuration.(由於設定錯誤,執行失敗。)” 導致錯誤的原因是什麼?如何解決此問題?

簡短說明

如果您有具 Lambda 整合的 API Gateway REST API,則該 API 必須使用 HTTP 方法 POST 調用後端 Lambda 函數。如果您對後端整合請求使用任何其他 HTTP 方法 (例如,ANYGET),則調用會失敗。接著,API Gateway 會在 Amazon CloudWatch logs 中傳回類似下列範例的錯誤訊息:

Mon Oct 14 14:08:49 UTC 2019 : Received response. Status: 403, Integration latency: 3 ms
Mon Oct 14 14:08:49 UTC 2019 : Endpoint response headers: {Date=Mon, 14 Oct 2019 14:08:49 GMT, Content-Length=130, Connection=keep-alive, x-amzn-RequestId=abc1d2ef-34ab-56c7-de8f-90123a456789}
Mon Oct 14 14:08:49 UTC 2019 : Endpoint response body before transformations: <AccessDeniedException>
<Message>Unable to determine service/operation name to be authorized</Message>
</AccessDeniedException>
Mon Oct 14 14:08:49 UTC 2019 : Lambda invocation failed with status: 403. Lambda request id: abc1d2ef-34ab-56c7-de8f-90123a456789
Mon Oct 14 14:08:49 UTC 2019 : Execution failed due to configuration error:
Mon Oct 14 14:08:49 UTC 2019 : Method completed with status: 500

**注意:**您仍然可以](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-settings-method-request.html#setup-method-add-http-method)為 REST API 的前端[設定任何 HTTP 方法。

如果您使用下列任一方式建立具有 Lambda 整合的 REST API,則必須為後端整合請求指定 POST 方法:

**注意:**如果您使用 API Gateway 主控台設定 Lambda 整合,則後端整合請求會自動設定為 POST

解決方法

CloudFormation 的最佳做法是透過 AWS CloudFormation 管理所有堆疊資源,而不是在 CloudFormation 之外變更堆疊資源。

用來將 HTTP 方法變更為後端整合請求的 POST 取決於原始範本定義。例如,更新使用 CloudFormation 部署的 API Gateway 與使用 OpenAPI 或 AWS CLI 建立的API Gateway 的過程有所不同。

針對使用 CloudFormation 建立之 API Gateway,將整合的請求方法變更為 POST

如需指示,請參閱修改堆疊範本

A.如果您的方法是透過在 CloudFormation 範本中定義 AWS::ApiGateway::Method 資源來建立,請將 HttpMethod 屬性更新為 POST。如需相關指示,請參閱 AWS::ApiGateway::Method 文件的範例一節。

B.如果您的方法包含 OpenAPI 定義作為 AWS::ApiGateway::RestAPI 資源的 Body 屬性的值: 將 API 定義檔案中的 HttpMethod 屬性值設定為 POST。  如需指示,請參閱 x-amazon-apigateway-integration 物件和 aws-samples GitHub 儲存庫上的 Swagger 範本範例

透過使用已編輯的範本執行 AWS CloudFormation 堆疊更新來更新您的 API:

"x-amazon-apigateway-integration" : {
    "type" : "aws",

    "httpMethod" : "POST"

針對在 CloudFormation 外建立的 API Gateway,將後端整合的 HTTP 方法變更為 POST

使用主控台更新方法

  1. API Gateway 主控台,選擇您的 API。
  2. 資源窗格中,選擇具有 Lambda 整合的 HTTP 方法。
  3. 方法執行窗格中,選擇 Integration Request(整合請求)。
  4. 整合請求窗格中,針對 HTTP 方法,按一下 Lambda 函數名稱右側的鉛筆圖示來編輯 Lambda 函數名稱,然後按一下右側顯示的核取記號圖示。
  5. 當系統提示您向 Lambda 函數新增許可時,選擇 OK(確定)。
  6. 部署您的 API
  7. (選做) 測試具有 Lambda 整合的 HTTP 方法

注意:每次在向 Lambda 函數新增許可提示中選取 OK(確定)時,主控台都會向 Lambda 函數的資源政策新增一個新的陳述式。請注意函數的資源政策大小,以確保其未達資源政策大小限制。請參閱: 函數設定、部署和執行

如需詳細資訊,請參閱開始使用 API Gateway使用 API Gateway 主控台設定 API 整合請求

**使用 AWS CLI 更新方法 **

  1. 執行 put-integration 方法,將整合 http 方法更新為 POST:
aws apigateway put-integration \
--rest-api-id 1234123412 \
--resource-id a1b2c3 \
--http-method ANY \
--type AWS \
--integration-http-method POST \
--uri 'arn:aws:apigateway:us-west-2:lambda:path//2015-03-31/functions/arn:aws:lambda:us-east-1:123412341234:function:function_name/invocations
  1. 將為 API 設定的資源部署至現有階段:
aws apigateway create-deployment \
--rest-api-id 1234123412 \
--stage-name dev \
--description 'Deployment to an existing dev stage'

**注意:**如果您在執行 AWS CLI 命令時收到錯誤訊息,請確認您使用的是最新版本的 AWS CLI

更新使用 OpenAPI 定義匯入的方法

  1. 將 API 定義檔案中的 HttpMethod 屬性值設定為 POST。如需指示,請參閱 x-amazon-apigateway-integration 物件和 aws-samples GitHub 儲存庫上的 Swagger 範本範例
  2. 將編輯過的 API 定義檔案匯入 API Gateway 來更新您的 API。請參閱匯入 OpenAPI 檔案以更新現有的 API 定義

相關資訊

AWS::ApiGateway::Method 整合

教學課程: 建立具有兩個 AWS 服務整合和一個 Lambda 非代理整合的 Calc REST API

我使用階段變數在 Amazon API Gateway 中定義了 Lambda 整合。為什麼當我調用 API 方法時,會收到 "Internal server error(內部伺服器錯誤)" 和 500 狀態碼?

如何解決 API Gateway REST API 中的 HTTP 502 "Malformed Lambda proxy response(格式錯誤的 Lambda Proxy回應)" 錯誤?

如何解決 API Gateway 的 "Invalid mapping expression specified(指定的對應運算式無效)" 錯誤?

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