.net core tutorials

Getting started with .NET Core and the Serverless Framework

Serverless framework is a cloud provider agnostic toolkit designed to aid operations around building, managing and deploying serverless components for either full-blown serverless architectures or disparate functions as a service (FaaS).

I have previously posted about using Build a Twitter bot with AWS lambda serverless framework and node.js but in this post we’ll explore the Serverless Framework with .net core and provide an example of how to develop AWS Lambda functions using the Serverless framework and .net core.

The key objective of the Serverless Framework is to provide developers with an interface that abstracts away Cloud Vendor specific APIs and configuration whilst simultaneously providing additional tooling to enable the testing and deployment functions with ease, perfect for rapid feedback or being able to integrate into your CI/CD pipelines.

This article is based on using the Serverless Framework and .net core on Ubuntu, but the process should be similar using whichever operating system you use.

Install the Serverless Framework

The Serverless Framework is a node based package, therefore required node to be installed on your system.  I typically use and have Node Version Manager installed on my Ubuntu development desktops which enables me to manage install multiple different versions of the node framework on my development machine.

Installing Serverless Framework is really easy and is well documented on the but I will reproduce it here for reference.

Once installed you are good to go.  I do recommend you open an account on the Serverless Framework website, but it is essential to get started to play with the framework.

To create an account you can use the terminal shortcut, which will open a browser and ask to create an account or login


Serverless Platform

Although it is not absolutely necessary to have one and you can still develop and deploy Serverless Applications using the Serverless Framework without one, you may find the Serverless Platform to be quite a handy utility to help you on your path.

You may want to take this opportunity as you’re on the Serverless website to create your Serverless Platform account too. I simply login with my Github profile.

You may also want to create a Application bucket for your demo applications.

The Serverless platform helps you too

Gain and share visibility across all serverless applications, the cloud resources they contain, and their deployments, in a single dashboard.


Install AWS CLI

Serverless Framework enables you to seemlessly deploy applications to AWS. However,  this does require AWS Credentials to do so.  There are a few ways to supply your credentials, my preferred approach it to use the AWS CLI to configure my details.

In order to use Serverless on AWS you will need an AWS Account. If you don’t already have one, you can sign up for a free trial that includes 1 million free Lambda requests per month

Installing the AWS CLI on Ubuntu you will need Python Pip installed,

To install the AWS CLI simply

Once complete we just need to ensure the Path to AWS CLi is configured correctly

We then just need to refresh our terminal window to get the changes

We can then configure our AWS CLI to use our IAM credentials using

Complete the questions and enter your details. You will now be set to use your AWS account for Lambda services.

For detailed information of the aws configure check out the Configuring the AWS CLI

Create Lambda Project

The serverless framework has a number of templates available to help create lambda projects quickly and easily. To see the complete list yo can use

Which will return a list similar too:

You should notice that both C# and F# project templates are available.

To create a lambda project and instruct Serverless to use C#  we proide a –template switch

This will generate a project for us comprising a number of files.

We can open the project in whichever IDE or Text Editor of your choice to view the files , in my case I use Jetbrains Rider – A fast & powerful, cross-platform .NET IDE

Lets go through some of the important files here, in order to get a better understanding of the project before we start coding.

serverless.yml

The serverless.yml is basically the schema which defines the configuration of your serverless application and how it will interact with components included in your wider serverless architecture.

When you deploy your serverless application the framework generates a Cloud Formation template with AWS – in this case – will use to provision the appropriate infrastructure.

Serverless provides a comprehensive Reference of all the available properties to use when configuring your application with AWS.

It’s well woth taking the time to read through this, in order to gain a broader understanding of what is available.

build.sh

The build.sh is important for when you want to build your serverless application in preparation for deployment.

The build.cmd is the build file for windows based developers.

Deploy and Test

Your lambda can be deployed and executed at this stage. If you would like to deploy and test your lambda at this stage Serverless Framework have a great quick start guide to take you through this process.

Serverless Lambda in Action

I wanted to create a bit more of meaningful example to illustrate why developers would use Lambdas with a real world example and just how the Serverless Framework simplifies this process.

In the example I am going to build a Lambda which is notified by S3 storage when a new file is created. The Lambda then parses the file to a JSON format then dumps the JSON data to a DynamoDB database.

I will focus on the Lambda specifics of this process and won’t be discussing the other code involved in this process in depth.

The problem the Lambda I will be developing will solve, primarily focuses around the processing of Word Documents, primarily Docx.

The lambda will be cconfigured to monitor a S3 bucket and if a new file is created in the bucket it will process the file. Processing the file includes parsing the file extracting keywords, phrases and other bits of information contained in the file. Creating a JSON representation of the data contained and saving it to the DynamoDB table for further processing.

All the code for a first test is now complete. We can now build our application and deploy it. In a terminal window we need to ensure we are in the directory where our build.sh then we can simply execute our build file

