3
votes

I would like to use a serverless application model (sam.yml) file to deploy two AWS lambda functions with api gateway integration. My file looks like this:

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

Within the lambda web console, I can see both functions created, yet I can only call one of them via the API Gateway. For the other I get {"message":"Gateway timeout"}.

Any ideas? What is the best way to describe two function deployments within a single serverless application model?

I have seen this example: https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_backend/template.yaml

Yet this creates two lambda functions from the same source code. This is not what I am trying to achieve. I would like to create two lambda functions from different deployments within a single sam.yml file.

1
I think the most telling difference between your .yaml and the example .yaml are the timeout keys. The example does not use them. Have you tried removing them? - Nicholas Martinez
I don't know how exactly you're deploying your template, but I'm pretty sure it's possible to have two different Java artifacts referenced in the yaml template. Example for Nodejs and Java in one template: github.com/seeebiii/aws-lambda-boilerplate/tree/master/… (disclaimer: I'm the owner of the repo) Depending on the JAR size you should increase the timeout for your functions. Is there maybe also a hint in your CloudWatch Logs? - s.hesse
Yes, indeed both of you are correct. It was the timeout. One function was calling the other. So the calling function needed to wait for both functions to start up, therefore the timeout was not sufficient. - user152468

1 Answers

0
votes

I found answer here. You need to set first ApiGatewayApi resource, and then update Events with ProxyApiRoot and reference to 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: ANY