Access AWS ElasticBeanstalk Custom Environment Variables with .NET Core WebApp
Previously, Elastic Beanstalk didn't support passing environment variables to .NET Core applications and multiple-application IIS deployments that use a deployment manifest [1]. The Elastic Beanstalk Windows Server platform update on June 29, 2020 [2] now fixes this gap. For details, see Configuring your .NET environment in the Elastic Beanstalk console [3].
[1] https://docs-aws.amazon.com/elasticbeanstalk/latest/dg/dotnet-manifest.html
[2] https://docs.aws.amazon.com/elasticbeanstalk/latest/relnotes/release-2020-06-29-windows.html
[3] https://docs-aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_NET.container.console.html#dotnet-console
Based on my research and testing, this is a deficiency in AWS Elastic Beanstalk for ASP.NET Core 1.1 applications. Just ran into this issue today and the only way to solve it is to load the config that AWS writes (if it's there) using the ASP.NET ConfigurationBuilder and parse it.
AWS should eventually fix this, until then you can use the method I'm using:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile(@"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var config = builder.Build();
builder.AddInMemoryCollection(ParseEbConfig(config));
Configuration = builder.Build();
}
private static Dictionary<string, string> ParseEbConfig(IConfiguration config)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (IConfigurationSection pair in config.GetSection("iis:env").GetChildren())
{
string[] keypair = pair.Value.Split(new[] { '=' }, 2);
dict.Add(keypair[0], keypair[1]);
}
return dict;
}