Reputation: 1717
In the AWS Lambda Visual Studio walkthrough to create a Lambda function: http://docs.aws.amazon.com/lambda/latest/dg/lambda-dotnet-create-deployment-package-toolkit.html you create a single AWS Lambda function in the Visual Studio project.
Does that mean that you can only create one function per project? What do you do if your serverless app has many functions? Is the function to VS project ratio 1:1, or am I missing something?
Upvotes: 17
Views: 13801
Reputation: 1140
You can create multiple lambda functions in one lambda project without using an API gateway or Serveless Project.
Adding Serverless Template: Right click on your project and insert AWS server template. This template by default has a setup for AWS API gateway output. Modify this file to get rid of this. It should look like the following:
{
"AWSTemFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "Test Project AWS Serverless Application.",
"Parameters": {},
"Resources": {
"Register": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "TestProject.Lambda::TestProject.Lambda.Function::RegisterAsync",
"Runtime": "dotnetcore2.1",
"CodeUri": "",
"Description": "Register Function",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [
"AWSLambdaFullAccess"
]
}
},
"CreateUserFor": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "TestProject.Lambda::TestProject.Lambda.Function::CreateUserForAsync",
"Runtime": "dotnetcore2.1",
"CodeUri": "",
"Description": "Create User Function",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [
"AWSLambdaFullAccess"
]
}
}
}
}
In this example we have two lambda functions defined Register and CreateUserFor.
Now add a reference to this template in the projects aws-lambda-tools-defaults.json
as follows:
{
"profile" : "default",
"region" : "us-west-2",
"configuration" : "Release",
"framework" : "netcoreapp2.1",
"tem" : "serverless.tem",
"s3-bucket" : "towmenot",
"stack-name" : "TowMeNot"
}
You can now define your function handlers with the names mentioned in the template. In this case:
public async Task<bool> RegisterAsync(Registration registration, ILambdaContext context)
{
await RegisterHelper(registration);
return true;
}
public async Task<User> CreateUserAsync(User newUser, ILambdaContext context)
{
return await CreateUserHelper(newUser);
}
Upvotes: 4
Reputation: 1256
If you use the AWS Lambda Project(.Net Core) template, you can only write one function per project. You can see that the aws-lambda-tools-defaults.json file only contains configuration for one function.
However, if you use AWS Serverless Application(.Net Core) template, you can manage multiple Lambda functions in one project to response to different API call using API Gateway. This is achieved through CloudFormation.
Check out this AWS ReInvent video: https://www.youtube.com/watch?v=Ymn6WGCSjE4&t=24s Jump to 31:08 to see how AWS Serverless Application with multiple Lambda functions works.
Upvotes: 14