问题:我是否必须将多个 lambda 表达式(每个功能一个)附加到这个 codestar 项目?
答:是的
AWS CodeStar 项目详情:
AWS Code star 项目包含以下文件结构(reference link):
README.md - this file
buildspec.yml - this file is used by AWS CodeBuild to package your service for deployment to AWS Lambda
app.js - this file contains the sample Node.js code for the web service
index.js - this file contains the AWS Lambda handler code
template.yml - this file contains the Serverless Application Model (SAM) used by AWS Cloudformation to deploy your service to AWS Lambda and Amazon API Gateway.
假设你有如下的 template.yml 文件:
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar
Resources:
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Handler: index.first_handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /first
Method: get
HelloWorld2:
Type: AWS::Serverless::Function
Properties:
Handler: index.second_handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /second
Method: get
请注意,在上面的 tamplate.yml 文件中指定了“HelloWorld”和“HelloWorld2”配置。
- HelloWorld 配置包含“Handler”值为“index.first_handler”,这意味着“index”是 index.js 的文件名,first_handler 是 index.js 文件中的方法。
- 同样,HelloWorld2 配置包含“Handler”值为“index.second_handler”,这意味着“index”是 index.js 的文件名,second_handler 是 index.js 文件中的方法。
结论:
您可以在 index.js (whatever.js) 文件中指定任意数量的 lambda 函数。只需指定适当的 Handler 来识别您的 lambda 函数的应用程序。
希望这是您问题的答案。如有疑问,请随时提出疑问!