Aws-lambda-dotnet: Add support for app settings

Created on 14 Feb 2018  路  21Comments  路  Source: aws/aws-lambda-dotnet

Would this be possible?

guidance modullambda-client-lib response-requested

Most helpful comment

When .NET core 2.0 is deployed on to lambda, it doesn't honor the environment variables as far as appSettings.{env.EnvironmentName}.json is concerned and always picks appSettings.json. However the environment variable for ASPNETCORE_ENVIRONMENT is correctly read in rest of the program, is this the known issue?

All 21 comments

Can you give more details about what you mean by support for app settings?

This may be instead a request for the AWS Lambda team, regarding this issue:
https://stackoverflow.com/questions/46879797/app-config-in-an-aws-lambda-function
(App.config is not respected by Lambda).

I noticied the issue when using System.Configuration.ConfigurationManager nuget package in a .net core 2.0 Lambda; the ConfigurationManager loads the app settings when running locally, but it errors when being run by Lambda. I don't know enough about the Lambda .net core 2.0 runtime to know if it would be possible to modify the deploy/package utilities to support either an app.config file getting deployed to the proper location so as to be picked up by the Lambda .net core runtime, or to add some type of custom app settings parameter (to allow separating out credentials, etc to a separate file from source code).

Any reason you can't use environment variables for your deployed lambdas? Dotnet core has good support for merging all the places it can get config from, so we set ours up to pull in environment variables last. Devs can use app config locally during development, but then the deployed ones skip that and we set everything in environment variables. As a bonus, if you find you need to tweak a config setting on a deployed lambda (for instance, to turn up your logging level temporarily), you can do it via the console (or terraform, or whatever you use to manage infrastructure) instead of having to redeploy the whole package.

@noahhai My solution ended up being to just move away from ConfigurationManager references in my app and put everything in environment variables. It took a few days but in the end it was worth it. Environment variables are the way to go for configuration instead of App.Config. It makes it easier to run your app in a Docker Container or to stick it in Lambda. I'd suggest just biting the bullet and moving your configuration to environment variables if possible.

Does this issue also affect AppSettings.json and AppSettings{environment}.json etc? I don't think only supporting environment variables is a robust enough solution. dotnet core with docker and ECS etc has excellent support using appsettings for configuration as well as environment variables, and forcing people to move to environment variables only for lambda specifically makes it a lot less desirable.

Bit confusing why the ASP.NET Core 2.0 blue prints have explicit support for appsettings, but not the plain old dot net core ones as far as I can tell.

When .NET core 2.0 is deployed on to lambda, it doesn't honor the environment variables as far as appSettings.{env.EnvironmentName}.json is concerned and always picks appSettings.json. However the environment variable for ASPNETCORE_ENVIRONMENT is correctly read in rest of the program, is this the known issue?

Can you give more details about what you mean by support for app settings?

I think this refers to the fact that dotnet lambda package doesn't include appsettings.json by default when it publishes your app before zipping...

@noahhai you can probably add the following to your .csproj to enable this:

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </None>
  </ItemGroup>

Has anyone ever found a solution to either use the ConfigurationManager class that uses the app.config file, like in traditional .Net applications, or a workaround without having to change the references to the ConfigurationManager class, with .Net Core 2 and Lambda?

The reason why it would be helpful to us in Lambda, instead of moving away from using the ConfigurationManager class to use environmental variables, is because we have multiple EC2 instances that do use app.config and we want to be able to use the same library as it changes - it's our main CRUD library used for our users.

@raban33 Did you find the solution? I am experiencing the same issue.

Make your library .NetStandard2.0 + do not read settings directly, but instead always go through some "settings class".
Settings class only has properties, but underneath it uses some sort of helper class that will read the setting value given setting name and also convert to bool, string, int, ...

Now this helper class can access setting values from env variables, IConfiguration interface (look here https://www.nuget.org/packages/Microsoft.Extensions.Configuration/2.1.1) or System.Configuration.ConfigurationManager depending how you configure it.

Not ideal, but the same NetStandard2.0 code works on both .NetFx and .Netcore runtimes.

P.S. @Nodios: nice surname you have there :)

