aws-serverless-samfarm icon indicating copy to clipboard operation
aws-serverless-samfarm copied to clipboard

Multiple Lambda functions from the sam.yml file possible?

Open joelfogue opened this issue 7 years ago • 37 comments

I have a situation where I have multiple lambda functions within our API Gateway's API. I'm new to AWS and learning as much as I can. I am wondering, how would I change the template yaml (sam.yaml) so that we can deploy to any number of lambda functions and not just one as is the case in this lab. Would you create multiple index.js file per lambda function?

  • so index1.js -> will map to first lambda function
  • index2.js -> second lambda function;
  • etc....

And how do I update the sam.yaml file to reflect this change?

Here's a snippet code of what I'm trying to accomplish:

Resources:
  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs4.3
      Role:
        Fn::ImportValue:
          !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
        PostEvent:
          Type: Api
          Properties:
            Path: /
            Method: post
.... and more endpoints here to hit more lambda functions? So do I create multiple index.js to map to each individual lambda function?

joelfogue avatar Feb 16 '18 19:02 joelfogue

Guys,

I have the same question.

Really trying to learn serverless and questions like these - on best practices - is making it super difficult. Would love some direction here.

andrewryan1906 avatar Mar 23 '18 09:03 andrewryan1906

any help

dfloresgonz avatar Apr 04 '18 21:04 dfloresgonz

As far as I am aware, you can have multiple JS files, each containing a handler to a different Lambda. The files don't have to be in the same directory! You can also define the 2nd Lambda function in the same SAM template yaml file where you defined the 1st Lambda function.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs the time

Resources:
  TimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: firstsample/firstsample.handler   # firstsample.js file is in firstsample direcotory
      Role: !GetAtt BasicAWSLambdaRole.Arn
      Runtime: nodejs6.10
      CodeUri: ./                          # Look for code in the same directory as the output SAM file
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TimeResource
            Method: GET
  
  SecondSampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: secondsample.handler                  # didn't have to include secondsample directory
      Role: !GetAtt BasicAWSLambdaRole.Arn
      Runtime: nodejs6.10
      CodeUri: ./secondsample           # code is in the secondsample directory, located in same directory
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TextResource
            Method: GET

jairoVera avatar May 03 '18 16:05 jairoVera

This is the top google result for "sam template multiple functions". Be nice to have airoVera's solution in the official docs if thats the way we should be doing this.

And how does this work with aws cloudformation package and aws cloudformation deploy ? Do I need to run that for each lambda/CodeUri?

red8888 avatar Sep 18 '18 20:09 red8888

@red8888 what I do is having my repo structure as below. I used Jenkins pipeline to do 'npm install' for my lambda functions, FYR.

├── 00_DEVOPS-test1 │   ├── index.js │   ├── package.json │   ├── package-lock.json │   └── README.md ├── 00_DEVOPS-test2 │   ├── index.js │   ├── package.json │   ├── package-lock.json │   └── README.md ├── Jenkinsfile ├── packaged.yaml ├── README.md └── template.yaml

joeyshub avatar Jan 17 '19 14:01 joeyshub

@red8888, using the @jairoVera approach, as both AWS::Serverless::Function are on the same template only one sam package | aws cloudformation package must be executed, same for the deploy.

Regarding the first code if you have a main.js that check the origin of the request and it can route it Post to index-1.js and the Get to index-2.js using the main.js handler and directing to 2 "subhandlers"

hugoprudente avatar Jan 25 '19 15:01 hugoprudente

@jairoVera do you know if that approach will generate 1 or 2 API Gateways?

deleugpn avatar Jan 30 '19 16:01 deleugpn

It will generate two separate API gateways but the issue is (at least when generating a testing environment locally) if you create separate functions that have separate handler files both of the handler files will be written into the build folder for BOTH Lambda functions. I'm not sure if this would happen when deploying though.

capture

