Dotnet Example

Introduction

This is an example of a very simple lambda written for the .NET Core CLR. The current implementation requires that the lambda author build and publish their dependencies into a directory and zip both the executable and all of it's dependencies into an artifact to be inflated and executed.

We're currently using a docker container built from the debian:jessie, but more specifically we're using the following base : buildpack-deps:trusty-scm.

In order to remain consistent, lambda authors should use the following container to build and publish their executables : galacticfog/dotnet-build:1.0.1 which is also based on the above listed docker images.

The following is a brief example of how to create a .NET lambda.

Requirements

  • docker must be installed on the system
  • artifact hosting like Amazon S3 or similar
  • Gestalt Platform access

HelloWorld .NET lambda

Simplest possible .NET lambda. Note that you must implement Main(string[] args) entry point in order for your lambda to be executed by the system. The code and Makefile for this lambda is container in Hello World .NET

Program.cs

In .NET you create a .cs file that contains the business logic for your application. An example of hello world is below

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var eventString = args[0];
            Console.WriteLine("Event : " + eventString );
        }
    }
}

Hello.csproj

In .NET you emplay a .csproj to define how to build your project. Here is the example for this particular example :

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <RuntimeIdentifiers>debian.8-x64</RuntimeIdentifiers>
  </PropertyGroup>
</Project>

Build the Project

In order to ensure that the versions of the .NET core runtime matches the executable that is created, we've provided a build container that will allow you to build your executable against a good runtime.

While you're in the directory that contains your project you can run the following command to build and publish your lambda :

> docker run --rm -v "$PWD":/hello_world -w /hello_world galacticfog/dotnet-build:1.0.1 /bin/bash -c "dotnet restore && dotnet publish -o publish -c release -r debian.8-x64"

Note that there is a Makefile included with the example in the github repository that encapsulates this build process (for future reference).

Package the artifact

We will now take our executable and all its dependent assemblies and package them up into a .zip file that will serve as our lambda artifact. While you're in the working directory you can run the following command to create the zip :

zip -r hello_world.zip publish

This will take the contents of your publish directory and create a zip file. Note that the relative path to your binary executable after inflation will be publish/Hello.

Publish the artifact

The artifact will need to reside somewhere that Gestalt Laser can access via HTTP. This is typically an S3 bucket, or some other internal CDN that can host binary artifacts. This example will use an S3 bucket for this.

Define the Lambda in Gestalt

  1. Navigate to your desired Environment and on the Create Menu Click Create Lambda
  2. Select the desired Lambda Provider and Name
  3. Select the a Dotnet Runtime and select Package Type
  4. Specify the Handler as publish/Hello
  5. Specify the Package URL as the place where you hosted your artifact
  6. Make sure to click the Compressed Package radio box to indicate that your artifact needs to be inflated.

No other options are technically necessary for this example

Expose an API Endpoint

Now that we've created our Slack Lambda we need to expose an api endoint.

  1. On the same Lambda screen, in the Public Endpoints section click Add Endpoint
  2. Select an API and click Next
  3. Enter a Relative Path e.g /hello-dotnet
  4. Check only the GET Allowed HTTP Method
  5. Check the Rate Limit and enter 60 (the number of requests allowed per minute)
  6. Click Finish
  7. Click or copy the Public URL which you will use to make your REST API call

Now click on the URL and see the output of your first Hello World in .NET!