Deploying Multiple Lambda Functions under a Single Lambda Project
In this Article I am going to explain how to write, test and deploy multiple Lambda Functions with the help of one single Lambda Project.
Here, I will be creating three Lambda Functions to demonstrate about the topic. So, Let’s Start Coding.
Prerequisites needed to implement this are AWS Toolkit for Visual Studio , AWS Account & Visual Studio (Whichever version which supports AWS & .NET Core)
Step 1:
If you are aware of setting up an AWS Profile in your Visual Studio, then please proceed to Step 2.
- Install AWS Toolkit for Visual Studio. I am using Visual Studio 2019 install it from MarketPlace.
2. Once you install the AWS Toolkit it should appear in Visual studio as the below
3. Click on AWS Explorer and Add a user Profile (i.e. The Access Key and Secret Key of your AWS IAM User) as shown in the pic below.
4. Click on Ok. Now we are ready to get going with our Lambda Functions.
Step 2:
- Open Visual Studio & create a new Lambda Project in .Netcore
2. Provide a Name to it and Proceed.
3. For simplicity, I have selected Empty Function Template ( you are free to use whatever is feasible for you)
4. Create a Class to send the inputVariables for testing. Name it as InputModel
public class InputModel{public int Input1 { get; set; }public int Input2 { get; set; }}
5. Modify your Function.cs class like below.
public class Function{public int AddNumbers(InputModel input, ILambdaContext context){Console.WriteLine($”This is First Lambda Function Log. We are adding {input.input1}, {input.input2}.”);return input.input1 + input.input2;}public int SubtractNumbers(InputModel input, ILambdaContext context){Console.WriteLine($”This is Second Lambda Function Log. We are Subtracting {input.input1}, {input.input2}.”);return input.input1 — input.input2;}public int MultiplyNumbers(InputModel input, ILambdaContext context){Console.WriteLine($”This is Third Lambda Function Log. We are multiplying {input.input1}, {input.input2}.”);return input.input1 * input.input2;}}
6. Now we have 3 functions in a single Lambda Project ready to test. But, they are not yet ready to get deployed all at a time. Here, Serverless.template will come to our rescue. Add Serverless.template file by right click on the Project and Add Serverless.template.
7. My Resources section in serverless.template looks like this.
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Transform" : "AWS::Serverless-2016-10-31",
"Description" : "Starting template for an AWS Serverless Application.",
"Parameters" : {
},
"Resources" : {
"AddFunction" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "MmultipleLambdaSolution::MmultipleLambdaSolution.Function::AddNumbers",
"FunctionName" : "AdditionLambda",
"Runtime": "dotnetcore3.1",
"CodeUri": "",
"Description": "This is an Addition Function deployed under Multiple Lambda in a single Solution Topic",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambda_FullAccess" ]
}
},
"SubtractFunction" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "MmultipleLambdaSolution::MmultipleLambdaSolution.Function::SubtractNumbers",
"FunctionName" : "SubtractLambda",
"Runtime": "dotnetcore3.1",
"CodeUri": "",
"Description": "This is an SubtractNumbers Function deployed under Multiple Lambda in a single Solution Topic",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambda_FullAccess" ]
}
},
"MultiplyFunction" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "MmultipleLambdaSolution::MmultipleLambdaSolution.Function::MultiplyNumbers",
"FunctionName" : "MultiplyLambda",
"Runtime": "dotnetcore3.1",
"CodeUri": "",
"Description": "This is an MultiplyNumbers Function deployed under Multiple Lambda in a single Solution Topic",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambda_FullAccess" ]
}
}
},
"Outputs" : {}
}
Step 3
- Publish Lambda.
2. Test the Lambda Function either locally or through AWS Console.
Summary
With this we came to know that we can write all the Logic and common functionalities of Lambda Functions working on one Entity. Hope you all Learned a new thing today.
Happy Coding!!!