Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Lambda functions from the sam.yml file

Still waiting for actual AWS support for this: https://github.com/aws-samples/aws-serverless-samfarm/issues/5

How is this suppose to work?

My use case is I have an API Gateway fronted lambda that writes events to an SNS topic. I have another lambda that is subscribed to that topic.

Could these lambdas be in separate repos? yes. Is part of the purpose of using a pub/sub pattern to separate these two applications in the first place? yes.

BUT this is a simple app. The topic wont be shared with other functions and the whole thing is self contained. It should all be deployed together ideally all in the same template.

I can easily add all the functions I want to my SAM template but how do I deploy them? Should they each have a different CodeURI? That mean I need to script copying and install each lambdas dependancies into a different folder then point the codeuri's for each lambda in the template to the different folder.

Is there not better support for this?

like image 573
red888 Avatar asked Sep 21 '18 18:09

red888


People also ask

Can you have multiple Lambda functions?

Serverless applications usually consist of multiple Lambda functions. Each Lambda function can use only one runtime but you can use multiple runtimes across multiple functions. This enables you to choose the best runtime for the task of the function.

How do you create multiple Lambda functions?

Let's first create a Lambda function with amplify add function command and name it multiplefunction . After the execution completes, you will see multiplefunction{random_value} project created. After that, let's create directories functionOne and functionTwo under src directory and copy index.

How many Lambda functions can be created in an AWS account?

The default concurrency limit per AWS Region is 1,000 invocations at any given time. The default burst concurrency quota per Region is between 500 and 3,000, which varies per Region. There is no maximum concurrency limit for Lambda functions.


1 Answers

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.

If you prefer to keep several lambda functions in a single repository you will have to provide different CodeUri for each lambda. For instance CodeUri: ./handler-my-lambda-one.zip and CodeUri: ./handler-my-lambda-two.zip.

Usually, it's a good practice to have a Makefile in your repository that would have a build target responsible for preparing handler-my-lambda-*.zip something like:

build: 
    rm -rf node_modules
    npm install
    zip -r handler.zip index.js lib node_modules

and a deploy target that would package your code (upload code to s3) and deploy cloudformation.

The package command is responsible for uploading the zip artifact specified in CodeUri and replace it with s3 URL in the sam.out.yaml

deploy:
    aws cloudformation package \
        --template-file sam.yaml \
        --output-template-file sam.out.yaml \
        --s3-bucket my-artifact-bucket-name

    aws cloudformation deploy \
        --template-file sam.out.yaml

Since you decided to have multiple lambdas in a single repository probably you would have two build commands for each lambda function and some cd ... logic to change working directory per function

like image 83
b.b3rn4rd Avatar answered Sep 16 '22 13:09

b.b3rn4rd