AWS Lambda function supports .NET Core 2.0

09 May, 2018 | 3 minutes read

On 15th of January 2018 Amazon Web Services announced new support for creating Lambda functions with the C# programming language and .NET Core 2.0 libraries. This functionality was pre-announced on Re:Invent in November in Las Vegas and was welcomed by the community.

AWS Lambda is the cloud “serverless computing” service, which lets developers create functions that are executed, mostly in response to events, without the need to provision or manage specific servers.

Our client has many services that are running C# code, so we agreed to make a change to a serverless model and move some of them to the cloud. Decision was made based on a research where we compared the approach and the outcome with something that we were used to – Node JS function deployed to AWS Lambda functions.

AWS said the easiest way to get started with its new .NET Core 2.0 support for authoring Lambda functions is to use the AWS Toolkit for Visual Studio and its built-in project templates for individual C# Lambda functions, complete C# serverless applications and tools for publishing both types of projects to the AWS cloud platform. We took this approach and built one AWS Serverless Application (.NET Core). From this point on, everything was well known for our .NET team. They were able to apply all their knowledge of Web API and deliver new functionalities really fast.

AWS serverless application

AWS Serverless Application (.NET Core) type of AWS Lambda project comes with serverless.template used for building the cloud formation stack in the process of deployment.

This template gives you out of the box syntax to deploy your code as one lambda function that will be called from API Gateway through proxy.

There is no option to create several Lambda functions from one Web API solution that will be called from different API Gateway methods.

If you need to do separate lambda function you need to have separate Web API solutions or to zip your solution and deploy it as a package to Lambda function.

To manually create a C# Lambda function, you simply specify the Lambda runtime parameter as dotnetcore2.0 and upload the ZIP of all NuGet dependencies as well as your own published DLL assemblies through the AWS CLI or AWS Lambda console,” AWS said.

If your solution can be run as one Lambda function that serves all operations through one proxy, the serverless.template provided in AWS Serverless Application will create complete solution for you directly from Visual Studio.

Publish to AWS Lambda

When it comes to parameters in Cloud formation template, there is no automated way to take values from appsettings.json file. If you have different configuration files for different environment and you want to deploy solution from Visual Studio in one of those environment, parameters inside the cloud formation script cannot be passed automatically from appsettings.json file. Parameters can be handled inside cloud formation template itself (serverless.template). You can set all parameters values here and choose the correct one for each environment:

"Parameters": {
"ShouldCreateBucket": {
"Type": "String",
"AllowedValues": [ "true", "false" ],
"Description": "If true then the S3 bucket that will be proxied will be created with the CloudFormation stack."
},
"BucketName": {
"Type": "String",
"Description": "Name of S3 bucket that will be proxied. If left blank a new table will be created.",
"MinLength": "0"
},
"isStubData": {
"Type": "String",
"AllowedValues": [ "true", "false"],
"Description": "If true then return mock data."
},
"WOWENVIRONMENT": {
"Type": "String",
"AllowedValues": [ "dev", "qa", "uat", "prod"],
"Description": "Config enviroment settings."
},
"Subnet1" : {
"Type": "String",
…
Editing template parameters

This is not the best approach because it is prone to human error.

If you are headed to continued delivery, best way to eliminate human errors and read values from configuration file is using Jenkins or other tool that provides continuous delivery.

When it comes to cold start, it exists and is a little bit longer compared to dynamic languages like Node JS and Python. What is interesting here is that AWS are really working on performance of the Lambda function (see links below this blog). Performance is significantly increased for all supported languages, but the most surprising thing is that .Net Core 2.0 has an outstanding performance compared to Java, Node JS, Python and Go. So, if you already have function running in .NET Core 1.0, consider upgrading to 2.0.

Conclusion

.NET Core 2.0 is the right choice to go serverless using AWS Lambda functions with its current outstanding performance, easy way to deploy to AWS and continued building Web API as you were used to in standard .NET.

Some excellent articles about Lambda function performance:

2017: https://read.acloud.guru/comparing-aws-lambda-performance-when-using-node-js-java-c-or-python-281bef2c740f

2018: https://read.acloud.guru/comparing-aws-lambda-performance-of-node-js-python-java-c-and-go-29c1163c2581