Few days ago I decided to create my own email marketing tool on serverless architecture. I mostly use .NET Core if it is reasonable enough to use it. After created my first function, I switched from 'aws-lambda-tools-defaults.json' to 'serverless.template' file to use multiple functions in single .NET Core project with Visual Studio. Here is how is done.

Source Repo! You can find fully functional sample built on .NET Framework of this post at GitHub repository .NET-Core-AWS-Lambda-Multiple-Function-in-Single-Project

Create Project

In order to do that you need to install AWS Toolkit for Visual Studio. Then create new project as 'AWS Lambda Project (.NET Core)'.

And choose 'Empty Function', click 'Finish'.

aws-lambda-tools-defaults.json:

{ "Information" : [ "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", "dotnet lambda help", "All the command line options for the Lambda command can be specified in this file." ], "profile":"visualstudio_et", "region" : "eu-central-1", "configuration" : "Release", "framework" : "netcoreapp2.1", "function-runtime":"dotnetcore2.1", "function-memory-size" : 256, "function-timeout" : 30, "function-handler" : "AWSLambda::AWSLambda.Function::FunctionHandler" }

As you see on default configuration there is space for one function handler. Even if you add another function to 'Function.cs' you wont be able to see it on Mock tool.

But I want to code multiple functions in single project of class.

namespace AWSLambda { public class Function { public string ToUpper(string input, ILambdaContext context) { return input?.ToUpper(); } public string ToLower(string input, ILambdaContext context) { return input?.ToLower(); } } }

Solution

To use multiple functions in single project we need serverless.template and reference from 'aws-lambda-tools-defaults.json' file. You can add template file by simply right-click to project and Add>AWS Serverless Template.

First we need to delete function handler references 'aws-lambda-tools-defaults.json' and add referencet to template.

{ "Information": [ "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", "dotnet lambda help", "All the command line options for the Lambda command can be specified in this file." ], "profile": "visualstudio_et", "region": "eu-central-1", "configuration": "Release", "framework": "netcoreapp2.1", "template": "serverless.template" }

In template file we need to set references to all function handler with different name under Recources.

{ "AWSTemplateFormatVersion" : "2010-09-09", "Transform" : "AWS::Serverless-2016-10-31", "Description" : "Starting template for an AWS Serverless Application.", "Parameters" : { }, "Resources" : { "ToUpperFunction" : { "Type" : "AWS::Serverless::Function", "Properties": { "Handler": "AWSLambda::AWSLambda.Function::ToUpper", "Runtime": "dotnetcore2.1", "CodeUri": "", "Description": "Default function", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaFullAccess" ] } }, "ToLowerFunction" : { "Type" : "AWS::Serverless::Function", "Properties": { "Handler": "AWSLambda::AWSLambda.Function::ToLower", "Runtime": "dotnetcore2.1", "CodeUri": "", "Description": "Default function", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaFullAccess" ] } } } }

That's it.

I hope this post has helped you.