I have an AWS Lambda that was built as a .NET 8 Minimal API using AWS.Lambda.PowerTools
. The function works perfectly when I run it locally in Visual Studio 2022. It’s able to read my appsettings.{environment}.json
file and execute the function accordingly.
But the reading of the appsettings doesn’t appear to be happening after I’ve deployed it to my AWS account and executed it via the URL.
Here’s what the config initialization code looks like in my app:
var netEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Logger.LogInformation($"Using .NET environment '{netEnv}'...");
IConfigurationBuilder configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(Constants.RootAppSettingsFile, false, true)
.AddJsonFile(string.Format("appsettings.{0}.json"), false, true);
IConfigurationRoot config = configBuilder.Build();
The resulting CloudWatch log verifies that netEnv
is being set to “Development”, as expected. Thus, appsettings.{0}.json
is resolving to appsettings.Development.json
as expected.
I’ve also downloaded the app code zip directly from the Lambda hosted in my AWS account and verified that appsettings.Development.json
is at the root of the folder (at the same level as my app’s binaries) – as well as verifying that this appsettings.Development.json
is populated with the right values.
However, in my code, after the config setup, if I serialize and log the contents of config
to CloudWatch as follows:
Logger.LogInformation($"using config '{JsonSerializer.Serialize(config)}'");
Instead of seeing the contents of config
(i.e. what’s in my appsettings file), I merely see this:
{
"timestamp": "2024-05-22T15:29:10.2626946Z",
"level": "Information",
"service": "service_undefined",
"name": "AWS.Lambda.Powertools.Logging.Logger",
"message": "using config '{"Providers":[{},{}]}'"
}
Basically, the hosted app thinks that the only thing in config
is {"Providers":[{},{}]}
.
Why is it not reading my config data?
(Btw, as an added debugging step, I also logged the output of Directory.GetCurrentDirectory()
. Instead of logging a string indicating my app’s current directory, it merely shows "{}"
.
Why is this happening? What do I need to fix to ensure that the deployed Lambda reads the appsettings file?