Getting started with .NET Core and AWS Lambda
March 22, 2017
Serverless and AWS Lambda
AWS Lambda is the serverless product offered by Amazon Web Services. Serverless does not mean that there is no server involved - obviously there is - but just that managing servers, scaling, etc is not something that you need to worry about.
What you need to worry about (as programmer) is writing the code that executes the logic. You can then deploy it to a serverless environment such as AWS Lambda and it will take care of scaling etc. Some of the other offerings available are Azure Functions, Google Cloud Functions and also Webtask.
The other advantage is that serverless environments only charge you for the time your code takes to execute. So if you have a small POC you want to develop, this is ideal as you do not have to worry about paying for a server which sits idle 99% of the time.
Programming models for C# / .NET Core
I have only started playing around with AWS Lambda recently, but from what I can see so far there are basically 3 models which you can use when developing AWS Lambda functions using .NET Core and C#.
- Plain Lambda Function. In this case you create a Lambda function which is a simple C# class with a function. This function can be triggered by certain events, or triggered manually from an application.
You can also read more about this model in Deploying Lambda-based Applications.
ASP.NET Core app as Serverless Application. This is a variant of the model above, but in this instance your entire exiting ASP.NET Core application is published as a single Lambda function. This then utilizes the API Gateway Proxy Integration to forward all request coming through API Gateway to this Lambda Function.
The Lambda function then hands the request off to the ASP.NET Core pipeline. So you have all the middleware you love at your disposal.
This means that you basically have the normal ASP.NET Core request pipeline, but instead of IIS/Nginx and Kestrel, you have API Gateway and Lambda. So instead of this:
You have this:
In the coming blog posts I will dive deeper into each of these.
So why AWS Lambda and not any of the others?
So I guess another question is why I am using AWS Lambda and not the offerings from Microsoft, Google or Auth0’s own WebTask? Well, for a couple of reasons which are true at the time of writing this blog post:
- It supports .NET Core - the others don’t. Google Functions isn’t even generally available yet.
- The 3rd programming model I mentioned above (ASP.NET Core app as Serverless Application) is one which interests me the most. I can take an entire ASP.NET Core app and publish it to Lambda. This means I can test my app locally with my normal development flow, and then simply push it to AWS to host it as a Lambda function.
Some resources to get you started
I recommend watching this video first as it gives a great overview of AWS Lambda, the C# and Visual Studio support for AWS Lambda as well as the 3 programming models I discussed above.
- Preview of the AWS Toolkit for Visual Studio 2017
- Using the AWS Lambda Project in Visual Studio
- AWS Serverless Applications in Visual Studio
- Running Serverless ASP.NET Core Web APIs with Amazon Lambda
- Deploy an Existing ASP.NET Core Web API to AWS Lambda
- Deploying .NET Core AWS Lambda Functions from the Command Line
- Creating .NET Core AWS Lambda Projects without Visual Studio
Also stay tuned, as I plan to do more posts on C#, .NET Core and Lambda in the future.