這是完全有效的映射多種方法來一個lambda函數,很多人都在使用今天這個方法,而不是創建每個離散方法的API網關資源和lambda功能。
您可能會考慮將所有請求代理爲單個函數。查看以下有關創建API網關的文檔=> Lambda代理集成: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html
他們的示例在這裏很棒。類似的請求如下:
POST /testStage/hello/world?name=me HTTP/1.1
Host: gy415nuibc.execute-api.us-east-1.amazonaws.com
Content-Type: application/json
headerName: headerValue
{
"a": 1
}
會倒閉以下事件數據發送到您的AWS lambda表達式:
{
"message": "Hello me!",
"input": {
"resource": "/{proxy+}",
"path": "/hello/world",
"httpMethod": "POST",
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"cache-control": "no-cache",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Content-Type": "application/json",
"headerName": "headerValue",
"Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com",
"Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f",
"User-Agent": "PostmanRuntime/2.4.5",
"Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==",
"X-Forwarded-For": "54.240.196.186, 54.182.214.83",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"queryStringParameters": {
"name": "me"
},
"pathParameters": {
"proxy": "hello/world"
},
"stageVariables": {
"stageVariableName": "stageVariableValue"
},
"requestContext": {
"accountId": "12345678912",
"resourceId": "roq9wj",
"stage": "testStage",
"requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"apiKey": null,
"sourceIp": "192.168.196.186",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "PostmanRuntime/2.4.5",
"user": null
},
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "gy415nuibc"
},
"body": "{\r\n\t\"a\": 1\r\n}",
"isBase64Encoded": false
}
}
現在,您可以訪問所有標題,網址參數,車身等和你可以使用它在單個Lambda函數(基本上實現您自己的路由)中以不同的方式處理請求。
作爲意見,我看到這種方法的一些優點和缺點。他們中的許多依賴於你的具體使用情況:
- 部署:如果每個lambda函數是離散的,那麼你可以單獨部署它們,這可能會降低代碼的變化(微服務策略)的風險。相反,您可能會發現需要單獨部署功能會增加複雜性並且很麻煩。
- 自我描述:API網關的界面使其非常直觀地查看RESTful端點的佈局 - 名詞和動詞都一目瞭然。實現自己的路由可能會犧牲這種可見性。
- Lambda的大小和限制:如果您代理全部 - 那麼您將需要選擇實例大小,超時等,以適應所有RESTful端點。如果您創建離散函數,那麼您可以更仔細地選擇最符合特定調用需求的內存佔用空間,超時,死信行爲等。
大答案只是找到了一個link。對我而言,我沒有想到的唯一優勢是:Lambda的大小和限制。不過,我認爲這些優勢勝過這個因素。我也可能將它分成兩個端點來滿足不同類型的請求。 1.使用有限的lambda資源和2個簡單的數據庫查詢。用更強大的資源來計算繁重的功能。 – Chris
當然。 2017年有什麼不錯的是他們已經爲我們提供了所有的選擇,所以我們可以選擇最適合我們需求的工作流程。 –