Neil
Neil

Reputation: 8584

Using Custom AWS Lambda Runtimes with asp.net core

Recently amazon announced support for a custom .net core runtime: https://aws.amazon.com/blogs/developer/announcing-amazon-lambda-runtimesupport/

My current aws lambda project is written C# using the .NET Core 2.1; 2.2 is not supported yet hence I'd like to use custom runtime.

However, it seems that in order to use the custom runtime, it is necessary to use LambdaBootstrap. From the docs it is not clear to me if it is possible to use it alongside asp.net core: https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.RuntimeSupport

p.s. currently my aws lambda is using asp.net core for authentication and other

Upvotes: 1

Views: 1875

Answers (1)

Liam
Liam

Reputation: 5476

Here's a good demo / tutorial that shows how the author approaches using the lambda bootstrap:

https://medium.com/@dimoss/asp-net-core-2-2-3-0-serverless-web-apis-in-aws-lambda-with-a-custom-runtime-and-lambda-warmer-ce19ce2e2c74

The author uses the following code to switch between local debugging and the lambda entry:

public class LocalEntryPoint
{
    private static readonly LambdaEntryPoint LambdaEntryPoint = new LambdaEntryPoint();
    private static readonly Func<APIGatewayProxyRequest, ILambdaContext, Task<APIGatewayProxyResponse>> Func = LambdaEntryPoint.FunctionHandlerAsync;

    public static async Task Main(string[] args)
    {
#if DEBUG
        BuildWebHost(args).Run();
#else
        // Wrap the FunctionHandler method in a form that LambdaBootstrap can work with.
        using (var handlerWrapper = HandlerWrapper.GetHandlerWrapper(Func, new JsonSerializer()))

        // Instantiate a LambdaBootstrap and run it.
        // It will wait for invocations from AWS Lambda and call the handler function for each one.
        using (var bootstrap = new LambdaBootstrap(handlerWrapper))
        {
            await bootstrap.RunAsync();
        }
#endif
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

Upvotes: 2

Related Questions