我想使用一个无服务器应用程序模型(sam.yml)文件来部署两个AWS函数,并进行api网关集成。我的档案是这样的:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: "Client Service"
Resources:
PetStoreServerFunction:
Type: AWS::Serverless::Function
Properties:
Handler: server.LambdaHandler::handleRequest
Runtime: java8
CodeUri: petstore-server/target/petstore-server-1.0-SNAPSHOT.jar
MemorySize: 512
Policies:
- AWSLambdaBasicExecutionRole
Timeout: 20
Events:
GetResource:
Type: Api
Properties:
Path: /server/{proxy+}
Method: any
PetStoreClientFunction:
Type: AWS::Serverless::Function
Properties:
Handler: client.LambdaHandler::handleRequest
Runtime: java8
CodeUri: petstore-client/target/petstore-client-1.0-SNAPSHOT.jar
MemorySize: 512
Policies:
- AWSLambdaBasicExecutionRole
Timeout: 20
Events:
GetResource:
Type: Api
Properties:
Path: /client/{proxy+}
Method: any在lambda控制台中,我可以看到创建了两个函数,但我只能通过API网关调用其中一个函数。另一方面,我得到了{"message":"Gateway timeout"}。
有什么想法吗?在单个无服务器应用程序模型中描述两个功能部署的最佳方法是什么?
我看过这个例子:后端/模板
然而,这将从相同的源代码中创建两个lambda函数。这不是我想要达到的目标。我想在一个sam.yml文件中从不同的部署中创建两个lambda函数。
发布于 2021-06-17 13:19:05
我找到答案了,这里。您需要首先设置 ApiGatewayApi 资源,然后使用ProxyApiRoot更新Events并引用ApiGatewayApi。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: /lambda1
Handler: index.handler
Runtime: nodejs12.x
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /lambda1
Method: ANY
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: /lambda2
Handler: index.handler
Runtime: nodejs12.x
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /lambda2
Method: ANY
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: /lambda3
Handler: index.handler
Runtime: nodejs12.x
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /lambda3
Method: ANYhttps://stackoverflow.com/questions/48303130
复制相似问题