The 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
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 Open Source 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
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
Although it is not absolutely necessary to have a Serverless Enterprise account, you can still develop and deploy Serverless Applications using the Serverless Framework without one, you may find the Serverless Enterprise to be quite a handy platform to help you on your company on the path to adopting Serverless Architecture.
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
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:
To create a lambda project and instruct Serverless to use the C# template we provide a
This will generate a project for us comprising a number of files to a directory created hello-world.
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 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 worth taking the time to read through this, in order to gain a broader understanding of what is available.
build.sh is important for when you want to build your serverless application in preparation for deployment.
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
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.
We have included libraries in our lambda which will assist in the actual parsing of documents and creating the relevant JSON objects to be stored in dynamoDb.
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
This is also the function name we defined in
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. Its a great short cut to have when you’re testing your function. However, if you would like to see more detail logs or logs for previous versions of your lambda, you can view the logs generated in your AWS CloudWatch, the Serverless Framework has created this all for you!
Navigating to your CloudWatch in your AWS Services console and Selecting Logs for for Lambda service you will be able to see the detailed view.
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.
If you have a Serverless Framework Enterprise Account your ReadMe will be displayed as follows along with a break down of all the services and features and usage statistics of your Lambda.
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
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.
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.
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
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:
If you don’t like the idea of instantiating objects within your Lambda Functions check out how to implement dependency injection in AWS Lambdas
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.
The README file we created will help to relay any other important information we need to share with other developers on our team.
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
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.
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.
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.
Latest posts by Gary Woodfine (see all)
- Using Environment Variables in Vue.js - June 24, 2019
- Using Vuex and Vue-Router in a Vue Project - June 20, 2019
- Building Custom Entity Recongnition with AWS Comprehend – Part 1 - May 28, 2019