This issue should be renamed. The OP was asking about an app.config file specifically, not the appsettings.json file from .NET Core.

As for the appsettings.json issue some people are having (@Nodios & @raban33) I was running into an issue as well. My environment specific file wasn't being picked up. After some digging I realized AWS is running .NET Core on Linux and the file system is case sensitive. Changing my environment variable ASPNETCORE_ENVIRONMENT to have the same case as the settings file appsettings.<EnvironmentName>.json fixed the issue.

Here is what I have found. My appsettings.json is set to Always Copy. I can see that both files:

... zipping: appsettings.Development.json
... zipping: appsettings.json

are included in the AWS Lambda ZipFile. However, when I write out the appsettings.json I get this one (Is this a default appsettings.json?)

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information"
}
}
}

So it looks like the copied appsettings.json is not getting into the Lambda app execution directory.

As a test, I created myconfig.json with the contents from AppSettings.json

Then in statup.cs use this file as in:

            var config = new ConfigurationBuilder().AddJsonFile("myconfig.json").Build();

it all works fine.

I am having the same issue, I was reviewing the cloudwatch logs and I saw the following log:

zipping: adding: appsettings.Staging.json (deflated 60%)

But for some reason the configuration of appsettings.Staging is not being read. The lambda has the env variable ASPNETCORE_ENVIRONMENT=Staging. Does anyone have the same issue?

Hi @noahhai,

Good morning.

I was going through issue back log and came across this one.

From high level, it doesn't appear to be Lambda causing the issue, but the issue with .NET Core 2.0. Please let me know if you have tried this in .NET Core 3.1 and if this is still the issue. Also refer to input by @genifycom. If this is no longer the issue, kindly confirm if it could be closed.

Thanks,
Ashish

I am facing same issue with dot net core 3.1. The configurationManager.ConnectionStrings loads fine from app.config when running from "LocalEntryPoint.cs" or when running unit test that runs "LambdaEntryPoint.cs". However, when the app is deployed in aws, the connection strings fail to load. I have verified that the config is deployed with the app in aws.

Thanks,
Ratul

Hi @noahhai,

Please refer the link Add .NET Core DI and Config Goodness to AWS Lambda Functions on guidance on how to include appsettings.json files in Lambda.

I don't think reading app.config issue is specific to AWS Lambda but a limitation of .NET Core. Please refer the link File Configuration Provider section on Microsoft documentation home page for more details. Hope this helps.

Thanks,
Ashish

Hi @ashishdhingra ,
I am using the following
AWS Template: Aws Serverless Application With Tests
AWS Blueprint: ASP.Net Core Web Api
Dot Net Core 3.1

As I mentioned in my previous comment, app.config is working fine when I run the web api application locally using visual studio. Also the unit test for default ValuesController works fine and is able to load the data from app.config when run locally using visual studio. The issue occurs when the web api application is deployed in AWS lambda. It throws a null reference exception. I have verified by downloading the application files and ensured that app.config is being deployed.
Can you please advise?

Thanks,

Hi @ratulb87,

Using the AWS Serverless application with Tests in Visual Studio 2019 generates appsettings.json (and appsettings.Development.json) by default. Kindly advise on how in your case app.config is generated. Did you add this file manually or was added as a part of migration from previous version, and then trying to parse the same. .NET Core 3.1 doesn't appear to support app.config by default. Since you are using Windows environment, you might be able to use the System.Configuration.ConfigurationManager class, but Lambda is deployed on Linux EC2 instances behind the scenes, hence it might not work as expected. However, as mentioned in the section "XML configuration provider" in Microsoft documentation at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#xml-configuration-provider, you might try using XmlConfigurationProvider class and treat your app.config as general XML file to read configuration properties.

Please give it a try and let me know the outcome.

Thanks,
Ashish

Hi @ashishdhingra ,
I was able to get this to work by putting the settings parameters from app.config to appsettings.json.
Thanks,
Ratul

Closing issue based on guidance already provided.

Was this page helpful?
0 / 5 - 0 ratings