1 回答

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
是否可以使用單個(gè) lambda 公開(kāi) 4 個(gè) api。
AWS lambda 是FaaS
- 函數(shù)即服務(wù),每個(gè) Lambda 一個(gè)函數(shù)。
但是,您可以使用包裝器/代理函數(shù)作為入口點(diǎn)來(lái)實(shí)現(xiàn)預(yù)期的功能,并根據(jù)需要將請(qǐng)求路由到上游方法/函數(shù)
它在這里描述aws api gateway & lambda: multiple endpoint/functions vs single endpoint
查看以下有關(guān)創(chuàng)建 API 網(wǎng)關(guān) => Lambda 代理集成的文檔:http : //docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html
以下只是對(duì)aws api gateway & lambda: multiple endpoint/functions vs single endpoint 中給出的內(nèi)容的重新措辭解釋。
AWS 例子有很好的解釋?zhuān)蝗缦滤镜?Lambda 請(qǐng)求:
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 函數(shù)發(fā)送以下事件數(shù)據(jù):
{
"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
}
}
現(xiàn)在您可以訪問(wèn)所有標(biāo)頭、url 參數(shù)、正文等。因此,您可以使用它在包裝器/代理 Lambda 函數(shù)中以不同方式處理請(qǐng)求,并根據(jù)您的路由需求路由到上游函數(shù)。
今天很多人都在使用這種方法,而不是為每個(gè)方法和 api 網(wǎng)關(guān)資源創(chuàng)建 lambda 函數(shù)。
這種方法有利有弊
部署:如果每個(gè) lambda 函數(shù)都是離散的,那么您可以獨(dú)立部署它們,這可能會(huì)降低代碼更改的風(fēng)險(xiǎn)(微服務(wù)策略)。相反,您可能會(huì)發(fā)現(xiàn)需要單獨(dú)部署功能會(huì)增加復(fù)雜性和負(fù)擔(dān)。
自我描述:API Gateway 的界面讓您可以非常直觀地查看 RESTful 端點(diǎn)的布局——名詞和動(dòng)詞都一目了然。實(shí)現(xiàn)您自己的路由可能會(huì)以這種可見(jiàn)性為代價(jià)。
Lambda 大小和限制:如果您代理所有——那么您最終需要選擇一個(gè)實(shí)例大小、超時(shí)等,以適應(yīng)您的所有 RESTful 端點(diǎn)。如果您創(chuàng)建離散函數(shù),那么您可以更仔細(xì)地選擇最能滿(mǎn)足特定調(diào)用需求的內(nèi)存占用、超時(shí)、死信行為等。
更多關(guān)于Monolithic lambdavsMicro Lambda在這里:https : //hackernoon.com/aws-lambda-should-you-have-few-monolithic-functions-or-many-single-purposed-functions-8c3872d4338f
添加回答
舉報(bào)