4

Background & Attempted Fixes

I am attempting to automate my .NET Core web application deployment using AWS ElasticBeanstalk and configuring each environment with the ASPNETCORE_ENVIRONMENT variable.

I then found out that via numerous (one & two) Stackoverflow questions this is broken in ElasticBeanstalk.

Then following those two SO questions, I tried this in my Startup.cs:

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();

    Configuration = builder.Build();

    var ebConfig = ParseEbConfig(Configuration);
    builder.AddInMemoryCollection(ebConfig);

    Log.Logger = new LoggerConfiguration()                
        .ReadFrom.Configuration(Configuration)
        .CreateLogger();

    CurrentEnvironment = env;
}

public IConfigurationRoot Configuration { get; }
public IHostingEnvironment CurrentEnvironment { get; set; }

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;
}

and tried accessing the new containerconfiguration values in my HomeController (to test this out) like this: _config.GetSection("ASPNETCORE_ENVIRONMENT").Value; where _config is IConfiguration. This worked when debugging locally & I was able to get the ASPNETCORE_ENVIRONMENT properly. But in Beanstalk it was unable to find that variable.

What seems to be going on

So it appears that Beanstalk is unable to access the containerconfiguration file and it certainly is not able to retrieve the ASPNETCORE_ENVIRONMENT variable, no matter where I set it in my configurations with Beanstalk.

What I am wondering

At the end of the day, I want to be able to set the ASPNETCORE_ENVIRONMENT variable to Dev (for example) in my Beanstalk configuration and have my .NET application load the appropriate appsettings.json file for the environment. I don't necessary need to go the route of the containerconfiguration file, but this seems to be the way it needs to be done.

james
  • 5,006
  • 8
  • 39
  • 64

0 Answers0