在无服务器应用程序模型中部署多个Lambda函数

4
我希望使用无服务器应用程序模型(sam.yml)文件部署两个带有 API 网关集成的 AWS Lambda 函数。我的文件如下所示:
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 Web控制台中,我可以看到两个函数都已创建,但是我只能通过API Gateway调用其中一个。对于另一个,我得到了{"message":"Gateway timeout"}的错误信息。
有什么想法吗?如何最好地描述在单个无服务器应用程序模型中部署两个函数?
我看过这个示例:https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_backend/template.yaml 然而,这个示例是从同一源代码创建了两个Lambda函数。这不是我想要实现的。我希望在一个sam.yml文件中从不同的部署中创建两个Lambda函数。

2
我认为你的 .yaml 文件和示例文件最明显的区别在于超时键。示例文件没有使用它们。你尝试过将它们删除吗? - Nicholas Martinez
3
我不知道你具体是如何部署你的模板的,但我确信在yaml模板中引用两个不同的Java构件是可能的。以下是一个Node.js和Java同时存在于一个模板中的例子:https://github.com/seeebiii/aws-lambda-boilerplate/tree/master/aws-lambda-node-java-starter(免责声明:我是该仓库的所有者)。根据JAR文件的大小,你应该增加函数的超时时间。也许在你的CloudWatch日志中还有其它提示吗? - s.hesse
是的,你们两个都正确。这是超时引起的问题。一个函数在调用另一个函数。因此,调用函数需要等待两个函数都启动,因此超时时间不足。 - user152468
1个回答

1
我在这里找到了答案here。您需要先设置ApiGatewayApi资源,然后使用ProxyApiRoot和对ApiGatewayApi的引用来更新Events
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: ANY

有没有任何方法可以使用不同的模板来实现相同的功能?例如,我希望能够独立部署我的 Lambda。 - ravi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接