Last active
June 6, 2023 13:06
-
-
Save PureKrome/fa52f50dceea86c761c37a7c65691954 to your computer and use it in GitHub Desktop.
A simple AWS Lambda for .NET7 + Dependency Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SomeLambdaFunction | |
{ | |
public class ApplicationSettings | |
{ | |
private const string ConnectionStringEnvironmentalVariableName = "MY_POSTGRES_CONNECTIONSTRING"; | |
public ApplicationSettings() | |
{ | |
var isMissingEnvironmentalVariableData = false; | |
var connectionString = Environment.GetEnvironmentVariable(ConnectionStringEnvironmentalVariableName); | |
if (string.IsNullOrWhiteSpace(connectionString)) | |
{ | |
Console.WriteLine($"Missing database connection string via Environment Variables: {ConnectionStringEnvironmentalVariableName}"); | |
isMissingEnvironmentalVariableData = true; | |
} | |
else | |
{ | |
PostgressDatabaseConnectionString = connectionString; | |
} | |
// TODO: Add more "config" entries here | |
if (isMissingEnvironmentalVariableData) | |
{ | |
throw new Exception("Failed to create application settings because missing at least 1 environmental setting."); | |
} | |
} | |
public string PostgressDatabaseConnectionString { get; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Amazon.Lambda.Core; | |
using Amazon.Lambda.RuntimeSupport; | |
using Amazon.Lambda.Serialization.SystemTextJson; | |
using Microsoft.Extensions.DependencyInjection; | |
// ***** | |
// Start of application initialize. AKA cold-start initialization. | |
// ***** | |
ApplicationSettings applicationSettings = new(); | |
IServiceCollection serviceCollection = new ServiceCollection(); | |
serviceCollection | |
.AddLogging() // For services that require an ILogger<T> via ctor injection. | |
.AddSingleton<IDatabase>(implementationInstance => | |
{ | |
Database database = new(applicationSettings.PostgressDatabaseConnectionString); | |
return database; | |
}); | |
// TODO: Add your own services here. | |
serviceColltion | |
.AddSingleton<IFoo, Foo>() | |
.AddSingleton<IBar, Bar>(); | |
var serviceProvider = serviceCollection.BuildServiceProvider(); | |
// ***** | |
// Function handler - where the main business logic goes. | |
// ***** | |
var handler = new Func<ILambdaContext, Task<bool>>(async (ILambdaContext context) => | |
{ | |
// NOTE: Can this be injected instead? Like, what happens if the host has to shutdown? | |
var cancellationToken = new CancellationToken(); | |
context.Logger.LogDebug("Starting some function - pew pew!"); | |
// Business logic goes here | |
return true; | |
}); | |
await LambdaBootstrapBuilder.Create<Task<int>>(handler, new DefaultLambdaJsonSerializer()) | |
.Build() | |
.RunAsync(); | |
// What happens if you don't want the Function to return anything? | |
// Use this instead! | |
//var handler = async (ILambdaContext context) => | |
//{ | |
//} | |
// | |
//await LambdaBootstrapBuilder.Create<Task>(handler, new DefaultLambdaJsonSerializer()) | |
// .Build() | |
// .RunAsync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment