Running .Net Core on AWS Lambda
AWS Lambda offers a convenient way of hosting your function on AWS with inherent elasticity and fault tolerance. Many are looking to see their favorite runtime natively supported in the platform. This post talks about how you could do that today, using .Net Core as an example.
Preparing a Workbench
In many cases, your app may require OS specific preparation, like compilation. To ensure compatibility with Lambda's underlying execution environment, we will be preparing a workbench based on a specific public Linux AMI version. This AMI is the same machine image being used by AWS Lambda.
For our workbench, you can use smaller instance type assuming the code base is small and the compiler do not require alot of resources.
After the instance is launched, login to the instance via ssh
ssh -i your.pem ec2-user@yourec2ipaddress
Next, we'd like to install .Net Core on our instance. The AMI we're using comes with an older version of
libpng than the version required by .Net Core. Furthermore, the included
libpng is required by the
openjdk, which also included in the AMI. Thus for our purpose, we will be removing both libraries and replace the
libpng with the version that .Net Core expects.
Last tested 10/13/2016 against .Net Core 1.0.1
With this out of the way, we can start with the installation of .Net Core.
Preparing your application
At this point, we have a working workbench for our application preparation. We may choose to install git to get our code pulled into our workbench. It's now time for our preparation, in our case this means compiling our source code.
Our Lambda environment won't have the required dependencies we just installed on our workbench, so we will need to package them together along with out compiled app.
Our app is going to wake up when the Lambda function is invoked then quickly terminates. Currently, AWS Lambda only supports Python, Node.JS or Java handlers. We will need to execute our .Net Core app from within a supported handler. In this case we will be using a Node.JS based handler:
We can then package our Lambda manually, or using your favorite deployment tool.
Handling Params and Return Values
We kept the launcher above relatively simple to show you the basic principles of the approach. In practice, you may want to extend our launcher to pass params from the
event object to our .Net Core app as command line arguments. The same goes for return values, we may want to parse our stdout, which could be a JSON object to something that the Lambda client expects.
In this post, we've covered how we can use AWS Lambda to execute apps built on runtime other than Python, Node.JS and Java, specifically we used .Net Core as an example. This approach opens up new possibilities to developers to embed their favorite runtime.