question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple Lambda functions from the sam.yml file possible?

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:13
  • Comments:37

github_iconTop GitHub Comments

97reactions
jairoVeracommented, May 3, 2018

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
25reactions
andrewryan1906commented, Mar 23, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Lambda functions from the sam.yml file - Stack Overflow
You can have as many AWS::Serverless::Function resources in a single template as you want as long as they have a unique logical id....
Read more >
Automate deployment of nested applications using AWS SAM
A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks.
Read more >
SAM template: Configure an api gateway with multiple lambdas
The above template creates a lambda function ,an API Gateway resource, necessary lambda execution roles, permissions and associates the lambda ...
Read more >
Managing Multiple Functions with AWS SAM and Webpack
Copy-pasted webpack files can be tough to handle, with weird ... First of all, I'm going to be exporting two Lambda functions so...
Read more >
AWS Lambda Guide - Serverless.yml Reference
Here is a list of all available properties in serverless.yml when the provider ... List of existing Amazon SNS topics in the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found