Building Serverless Applications in C# with AWS Lambda

Building Serverless Applications in C# with AWS Lambda

by 

| September 21, 2021 | in

Hosting your backend solution used to be pretty straightforward for .NET developers. You were basically writing an ASP.NET website that was hosted in IIS. Today there are a lot of options, of which IIS is still one of them. Many of the web applications we write today end up hosted in either Azure App Services or Azure Functions, with the latter being a very attractive option for cost-sensitive customers because of its consumption-based pricing. However, AWS has another option for us: AWS Lambda.

AWS Lambda is a lot like Azure Functions, but their set up/hosting model is more typical. You are basically just writing an ASP.NET Web application that will be hosted in AWS. This makes the hosting seem easier to understand and probably easier to live with. AWS has done a great job of supporting us .NET developers with the AWS Lambda.

Let’s jump in and create one.

First, create a new AWS Serverless project.

configure your new project

You are then confronted with a lot of choices. Select “ASP.NET Core 5 (Container)”.

select blueprint

You will now have an ASP.NET API service. The great thing about AWS Lambda is they pretty much look like ASP.NET projects. AWS does a great job making a Lambda project feel very natural for .NET developers. Ironically, AWS and how they do Lambda will probably feel better to developers than Azure Functions created by Microsoft.

ApiServices

How does this project work? Like any .NET Core ASP.NET project, there is a Startup.cs file that really drives the configuration. And usually, there would also be a Program.cs file. Where is that?

With AWS Lambda, two entry points into the application exist. One for running locally so you can debug in VS and one for running in AWS Lambda. These two files are LocalEntryPoint and LambdaEntryPoint.

LocalEntryPoint looks pretty much like a Program.cs for most ASP.NET Core apps.

LambdaEntryPoint is used when the application runs in the AWS Lambda. This class is just calling Startup.cs.

AWS has done a great job of making Lambda development feel like a first-class citizen.

Publishing an AWS Lambda from VS is also extremely easy. Right-click on the project and select “Publish to AWS Lambda”.

publish to AWS Lambda

Name your Stack and your S3 Bucket, and click “Publish”.

publish AWS serverless application

That is pretty much all it takes to get a C# Lambda up and running. Pretty impressive when you think about it. You now have a C# Lambda running in AWS using Docker, and all you did was click some buttons.