12

We have set custom environment variables in the Elastic Beanstalk dashboard, under configuration=>software configuration=>"Environment Properties" section. In a C# MVC 5 project, we can just access these variables by looking for them with ConfigurationManager.AppSettings - that works great.

In .NET core, however, we don't use web.config anymore. We've been attempting to track down a way to access the environment variables, but all we've found is a nuget package called AWSSDK.Extensions.NETCore.Setup. However, this package does not seem to get us the access to the custom variables.

Any help would be greatly appreciated.

Javelin
  • 172
  • 11

2 Answers2

12

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;
    }
Ben Farmer
  • 695
  • 1
  • 6
  • 14
  • Thank you thank you thank you! I appreciate you doing all the work on breaking this out - it works quite well. It'd be great to supply a nuget package for people to use to get around this issue until AWS resolves it more permanently. – Javelin Jul 03 '17 at 17:17
  • Is there place where i can track this issue? – skorenb Jul 28 '17 at 07:31
  • 2
    What kind of god are you? – Jhayes2118 Sep 20 '17 at 00:39
  • 1
    just curious, how are you setting "env.EnvironmentName", since in your example, this must be available BEFORE you load the AWS configuration, right? – jeremyalan Feb 14 '18 at 15:34
  • I can't figure out how you actually use the configuration values (in a controller action for example) after doing this. – Nick Coad Feb 15 '18 at 04:48
  • See https://stackoverflow.com/a/47648283/60576 for a better solution that also works for including the correct JSON file for the environment. – realMarkusSchmidt Apr 11 '18 at 17:31
2

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