Tutorial: Build Serverless functions with C#
Tags: aws, csharp, serverless, tutorial
Published: 2021-11-23 12:32:28

The world of cloud computing has been revolutionized by a solution called serverless computing. It has been an absolute joy for developers to use.

Before this innovation, developers had to worry about the resources powering their code. Since the launch of serverless computing, the developer's focus on operating-system and hardware architecture is now a thing of the past. It handles all the server management while focusing on what you do well --- writing good quality code.

This article will explain how to build serverless functions on the primary cloud providers services: Azure and AWS.

> *If you are new to serverless computing or need to dig deep into the origins and evolutions of serverless, check out this* [*article*](https://dashbird.io/blog/origin-of-serverless/)*.*

**Using C# with Azure Functions**
---------------------------------

Two essential prerequisites need to be met to build serverless Azure Functions applications with C#:

1.  Have an active Microsoft Azure subscription. If you don't have one already, you can [create a free account](https://azure.microsoft.com/en-us/free/).
2.  Get an Azure Storage account. You can create a storage account if you don't have this by signing into [the Azure portal](https://portal.azure.com/).

As a C# developer, you are already familiar with the various tools for building your applications, including Visual Studio Code and Visual Studio IDE. Both tools help create applications with Azure Functions, depending on whichever one you are most comfortable with. Let's dig a little deeper into these two tools.

Visual Studio Code
------------------

Visual Studio Code is a lightweight but powerful code editor with different versions available for Windows, Linux, and macOS.

To build apps for Azure functions, you will need to get Azure Tools for Visual Studio Code which will give you convenient commands to access or create resources directly from the VS Code editor.

To set up Azure Tools, install the [Azure Extension Pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-azureextensionpack) by firing up your editor and navigating to the extension marketplace on the left side menu. The extension marketplace interface in VS Code should look like this:

When you are done with the setup of Azure for VS Code, log in to Azure from the code editor by firing the Command Palette with the commands **CTRL+Shift+P** and typing "**Azure: Login**" in the text field that appears. Click the corresponding result that appears, and a code will be generated by the editor with instructions on how to complete the process.

Another vital extension if you will be developing in C# with VS Code is the [C# for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp). As of the time of this writing, the extension supports basic debugging capabilities, full details of [which can be found here](http://aka.ms/vscclrdebugger).

Visual Studio 2017 IDE
----------------------

From version 15.5, the Azure Development workload comes bundled with Azure Functions tools. This implies if you plan on installing the latest version of Visual Studio 2019, you need to include Azure Development workload in your installation process.

Once installation is complete, sign in to your Microsoft account in Visual Studio and create a new Azure Functions project, and you are good to go.

Now that we've highlighted what you need to know if you want to build serverless functions in C# with Azure functions, let's move over to discussing what you need to begin running serverless apps on the AWS Lambda serverless framework

**Using C# with AWS Lambda**
----------------------------

AWS Lambda is the compute part of the AWS serverless architecture and requires almost zero administration. It is one of the most popular options for serverless, and though AWS launched it in 2014, in 2018, AWS announced official support for [a .NET Core 2.0 runtime](https://aws.amazon.com/about-aws/whats-new/2018/01/aws-lambda-supports-c-sharp-dot-net-core-2-0/), and in [2020 for version 3.1](https://aws.amazon.com/blogs/compute/announcing-aws-lambda-supports-for-net-core-3-1/). It is crucial to understand that .NET Core is different from the .NET framework, and here are a few reasons AWS chose .NET Core over the .NET framework

-   .NET Core is the newly redesigned version of .NET that focuses on more modern applications and, in particular, cloud-enabled applications, which has significant benefits when writing Lambda functions
-   .NET Core was designed with a modularized design which means you only get to include the part of .NET you will need when writing your Lambda functions. This leads to lesser memory usage, and since Lambda charges you for memory usage, lesser memory demands by your functions will lead to lesser costs for using AWS Lambda.
-   .NET Core is open-source and validated against Amazon Linux -- the underlying platform for AWS Lambda, making it possible for AWS to respond to security issues that might arise with using it.
-   With .NET Core, you can now write your C# code on any platform instead of earlier days with .NET, where you could write C# code on only Windows environments.

To begin building Lambda functions in C# with Visual Studio, you will need to understand AWS Lambda has a laid-down pattern for authoring code for your Lambda function. This pattern includes the following concepts:

-   **Handler** -- the handler function is a function called by AWS Lambda to begin execution of your Lambda function. AWS Lambda passes any input data into this function as the first parameter and passes a context object as a second parameter.
-   **Context Object** -- is the second parameter passed into the handler function, and it provides information through which your code interacts with AWS Lambda.
-   **Logging** -- a sound logging system is a critical component of a well-written function. AWS Lambda writes these logs to CloudWatch Logs.
-   **Exceptions**

Now that we know the framework for writing for AWS Lambda, let's see how all this works together.

Writing C# code for AWS Lambda with Visual Studio
-------------------------------------------------

A few prerequisites are required to begin writing code for Lambda functions:

-   If you don't have one, you can [create an AWS account here](https://portal.aws.amazon.com/billing/signup?nc2=h_ct&src=header_signup&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start)
-   [Download](https://visualstudio.microsoft.com/downloads/) and install Visual Studio IDE 2019.
-   [Download](https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.AWSToolkitforVisualStudio2017) and install the AWS Toolkit for Visual Studio

Once you've installed all prerequisites, open Visual Studio and fill in the AWS credentials on the **Getting Started with the AWS Toolkit for Visual Studio** window displayed next to the Start Page tab. The credentials (Access Key and Secret Key) can be obtained by following the instructions outlined in the window.

With all set, let's move on to build our Lambda project.

-   Create a .NET Core Lambda project
-   Open Visual Studio IDE and go to file> New > Project
-   On the Installed pane, Click Visual C# and then AWS Lambda Project (.NET Core) as shown below:
-   Fill out the project name with a name of your choice and click OK, leaving the defaults as they are, then move on to selecting the type of Lambda project you want to build.
-   Click Finish to create the project and review the project code and structure.

A critical file created with your project is the `aws-lambda-tools-defaults.json` file, where you set your function handler and other options. 

The contents of the file should look like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqa19aqkuds0czbfhjp1.png)

_For copyable code snippets see the [original article](https://dashbird.io/blog/serverless-and-c-what-you-need-to-know/)._

Publishing to AWS Lambda
------------------------

The next step to running our code on AWS Lambda is to publish it. This is done after the code has been reviewed and you are convinced it is good to go. The following steps outline what you need to do to get your code published.

-   On the right side, you can see the project. There, in the Solution Explorer, click Publish to AWS Lambda.
-   Next, fill in the Function Name in the Upload to AWS Lambda window that appears. You can give any preferred name to your function at this point. Once you are done, click Next.
-   On the Advanced Function Details page, fill the Role Name section with a role associated with your AWS account. This is a required field to supply before proceeding to the next stage of the upload process. Other sections include the VPC section (only applicable if your function will be accessing resources on Amazon VPC) and the Environment section.
-   Once your function begins uploading, a window is displayed showing the status of the upload, after which a function view page is displayed where you can test your function and view logs, respectively.
-   The Invoke button begins testing the function while the log output displays the output from the test. These logs are also saved into CloudWatch Logs in AWS, where more details on the logs can be viewed.

Conclusion
----------

Now, you have a quick peek into what you need to know to build and publish serverless applications with the powerful C# programming language, and though at [Dashbird](https://dashbird.io/), we are more inclined towards AWS Lambda, nothing is stopping you from exploring both the Microsoft Azure functions and AWS Lambda options in getting that serverless infrastructure for your applications.

* * * * *

*Further reading:*

[How to get an overview of C# Lambda functions](https://dashbird.io/blog/how-to-get-overview-c-lambdas/)

[AWS vs Azure, a quick comparison](https://dashbird.io/blog/aws-vs-azure/)

[Serverless observability and real-time troubleshooting](https://dashbird.io/observability/serverless-observability-real-time-debugging/)