Implementing AWS Lambda in C#
AWS Lambda is an AWS serverless compute that allows you to execute your code without managing any servers.
For more information please read AWS | Understanding AWS lambda
Here we are focusing on implementing AWS lambda function in C#
Please visit https://visualstudio.microsoft.com/downloads/ to download visual studio and install it on PC.
Step 1: Add Extension by click on Extensions Menu Item then Click on Manage Extensions
Step 2: Download AWS toolkit extension
Check Manage Extensions window from step 1 so now search for AWS toolkit, instantly in the search result window you will get AWS Toolkit for Visual Studio 2017 and 2019.
It shows you the download button, click on download, after successful download you will see a green tick mark.
The AWS Toolkit for Visual Studio is an extension for Microsoft Visual Studio on Windows that makes it easier for developers to develop, debug, and deploy . NET applications using AWS. … As your application runs, you can use the AWS Explorer to view the AWS resources used by the application.
Step 3: Create a new Project
After extension add from step 2, Create new Project from File Menu -> New -> Project
Step 4: Search for AWS template
we are now ready to create a new project you can see different templates that helps us to use AWS in your application.
Let’s select AWS Lambda Project with Tests(.Net Core — C#) and click on Next
Step 5: Configure Project
Specify a name and desired location for a project. I mentioned the project name as AWSLambdaEmployee. Click on Create button
Step 6: AWS Select Blueprint
This blueprint provides us started code to use Lambda with other AWS or any other third-party application. Here I’m using Empty Function, click on Finish.
Step 7: Here on Solution Explorer we can see the directory structure for AWSLambdaEmployee
AWSLambdaEmployee.Tests contains FunctionTest.cs file where we are going to write test cases. The actual code to make this test pass goes into FunctionHandler in Function.cs file
Step 8: Add a new class by right click and Add->New Item
Give the class name as Employee.cs
Step 9: Add properties of class Employee as follows.
Step 10: Write Test case
In this template, we are going to write Xunit test.
var function = new Function();
is the object variable of class from Function.cs which contains handler function.
var context = new TestLambdaContext();
TestLambdaContext is exactly what it sounds like, a lambda context for use with tests. You can get information like AwsRequestId, ClientContext, FunctionName, FunctionVersion and many more.
In the given template create an object for the Employee class and initialize it.
Employee emp = new Employee
{
EmployeeId = 101,
Name = “Yogita Kumar”,
Email = “abc@gmail.com”,
Post = “Software Developer”,
Salary = 50000
};
declare result variable which holds the output from the Handler function.
var result = function.FunctionHandler(emp, context);
Assert compares the output from the function handler which is stored in var result against the desired string.
Assert.Equal($”Name: {emp.Name} Post: {emp.Post} Email: {emp.Email}”, result);
Step 11: At this point, we have an Employee class with us and test case.
Now to make this test pass, we are going to write code into Function.cs file
To the Function FunctionHandler() just pass Object of class Employee and ILambdaContext.
Which returns a string with Employee name, post, and email.
Step 12: To run the test, just right click on test project and click on Run Tests
Test passed, you can see a green tick mark
Step 13: Now time to publish to the AWS Lambda function. Right-click on Project name here AWSLambdaEmployee and click on Publish to AWS Lambda
Step 14: You will get the following template to Upload Lambda Function
You have to select AWS Credentials, Region, You can specify Function Name, Description, Configuration, framework, and many more related to your function.
Click on Next, need to fill in some advanced function details, specify the role name, if you don’t have any create a new role. How much memory need to execute function etc
Click on the Upload button it takes few minutes.
Step 14: After successful upload, we can see the following window.
Here on the left-hand side, we can create an Employee object in JSON format, then click on Invoke button. You can see the response on the right-hand side Response window.
we can get a response as per our FunctionHandler().
By following these simple steps, you can create an AWS lambda function in C#. For source code please visit AWSLambdaInCSharp
Thank you!