-
Notifications
You must be signed in to change notification settings - Fork 240
Multiple Lambda functions from the sam.yml file possible? #5
Comments
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. |
any help |
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.
|
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 |
@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 |
@red8888, using the @jairoVera approach, as both Regarding the first code if you have a |
@jairoVera do you know if that approach will generate 1 or 2 API Gateways? |
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. |
It seems SAM is intended for one lambda per application. For multi-function applications, there is Nested application method. |
SAM does support multiple functions, the right way to do it is with the Maybe someone from the SAM team can reiterate this? |
@deleugpn it will generate two API Gateways, but you can add the API Gateway resource and It will create just one
|
And how about shared libraries and code? |
You can use Layers |
@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 I specified 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.
|
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 |
Yes, it happens when you deploy it. You can see it in the AWS Lambda console. In my case, when running |
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 |
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? |
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? |
This is the way |
one handler for all of the products API? |
Yes, I had that approach initially (one handler for all of the products API). Comment 1: Comment 2: |
@sromano88-svc @estebansolo That looks so so nice! |
How do you do that ? What is your file-system structure ? I have webpack too and plan to use typescript |
From #5 (comment)
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 |
@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 |
@jairoVera's solution worked for me I just created two resource in same templete.yml file.
** yml file **
|
Sorry for the delay.... I have moved from this AWS project so I missed part of the code, but here are some parts....
And the structure was Also I am attaching the webpack.config.js file (renamed as txt). |
@ninjasujan What about package.json ? can it be removed ? |
@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). |
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 |
@noopd13 what do you mean by 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. |
@iongion , Yes the client will decide , based on the base path, I need to use the same api gateway |
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 |
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?
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:
The text was updated successfully, but these errors were encountered: