AWS SAM中能否配置多Lambda处理不同POST请求?配置是否正确?
问题
我正在使用Visual Studio 2022构建AWS Serverless Application Model (SAM)应用,通过Functions.cs定义多个Lambda函数供外部Web应用调用。想请教两个问题:
- 是否可以定义多个Lambda函数来处理不同类型的POST请求?
- 是不是每个HTTP动词(如POST、GET、PUT等)只能对应一个函数?
以下是我的serverless.template文件片段(敏感信息已模糊处理),里面声明了两个POST函数,请问这样的定义是否正确,是否需要修改?
"ListCertificates": { "Type": "AWS::Serverless::Function", "Properties": { "Architectures": [ "x86_64" ], "Handler": "xxx.xxx.xxx.POC.AWS::xxx.xxx.xxx.POC.AWS.Functions::Post", "Runtime": "dotnet6", "CodeUri": "", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaBasicExecutionRole" ], "Environment": { "Variables": { "POWERTOOLS_SERVICE_NAME": "CertificateManagement", "POWERTOOLS_LOG_LEVEL": "Info", "POWERTOOLS_LOGGER_CASE": "PascalCase", "POWERTOOLS_TRACER_CAPTURE_RESPONSE": true, "POWERTOOLS_TRACER_CAPTURE_ERROR": true, "POWERTOOLS_METRICS_NAMESPACE": "ListCertificatesFunction" } }, "Events": { "RootPost": { "Type": "Api", "Properties": { "Path": "/", "Method": "POST" } } } } }, "UploadCertificate": { "Type": "AWS::Serverless::Function", "Properties": { "Architectures": [ "x86_64" ], "Handler": "xxx.xxx.xxx.POC.AWS::xxx.xxx.xxx.POC.AWS.Functions::Post", "Runtime": "dotnet6", "CodeUri": "", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaBasicExecutionRole" ], "Environment": { "Variables": { "POWERTOOLS_SERVICE_NAME": "CertificateManagement", "POWERTOOLS_LOG_LEVEL": "Info", "POWERTOOLS_LOGGER_CASE": "PascalCase", "POWERTOOLS_TRACER_CAPTURE_RESPONSE": true, "POWERTOOLS_TRACER_CAPTURE_ERROR": true, "POWERTOOLS_METRICS_NAMESPACE": "UploadCertificateFunction" } }, "Events": { "RootPost": { "Type": "Api", "Properties": { "Path": "/", "Method": "POST" } } } } }
回答
完全可以用多个Lambda函数处理不同类型的POST请求,HTTP动词和Lambda函数的数量没有绑定限制,你可以根据业务逻辑拆分多个函数分别处理不同场景的POST请求。
当前的模板定义存在问题,需要修改:
- 两个函数都绑定到了同一个路径
/和POST方法,API Gateway无法区分请求应该转发给哪个函数,会导致路由冲突,部署后可能出现随机触发其中一个函数或者报错的情况。 - 另外两个函数的Handler都指向同一个
Functions::Post方法,就算路由没问题,触发后执行的也是同一段逻辑,达不到区分处理的目的。
- 两个函数都绑定到了同一个路径
修改建议:
- 区分路由路径:给两个函数设置不同的API路径,符合RESTful设计,比如:
- ListCertificates函数绑定路径
/list-certificates,POST方法 - UploadCertificate函数绑定路径
/upload-certificate,POST方法
- ListCertificates函数绑定路径
- 拆分处理方法:在Functions.cs中分别定义两个不同的处理方法,比如
PostListCertificates和PostUploadCertificate,然后对应修改每个函数的Handler配置。
- 区分路由路径:给两个函数设置不同的API路径,符合RESTful设计,比如:
修正后的模板片段示例:
"ListCertificates": { "Type": "AWS::Serverless::Function", "Properties": { "Architectures": [ "x86_64" ], "Handler": "xxx.xxx.xxx.POC.AWS::xxx.xxx.xxx.POC.AWS.Functions::PostListCertificates", "Runtime": "dotnet6", "CodeUri": "", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaBasicExecutionRole" ], "Environment": { "Variables": { "POWERTOOLS_SERVICE_NAME": "CertificateManagement", "POWERTOOLS_LOG_LEVEL": "Info", "POWERTOOLS_LOGGER_CASE": "PascalCase", "POWERTOOLS_TRACER_CAPTURE_RESPONSE": true, "POWERTOOLS_TRACER_CAPTURE_ERROR": true, "POWERTOOLS_METRICS_NAMESPACE": "ListCertificatesFunction" } }, "Events": { "ListCertificatesPost": { "Type": "Api", "Properties": { "Path": "/list-certificates", "Method": "POST" } } } } }, "UploadCertificate": { "Type": "AWS::Serverless::Function", "Properties": { "Architectures": [ "x86_64" ], "Handler": "xxx.xxx.xxx.POC.AWS::xxx.xxx.xxx.POC.AWS.Functions::PostUploadCertificate", "Runtime": "dotnet6", "CodeUri": "", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaBasicExecutionRole" ], "Environment": { "Variables": { "POWERTOOLS_SERVICE_NAME": "CertificateManagement", "POWERTOOLS_LOG_LEVEL": "Info", "POWERTOOLS_LOGGER_CASE": "PascalCase", "POWERTOOLS_TRACER_CAPTURE_RESPONSE": true, "POWERTOOLS_TRACER_CAPTURE_ERROR": true, "POWERTOOLS_METRICS_NAMESPACE": "UploadCertificateFunction" } }, "Events": { "UploadCertificatePost": { "Type": "Api", "Properties": { "Path": "/upload-certificate", "Method": "POST" } } } } }
内容的提问来源于stack exchange,提问作者CB_at_Sw
相关产品推荐
相关产品推荐

