How to Deploy dotnet core functions using Serverless?

api-gateway
lambda
#1

I am finding it a challenge to properly package and deploy dotnet functions (using dotnet core 2.1 runtime) utilizing Serverless to AWS Lambda. I am not finding any examples other than ones that use the SAM and dotnet deploy lambda-serverless commands.

Example: https://stackoverflow.com/questions/50449530/how-do-you-package-up-a-visual-studio-aws-serverless-project/50453340#50453340

Using command line and Serverless, what needs to be done to properly deploy a dotnet core function to AWS Lambda? Is this even possible?

0 Likes

#2

I was finally able to overcome my issues.

  1. cd into .csproj folder
  2. dotnet restore
  3. dotnet lambda package using dotnet lambda tools dotnet tool install -g Amazon.Lambda.Tools
  4. Assuming the rest of your serverless.ymal is set up correctly, make sure your serverless.yml has a package property with an artifact that points to the .zip file generated by dotnet lambda package, for example:
package:
  artifact: ./<projectFolderName>/src/<projectName>/bin/Release/netcoreapp2.1/<zipFileName>.zip
  1. You need a Lambda Entry Point class (e.g. LambdaEntryPoint.cs) that uses a Startup class:

LambdaEntryPoint.cs example

using Microsoft.AspNetCore.Hosting;

namespace MyNameSpace
{
    public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
    {
        protected override void Init(IWebHostBuilder builder)
        {
            builder.UseStartup<Startup>();
        }

        ...
}

Startup.cs example

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

public class Startup
{
    private readonly IConfiguration _configuration;

    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    // This method gets called by the runtime. Use this method to add services to the container
    public void ConfigureServices(IServiceCollection services)
    {

    }

    /// <summary>
    /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
    /// </summary>
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {

    }
}

Note: some of this can be generated from templates.

  1. do a regular sls deploy

Outside of what is readily available on the Internet, these steps highlight some of the hurdles I had to overcome to get mine working.

0 Likes