sam-mundle avatar Feb 20 '19 21:02 sam-mundle

It seems SAM is intended for one lambda per application. For multi-function applications, there is Nested application method.

sertaco avatar Feb 25 '19 22:02 sertaco

SAM does support multiple functions, the right way to do it is with the sam build command which pulls deps for all functions and creates a package that will be deployed as a "serverless application"

Maybe someone from the SAM team can reiterate this?

red8888 avatar Mar 06 '19 22:03 red8888

@deleugpn it will generate two API Gateways, but you can add the API Gateway resource and It will create just one

Transform: AWS::Serverless-2016-10-31
Description: Outputs the time

Resources:
  ApiResource:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod  
  
  TimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: firstsample/firstsample.handler   # firstsample.js file is in firstsample direcotory
      Role: !GetAtt BasicAWSLambdaRole.Arn
      Runtime: nodejs6.10
      CodeUri: ./                          # Look for code in the same directory as the output SAM file
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TimeResource
            Method: GET
            RestApiId: !Ref ApiResource
  
  SecondSampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: secondsample.handler                  # didn't have to include secondsample directory
      Role: !GetAtt BasicAWSLambdaRole.Arn
      Runtime: nodejs6.10
      CodeUri: ./secondsample           # code is in the secondsample directory, located in same directory
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TextResource
            Method: GET
            RestApiId: !Ref ApiResource```

estebansolo avatar Mar 29 '19 20:03 estebansolo

And how about shared libraries and code?

MatteoGioioso avatar Aug 14 '19 14:08 MatteoGioioso

You can use Layers

estebansolo avatar Aug 14 '19 14:08 estebansolo

@jairoVera is life saver, I was banging on this problem for a week.

I have 13 serverless functions having go1.x and python run times and dependent to each others, previously I was managing the code bases and aws services manually, it was so pain, accidentally I discovered sam and I introduced one template file for all the functions but build sam was unable to find the code bases.

I specified CodeUri where my codes points.

Now I can easily debug locally, build and deploy in one click. :-)

debendraoli avatar Mar 17 '20 15:03 debendraoli

@jairoVera is life saver, I was banging on this problem for a week.

I have 13 serverless functions having go1.x and python run times and dependent to each others, previously I was managing the code bases and aws services manually, it was so pain, accidentally I discovered sam and I introduced one template file for all the functions but build sam was unable to find the code bases. Now I can easily debug locally, build and deploy in one click. :-)

So works or no? "but build sam was unable to find the code bases. Now I can easily debug locally, build and deploy in one click"

javiortizmol avatar May 08 '20 16:05 javiortizmol

@jairoVera is life saver, I was banging on this problem for a week. I have 13 serverless functions having go1.x and python run times and dependent to each others, previously I was managing the code bases and aws services manually, it was so pain, accidentally I discovered sam and I introduced one template file for all the functions but build sam was unable to find the code bases. Now I can easily debug locally, build and deploy in one click. :-)

So works or no? "but build sam was unable to find the code bases. Now I can easily debug locally, build and deploy in one click"

I already told.

"Now I can easily debug locally, build and deploy in one click. :-)"

debendraoli avatar Jul 02 '20 09:07 debendraoli

I have a different question. How do I have 1 lambda function with different events (different http methods) I don't want to create several lambda functions for each method its not interesting and not efficient. I want to be able trigger that function with either with boolean logic inside the actual script or even better edit this serverless.yml file so I can do that from yaml rather than inside the script Please help me, will much appreciate that

gavetisyanca avatar Aug 09 '20 03:08 gavetisyanca

It will generate two separate API gateways but the issue is (at least when generating a testing environment locally) if you create separate functions that have separate handler files both of the handler files will be written into the build folder for BOTH Lambda functions. I'm not sure if this would happen when deploying though.

capture

Yes, it happens when you deploy it. You can see it in the AWS Lambda console. Were you (or someone) able to isolate the functions handlers when deploying?

In my case, when running sam build, it executes the npm install for each function (Running NodejsNpmBuilder:NpmInstall) and it takes a lot of time. This is the main issue I am trying to avoid.

sromano88-svc avatar Oct 23 '20 09:10 sromano88-svc

I have a different question. How do I have 1 lambda function with different events (different http methods) I don't want to create several lambda functions for each method its not interesting and not efficient. I want to be able trigger that function with either with boolean logic inside the actual script or even better edit this serverless.yml file so I can do that from yaml rather than inside the script Please help me, will much appreciate that

This is what I do:

EcommerceFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: ecommerce-lambda
      Handler: src/handlers/ecommerceHandler.handler
      Runtime: nodejs12.x
      Description: Products Lambda function.
      Policies:
        - AWSLambdaBasicExecutionRole
        - AmazonDynamoDBFullAccess
      Environment:
        Variables:
          LAMBDA_ENVIRONMENT: local
      Events:
        # Server
        GetServiceRunningAPI:
          Type: Api
          Properties:
            Path: /api/products-health-check
            Method: GET
        # Products
        GetProductsAPI:
          Type: Api
          Properties:
            Path: /api/products
            Method: GET
        GetSingleProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: GET
        PostProductsAPI:
          Type: Api
          Properties:
            Path: /api/products
            Method: POST
        PutProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: PUT
        DeleteProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: DELETE

sromano88-svc avatar Oct 23 '20 09:10 sromano88-svc

Hi, how does this work with java maven reactor build, is every module considered a lambda function? Is a deployment package generated from the root module contains all lambda on one single jar?

emaayan avatar Nov 29 '20 12:11 emaayan

This is what I do:

...
        PutProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: PUT
        DeleteProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: DELETE

Only example I've been able to find where multiple http methods are specified from the same endpoint. Thank you!

jesstucker avatar Dec 11 '20 22:12 jesstucker

This is what I do:

...
        PutProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: PUT
        DeleteProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: DELETE

Only example I've been able to find where multiple http methods are specified from the same endpoint. Thank you!

The million dollar question, how do you do it multiple paths and methods for HTTP APIs?

afern247 avatar Jan 17 '21 13:01 afern247

I have a different question. How do I have 1 lambda function with different events (different http methods) I don't want to create several lambda functions for each method its not interesting and not efficient. I want to be able trigger that function with either with boolean logic inside the actual script or even better edit this serverless.yml file so I can do that from yaml rather than inside the script Please help me, will much appreciate that

This is what I do:

EcommerceFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: ecommerce-lambda
      Handler: src/handlers/ecommerceHandler.handler
      Runtime: nodejs12.x
      Description: Products Lambda function.
      Policies:
        - AWSLambdaBasicExecutionRole
        - AmazonDynamoDBFullAccess
      Environment:
        Variables:
          LAMBDA_ENVIRONMENT: local
      Events:
        # Server
        GetServiceRunningAPI:
          Type: Api
          Properties:
            Path: /api/products-health-check
            Method: GET
        # Products
        GetProductsAPI:
          Type: Api
          Properties:
            Path: /api/products
            Method: GET
        GetSingleProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: GET
        PostProductsAPI:
          Type: Api
          Properties:
            Path: /api/products
            Method: POST
        PutProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: PUT
        DeleteProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: DELETE

This is the way

estebansolo avatar Jan 19 '21 21:01 estebansolo

one handler for all of the products API?

keithics avatar Feb 20 '21 11:02 keithics

one handler for all of the products API?

Yes, I had that approach initially (one handler for all of the products API). Because on develop building several functions for each change was not possible. Right now I have moved 1 function per API endpoint.

Comment 1: Right now (Feb 2021 - and a couple of versions before), sam build has a --cached and --parallel parameters that helps to build faster

Comment 2: We have moved to typescript with a webpack solution, so real hot-reload is achieved even without building (sam build)

sromano88-svc avatar Feb 21 '21 09:02 sromano88-svc

@sromano88-svc @estebansolo That looks so so nice!

iongion avatar Jun 02 '21 15:06 iongion

one handler for all of the products API?

Yes, I had that approach initially (one handler for all of the products API). Because on develop building several functions for each change was not possible. Right now I have moved 1 function per API endpoint.

Comment 1: Right now (Feb 2021 - and a couple of versions before), sam build has a --cached and --parallel parameters that helps to build faster

Comment 2: We have moved to typescript with a webpack solution, so real hot-reload is achieved even without building (sam build)

How do you do that ? What is your file-system structure ? I have webpack too and plan to use typescript

iongion avatar Jun 02 '21 16:06 iongion

From https://github.com/aws-samples/aws-serverless-samfarm/issues/5#issuecomment-763156337

I have a different question. How do I have 1 lambda function with different events (different http methods) I don't want to create several lambda functions for each method its not interesting and not efficient. I want to be able trigger that function with either with boolean logic inside the actual script or even better edit this serverless.yml file so I can do that from yaml rather than inside the script Please help me, will much appreciate that

This is what I do:

EcommerceFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: ecommerce-lambda
      Handler: src/handlers/ecommerceHandler.handler
      Runtime: nodejs12.x
      Description: Products Lambda function.
      Policies:
        - AWSLambdaBasicExecutionRole
        - AmazonDynamoDBFullAccess
      Environment:
        Variables:
          LAMBDA_ENVIRONMENT: local
      Events:
        # Server
        GetServiceRunningAPI:
          Type: Api
          Properties:
            Path: /api/products-health-check
            Method: GET
        # Products
        GetProductsAPI:
          Type: Api
          Properties:
            Path: /api/products
            Method: GET
        GetSingleProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: GET
        PostProductsAPI:
          Type: Api
          Properties:
            Path: /api/products
            Method: POST
        PutProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: PUT
        DeleteProductsAPI:
          Type: Api
          Properties:
            Path: /api/products/{productId}
            Method: DELETE

This is the way

If we need multiple languages on the same API path (we have a library which only exists in python, but our code is otherwise kotlin, and we need time to port the library to jvm), is there a way to do so?

Or would we be forced to use a separate API in this case?

(also in general I'm curious if there's a way to use the --use-container flag with sam build for heterogeneous runtimes without specifying a custom docker image)

hughesjj avatar Jun 14 '21 21:06 hughesjj

@hughesjj This example shows how you can use the same lambda function (also language) with different endpoints.

If you want to use the same api with different endpoints and every of them with a different language, you can do it easily, check a previous comment where there are two functions with the same api, all you have to do is change the runtime and do what you need to do

estebansolo avatar Jun 14 '21 21:06 estebansolo

@jairoVera's solution worked for me I just created two resource in same templete.yml file. here is my folder structure But node dependency will be heavy worried about optimization in build process.

Root Dir
  api1 - api1 folder
      index.js - handler file
     node_modules/
 api2 - api2 folder
    index.js - handler file
    node_modules/
template.yml file

** yml file **

Globals:
  Function:
    Timeout: 100
    AutoPublishAlias: live
    DeploymentPreference:
      Enabled: true
      Type: Canary10Percent5Minutes
      Role: !Ref CodeDeployRole

Resources:
  api1:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub <Fn name>
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: api1/
      Environment:
        Variables:
          NODE_ENV: ""
          MONGO_URI: ""
      Role:
        Fn::GetAtt:
          - LambdaExecutionRole
          - Arn
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
  api2:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub <Fn Name>
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: api2/
      Environment:
        Variables:
          NODE_ENV: ""
          MONGO_URI: ""
      Role:
        Fn::GetAtt:
          - LambdaExecutionRole
          - Arn
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /get-user
            Method: get

ninjasujan avatar Jun 16 '21 05:06 ninjasujan