I've previously discussed how to develop a .net core AWS Lambda with the serverless framework to provide an abstraction over the underlying operating system and execution runtime so that you can more easily achieve economies of scale using Cloud computing paradigms.
In this post, I will expand on these concepts and provide an introduction on how you can introduce concepts like dependency injection and configuration to your lambda functions.
Serverless Framework enables you to easily create and manage resources used by your Lambda components and creates a simple stub function enabling you to focus on developing your function.
The one problem with this is that developers may perceive that they can't implement all the usual good things you would in any other development project and judging from some of the code I have had to deal with out in the wild this certainly is true!
AWS Lambda and .net core
AWS Lambda supports a number of programming languages and runtimes, well custom runtimes which enable the use of any language and execution environment. Included in the list of standard runtimes is Microsoft .NET Core, an open-source cross-platform runtime, which you can build apps on using the C# programming language.
When generating a project using the serverless framework template is that is lacks the usual hooks for setting up configuration and dependency injection. Youâ€™re on your own for adding the necessary code. I will walk you through the process of implementing all the boiler plate code required.
To generate a new serverless lambda project we'll use the following Serverless command:
To learn how to install the Serverless Framework and what these commands do I urge you to read Getting started with .NET Core and the Serverless Framework
Once the project has been generated we can set to work clearing out the
Handler.cs and remove most of the code there so we're only left with the following
Add .net core Dependency Injection to lambda
Anytime you directly create an object you are coupling yourself to a specific implementation. So it stands to reason that youâ€™d want to apply the same Inversion of Control pattern and .NET Core Dependency Injection goodness to your AWS Lambda Functions as you would in a standard ASP.NET Core Web API project.
The additional benefit of making use of IOC and Dependency Injection code in an AWS Lambda project is that you can eventually make use of AWS Lambda Layers which enable you to configure your Lambda function to pull in additional code and content in the form of layers.
A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package.
First we need to wire up dependency injection, so in order to do that add the following references to our project
Note the version number, this is due to fact AWS lambda currently only supports .net core 2.1.0.
In our next step lets add a new Interface which we'll name
Implement Lambda Configuration
We can now add a class that will implement the interface we'll imaginatively call this
Initially we are going to enable our Lambda to read configuration settings from an
appsettings.json file so before we add the code to do so add the file to your project. Just add an empty
json file, by convention .net developers name this file
appsettings.json but you can call it whatever you want.
Your aws-cshapr.csproj should now look something similar to this
We can edit the our LambdaConfiguration to get data from our appsettings
We can now complete the code from our simple lambda. So edit the Handler.cs to contain the following code.
All this code is basically doing is wiring up the dependency injection and configuring the application. On line 20 we have created a simple Hello lambda method and the only thing it does is read a value from the appsettings file, a key which we have called hello.
Our App Settings file is rather simple and looks like
Build and Deploy the Lambda
We can now build and deploy our lambda using Serverless Framework. I typically work on my Linux box so I will build the application making use of the
build.sh. I have kept the yaml file really simple and it contains the following
Using the build command
Then once the build has completed we can simply deploy our application to AWS using
after the deploy has completed we can simply test our function by invoking it as follows
We should then see the a response printed of whatever the value you placed in the appsettings file
In this simple tutorial we have discovered how to implement a basic implementation of Dependency Injection and we have also provided an example of how you can make use of application configuration to make use of appsettings to store addtional configuration information for your AWS Lambda and read this information at run time.
The objective of this article was just to introduce you to the concept that you can implement and use Dependency Injection with AWS Lambdas, if you would like to read more about Advanced dependency in AWS Lambda and .net core