Once the application has completed building we can then deploy it. Using the Serverless Framework command

We added the -v to display Verbose information regarding the deployment operation. Sometimes this may be necessary to display more information to help troubleshoot deployment issues. We’re not expecting any errors but its a good opportunity to get acquainted with the output and see what information is available.

After our deployment is complete lets first check our service logs, to ensure ourLambada has not executed. Using a terminal window in the directory of our lambda code.

We have used the logging functionality of the Serverless Framework and provided the -f which is the shorthand for --function then provided the function name we wanted to query. This is also the function name we defined in our serverless.yml. At this stage, you should see a blank return.

In order to test, at this stage lets manually load a document to our S3 Storage. Any .docx will suffice at this stage.

Once the file is uploaded we can now re-check our logs to see if our service executed

This time we should see something returned similar too

This essentially tells us our function has executed.

Add ReadMe

Although not essential lets add a README.md to our code. This will help us to document our code and leave important information for other developers.

At this stage We’ll simply add some basic information, but we will keep this file up to date during our development process.

We can now rebuild and deploy our Lambda. Once our deployment is finished lets go to the Serverless Platform and go an inspect the information that is now Available.

Configure Lambda Service

We will start by introducing some of the basic features of the Serverless Framework that as a developer you will more than likely use during your development process.

I have to admit, that although I do like the Serverless Framework, I do find the documention to quite light in general and even more so when it comes to C#, there are very few if any samples available.

First, we will configure our Lambda using the serverless.yml.

There is a lot going on in this code snippet, so it is worth explaining. The service tag is used to define a name for the service.

The custom is just a handy area to declare any variables that you would like to use later. In my case I have used it to define a name for my S3 Bucket, which I want to name rea-cv-upload.

The provider is the area which you use deine attributes of your application, with things like the runtime platfrom, region and whether the application is dev or production.

iamRoleStatements is where you define the permissions for your lambda, check out IAM in serverless documentation and the What is IAM? for more details.

The functions is where the important bits for our application in its present form. I have defined a process function and have delegated it’s handler to a method in my Handler.cs which is also name Process

The events is where I define that my Lambda will watch my S3 bucket , the name of which I get from my Custom Variable defined earlier, if any files are created with a .docx extension then my lambda will leap into action.

We can now go edit the Handler.cs slightly in order to conduct a test and highlight another feature of the Serverless Framework. Our updated Handler now looks like this:

serverless-platform

You can now see the added value that the Serverless platform provides in how it also helps us to document our service.  At a glance on the left hand side, we’re able to see what resources our lambda uses, how many functions are defined and the subscriptions to other services it has.

Our README.md is also displayed so we can relay other important information.

CloudFormation

You have probably noticed that Serverless and specifically the serverless.yml takes care of all your AWS configuration details. So when creating Serverless applications with the Framework there is no need to go an create and manage all your dependent resources individually. This can all be done by editing the serverless.yml

For instance, if I wanted to use DynamoDB functionality within my serverless application. I could quite simply edit my serverless.yml and add the additional resources as follows.

I could then simply deploy my update and DynamoDB table will be available. You can also inspect your CloudFormation details in your AWS Console

The core key benefit of the the Serverless Framework is that it helps you to configure and manage your serverless stack. Enabling you to focus on developing the functionality you need without having to worry about infrastucture details.

When you deploy your Serverless framework application, it handles everything for you! That is true serverless mindset at work!

Using AWS SDK

The thing that stumped me most when I started using the Serverless Framework, is that out the box, it didn’t come with support AWS Events etc. Although I could easily manage and configure my AWS serverless stack, the library didn’t seem to come shipped with the AWS SDK built in. After thinking about this for a while I concluded that this actually makes sense. In all probability most of the functionality in your Lambda won’t be focused on interacting with AWS components, and it would probably be in-efficient to ship the library with every single Lambda. So for good reason, it is up to the developer to include this should they need it.

For developers including libraries, it is just a case in importing a package using nuget package manager. In my particular case, I wanted to listen to events when documents are added to S3, my lambda is already configured for that, and whenever a Word Document is added to S3 my lambda is triggered. However, currently my lambda is not able to get any details about the document etc.

So I need to add a reference to the Amazon.Lambda.S3Events and while I’m at it, I need to add a reference to the AWSSDK.DynamoDBv2 library so I can save whatever details I need to my DynamoDB Bucket.

It is basically a simple case of using nuget to add these packages. I’ll do so by either using the command line or in my particular case I using Jetbrains Rider – A fast & powerful, cross-platform .NET IDE great built in Nuget capability.

I can implement an initial version of my Lambda code, using some additional libraries I have developed to handle the parsing of documents.

Conclusion

Hopefully this post has covered some important aspects of creating Serverless Application using the Serverless Framework and .net core. The Serverless Framework is a great framework to increase developer productivity.

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.


Buy me a coffeeBuy me a coffee
π