使用 Lambda 代理整合與開放API定義來設定代理資源 - Amazon API 网关

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 Lambda 代理整合與開放API定義來設定代理資源

若要使用 Lambda Proxy 整合類型設定代理API資源,請使用貪婪路徑參數 (例如/parent/{proxy+}) 建立資源,然後將此資源與ANY方法上的 Lambda 函數後端 (例如arn:aws:lambda:us-west-2:123456789012:function:SimpleLambda4ProxyResource) 整合。貪婪路徑參數必須位於資API源路徑的末尾。與非 Proxy 資源一樣,您可以使用 API Gateway 主控台、匯入開啟API定義檔或RESTAPI直接呼叫API閘道來設定 Proxy 資源。

下列開啟APIAPI定義檔案顯示了具API有代理資源的範例,該資源與名為的 Lambda 函數整合SimpleLambda4ProxyResource

OpenAPI 3.0
{ "openapi": "3.0.0", "info": { "version": "2016-09-12T17:50:37Z", "title": "ProxyIntegrationWithLambda" }, "paths": { "/{proxy+}": { "x-amazon-apigateway-any-method": { "parameters": [ { "name": "proxy", "in": "path", "required": true, "schema": { "type": "string" } } ], "responses": {}, "x-amazon-apigateway-integration": { "responses": { "default": { "statusCode": "200" } }, "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:SimpleLambda4ProxyResource/invocations", "passthroughBehavior": "when_no_match", "httpMethod": "POST", "cacheNamespace": "roq9wj", "cacheKeyParameters": [ "method.request.path.proxy" ], "type": "aws_proxy" } } } }, "servers": [ { "url": "https://gy415nuibc.execute-api.us-east-1.amazonaws.com/{basePath}", "variables": { "basePath": { "default": "/testStage" } } } ] }
OpenAPI 2.0
{ "swagger": "2.0", "info": { "version": "2016-09-12T17:50:37Z", "title": "ProxyIntegrationWithLambda" }, "host": "gy415nuibc.execute-api.us-east-1.amazonaws.com", "basePath": "/testStage", "schemes": [ "https" ], "paths": { "/{proxy+}": { "x-amazon-apigateway-any-method": { "produces": [ "application/json" ], "parameters": [ { "name": "proxy", "in": "path", "required": true, "type": "string" } ], "responses": {}, "x-amazon-apigateway-integration": { "responses": { "default": { "statusCode": "200" } }, "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:SimpleLambda4ProxyResource/invocations", "passthroughBehavior": "when_no_match", "httpMethod": "POST", "cacheNamespace": "roq9wj", "cacheKeyParameters": [ "method.request.path.proxy" ], "type": "aws_proxy" } } } } }

在 Lambda 代理整合中,在執行階段,API閘道會將傳入的要求對應至 Lambda 函event數的輸入參數。輸入包含請求方法、路徑、標頭、任何查詢字串參數、任何承載、相關聯內容和任何已定義的階段變數。「代理整合之 Lambda 函數的輸入格式」說明輸入格式。若要讓API閘道將 Lambda 輸出成功對HTTP應至回應,Lambda 函數必須以中所述的格式輸出結果代理整合之 Lambda 函數的輸出格式

在透過 ANY 方法之代理資源的 Lambda 代理整合中,單一後端 Lambda 函數透過代理資源作為所有請求的事件處理常式。例如,若要記錄流量模式,您可以讓行動裝置傳送其州、城市、街道和建築物的位置資訊,方法是/state/city/street/house在 Proxy 資源的URL路徑中提交要求。接著,後端 Lambda 函數就可以剖析URL路徑,並將位置元組插入 DynamoDB 資料表中。