Skip to content

Instantly share code, notes, and snippets.

@jbasinger
Last active March 30, 2021 21:26
Show Gist options
  • Save jbasinger/9302d64c19ac11bd51ed981cff177b86 to your computer and use it in GitHub Desktop.
Save jbasinger/9302d64c19ac11bd51ed981cff177b86 to your computer and use it in GitHub Desktop.
public class LambdaApplication
{
public string DoTheJob(string input)
{
Console.WriteLine(input);
return input.ToUpper();
}
}
version: "3.8"
services:
lambda:
container_name: lambda
build: .
image: com.sciencevikinglabs.lambda
stdin_open: true
tty: true
FROM public.ecr.aws/lambda/dotnet:5.0
#You can alternately also pull these images from DockerHub amazon/aws-lambda-dotnet:5.0
# Copy function code
COPY net5-lambda-template/bin/Debug/net5.0 ${LAMBDA_TASK_ROOT}
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "net5-lambda-template::net5_lambda_template.LambdaFunction::Handler" ]
using Amazon.Lambda.Core;
using Microsoft.Extensions.DependencyInjection;
using net5_lambda_template.Core;
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace net5_lambda_template
{
public class LambdaFunction
{
public string Handler(string input)
{
var host = new LambdaHost(null);
var services = host.HostBuilder.Build().Services;
var app = services.GetService<LambdaApplication>();
return app.DoTheJob(input);
}
}
}
using Amazon.Lambda.Core;
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace net5_lambda_template
{
public class LambdaFunction
{
public string Handler(string input)
{
Console.WriteLine(input);
return input.ToUpper();
}
}
}
public class LambdaHost
{
public IHostBuilder HostBuilder => GetHostBuilder();
private IHostBuilder _hostBuilder;
private readonly string[] _hostArguments;
public LambdaHost(string[] args)
{
_hostArguments = args;
}
private IHostBuilder GetHostBuilder()
{
if (_hostBuilder != null)
return _hostBuilder;
_hostBuilder = Host.CreateDefaultBuilder(_hostArguments)
.ConfigureServices(ConfigureServicesInternal);
return _hostBuilder;
}
private static void ConfigureServicesInternal(IServiceCollection services)
{
services.AddTransient<LambdaApplication>();
}
}
dotnet build .\net5-lambda-template.sln
docker-compose build
docker-compose run -p 9000:8080 lambda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment