Comments (37)
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.
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.
@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.
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.
It seems SAM is intended for one lambda per application. For multi-function applications, there is Nested application method.
from aws-serverless-samfarm.
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.
any help
from aws-serverless-samfarm.
You can use Layers
from aws-serverless-samfarm.
@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.
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.
@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.
@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.
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.
from aws-serverless-samfarm.
@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 buildsam
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.
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 thatThis 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.
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.
@jairoVera do you know if that approach will generate 1 or 2 API Gateways?
from aws-serverless-samfarm.
And how about shared libraries and code?
from aws-serverless-samfarm.
@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 discoveredsam
and I introduced one template file for all the functions but buildsam
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.
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.
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.
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.
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.
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.
This is what I do:
... PutProductsAPI: Type: Api Properties: Path: /api/products/{productId} Method: PUT DeleteProductsAPI: Type: Api Properties: Path: /api/products/{productId} Method: DELETEOnly 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.
one handler for all of the products API?
from aws-serverless-samfarm.
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.
@sromano88-svc @estebansolo That looks so so nice!
from aws-serverless-samfarm.
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 fasterComment 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.
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 thatThis 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: DELETEThis 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.
@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.
@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.
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.
@ninjasujan What about package.json ? can it be removed ?
from aws-serverless-samfarm.
@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.
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.
@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.
- https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-stages.html
- https://docs.aws.amazon.com/apigateway/latest/developerguide/stages.html
from aws-serverless-samfarm.
@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)
- 1 lambda function with several events (several http methods for restful api) HOT 4
- ServerlessPipeline Failure to create S3 bucket: Bucket name should not contain uppercase characters HOT 2
- Python Error
- ServerlessPipeline Failure to create S3 bucket: Bucket name should be between 3 and 63 characters long HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-serverless-samfarm.