Skip to content

Instantly share code, notes, and snippets.

@secretorange
Last active August 29, 2023 18:52
Show Gist options
  • Save secretorange/710375bc62bbc1f32e05822f55d4e8d3 to your computer and use it in GitHub Desktop.
Save secretorange/710375bc62bbc1f32e05822f55d4e8d3 to your computer and use it in GitHub Desktop.
Get access to the Lambda alias at Startup which you can then use to load environment specific config
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using AWSBoot.Boot;
using Microsoft.Extensions.Configuration;
using Amazon.Lambda.AspNetCoreServer.Internal;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
namespace SecretOrange.Api
{
public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
private ILambdaContext LambdaContext;
public LambdaEntryPoint()
: base(AspNetCoreStartupMode.FirstRequest)
{
}
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public override async Task<APIGatewayProxyResponse> FunctionHandlerAsync(APIGatewayProxyRequest request, ILambdaContext lambdaContext)
{
LambdaContext = lambdaContext;
return await base.FunctionHandlerAsync(request, lambdaContext);
}
protected override void Init(IWebHostBuilder builder)
{
var alias = LambdaContext?.InvokedFunctionArn.Substring(LambdaContext.InvokedFunctionArn.LastIndexOf(":") + 1);
// Do stuff based on the environment
builder
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile($"appsettings.{alias}.json", optional: true, reloadOnChange: true);
})
.UseStartup<Startup>();
}
}
}
@secretorange
Copy link
Author

I had to change the AspNetCoreStartupMode to FirstRequest - not sure if there are any implications. Seems ok so far.

@secretorange
Copy link
Author

I'll leave this code here as it may help some people but note that I found AspNetCoreStartupMode.FirstRequest caused longer cold starts by a few seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment