Giter VIP home page Giter VIP logo

Comments (37)

jairoVera avatar jairoVera commented on July 17, 2024 99

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

from aws-serverless-samfarm.

andrewryan1906 avatar andrewryan1906 commented on July 17, 2024 25

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.

from aws-serverless-samfarm.

estebansolo avatar estebansolo commented on July 17, 2024 20

@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```

from aws-serverless-samfarm.

red8888 avatar red8888 commented on July 17, 2024 19

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?

from aws-serverless-samfarm.

sertaco avatar sertaco commented on July 17, 2024 17

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

from aws-serverless-samfarm.

sromano88-svc avatar sromano88-svc commented on July 17, 2024 12

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

from aws-serverless-samfarm.

dfloresgonz avatar dfloresgonz commented on July 17, 2024 6

any help

from aws-serverless-samfarm.

estebansolo avatar estebansolo commented on July 17, 2024 3

You can use Layers

from aws-serverless-samfarm.

joeyshub avatar joeyshub commented on July 17, 2024 2

@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

from aws-serverless-samfarm.

red8888 avatar red8888 commented on July 17, 2024 2

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?

from aws-serverless-samfarm.

debendraoli avatar debendraoli commented on July 17, 2024 2

@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. :-)

from aws-serverless-samfarm.

hugoprudente avatar hugoprudente commented on July 17, 2024 1

@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"

from aws-serverless-samfarm.

sam-mundle avatar sam-mundle commented on July 17, 2024 1

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

from aws-serverless-samfarm.

javiortizmol avatar javiortizmol commented on July 17, 2024 1

@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"

from aws-serverless-samfarm.

estebansolo avatar estebansolo commented on July 17, 2024 1

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

from aws-serverless-samfarm.

jmactibold avatar jmactibold commented on July 17, 2024 1

Autant que je sache, vous pouvez avoir plusieurs fichiers JS, chacun contenant un gestionnaire pour un Lambda différent. Les fichiers ne doivent pas nécessairement se trouver dans le même répertoire ! Vous pouvez également définir la 2ème fonction Lambda dans le même fichier yaml de modèle SAM où vous avez défini la 1ère fonction Lambda.

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

Thank you very much for the answer. Do you know if it is possible to host the deployment packages of all the lambdas on a remote repository (Github for example) so that the CodeURI property in the yaml file for each resource retrieves the content it needs to build the lambda? Thanks in advance

from aws-serverless-samfarm.

deleugpn avatar deleugpn commented on July 17, 2024

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

from aws-serverless-samfarm.

MatteoGioioso avatar MatteoGioioso commented on July 17, 2024

And how about shared libraries and code?

from aws-serverless-samfarm.

debendraoli avatar debendraoli commented on July 17, 2024

@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. :-)"

from aws-serverless-samfarm.

gavetisyanca avatar gavetisyanca commented on July 17, 2024

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

from aws-serverless-samfarm.

sromano88-svc avatar sromano88-svc commented on July 17, 2024

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.

from aws-serverless-samfarm.

emaayan avatar emaayan commented on July 17, 2024

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?

from aws-serverless-samfarm.

jesstucker avatar jesstucker commented on July 17, 2024

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!

from aws-serverless-samfarm.

afern247 avatar afern247 commented on July 17, 2024

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?

from aws-serverless-samfarm.

keithics avatar keithics commented on July 17, 2024

one handler for all of the products API?

from aws-serverless-samfarm.

sromano88-svc avatar sromano88-svc commented on July 17, 2024

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)

from aws-serverless-samfarm.

iongion avatar iongion commented on July 17, 2024

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

from aws-serverless-samfarm.

iongion avatar iongion commented on July 17, 2024

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

from aws-serverless-samfarm.

hughesjj avatar hughesjj commented on July 17, 2024

From #5 (comment)

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)

from aws-serverless-samfarm.

estebansolo avatar estebansolo commented on July 17, 2024

@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

from aws-serverless-samfarm.

ninjasujan avatar ninjasujan commented on July 17, 2024

@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

from aws-serverless-samfarm.

sromano88-svc avatar sromano88-svc commented on July 17, 2024

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

Sorry for the delay.... I have moved from this AWS project so I missed part of the code, but here are some parts....
In package.json I had this two scripts lines

        "build": "webpack --watch",
        "start": "sam local start-api --warm-containers EAGER --skip-pull-image",

And the structure was
.
└── src
├── handlers
├── services
├── tests
└── utils

Also I am attaching the webpack.config.js file (renamed as txt).
webpack.config.js.txt

from aws-serverless-samfarm.

saadzaman avatar saadzaman commented on July 17, 2024

@ninjasujan What about package.json ? can it be removed ?

from aws-serverless-samfarm.

ninjasujan avatar ninjasujan commented on July 17, 2024

@saadzaman you need to keep package.json file as well for each lambda fn.

but after making detail research on SAM and the use case of the applications, I decided to change my approach, now I'm creating separate SAM package for each lambda function, if you have some small function like handlers or authentication (Lambda authorizer then you can create 2 functions in sam file).
Its completely my use-case and approach you can use it depending on your requirement.

from aws-serverless-samfarm.

noopd13 avatar noopd13 commented on July 17, 2024

Hello guys, i need to have single api gateway with dev and prod stages, which will be triggered by multiple lambda function according to stages
any help would be appreciated

from aws-serverless-samfarm.

iongion avatar iongion commented on July 17, 2024

@noopd13 what do you mean by triggered ? You mean the client code calling the api will decide if it invokes staging or prod / dev environments ?

The typical way to solve this is using stage urls, AWS has full support for this.

Try to avoid exposing your non production environments to the world also, non-production code might contain bugs, which might bring security issues.

from aws-serverless-samfarm.

noopd13 avatar noopd13 commented on July 17, 2024

@iongion , Yes the client will decide , based on the base path, I need to use the same api gateway
for the both the environment(stage lambda and prod lambda ).I am having hard time writing yml for it

from aws-serverless-samfarm.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.