AWS Elastic Beanstalk environment variables in ASP.NET Core 1.0

amazon-elastic-beanstalk, asp.net-core, asp.net-core-1.0, asp.net-mvc, environment-variables

Solution

Had the same problem, and just received a reply from AWS support about this issue. Apparently environment variables are not properly injected into ASP.NET Core applications in elastic beanstalk.

As far as I know, they're working to fix the problem.

The workaround is to parse `C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration` into the configuration builder. This file is part of your elastic beanstalk environment and should be accessible upon deploying your project.

First add the file:

var builder = new ConfigurationBuilder()
    .SetBasePath("C:\\Program Files\\Amazon\\ElasticBeanstalk\\config")
    .AddJsonFile("containerconfiguration", optional: true, reloadOnChange: true);

Then access the values:

var env = Configuration.GetSection("iis:env").GetChildren();

foreach (var envKeyValue in env)
{
    var splitKeyValue = envKeyValue.Value.Split('=');
    var envKey = splitKeyValue[0];
    var envValue = splitKeyValue[1];
    if (envKey == "HelloWorld")
    {
        // use envValue here
    }
}

Courtesy of G.P. from Amazon Web Services

Problem

How do I get environment variables from elastic beanstalk into an asp.net core mvc application? I have added a .ebextensions folder with app.config file in it with the following: ``` option_settings: - option_name: HelloWorld value: placeholder - option_name: ASPNETCORE_ENVIRONMENT value: placeholder ``` The .ebextensions folder is included in the publish package. On deployment, both the variables are visible in the aws elasticbeanstalk console at Configuration > Software Configuration > Environment Variables However, when I try to read the variables in the application, none of the below options are working: ``` Environment.GetEnvironmentVariable("HelloWorld") // In controller Configuration["HelloWorld"] // In startup.cs ``` Any ideas on what I could be missing? Thanks.

Original source