AWS Elastic Beanstalk environment variables in ASP.NET Core 1.0

后端 未结 9 2027
误落风尘
误落风尘 2020-12-01 02:59

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 follo

9条回答
  •  一个人的身影
    2020-12-01 03:25

    .NET Core 2 + posrgresql RDS

    Further to @sebastian's great answer above, I found that the settings were in a different part of the file, viz. plugins:rds:env.

    Also there was no need to split on =, so the parsing code I have is:

    private static void SetEbConfig()
            {
                var tempConfigBuilder = new ConfigurationBuilder();
    
                tempConfigBuilder.AddJsonFile(
                    @"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration",
                    optional: true,
                    reloadOnChange: true
                );
    
                var configuration = tempConfigBuilder.Build();
    
                var ebEnv = configuration.GetSection("plugins:rds:env")
                                            .GetChildren()
                                            .ToDictionary(child => child.Key, child => child.Value);
    
                foreach (var keyVal in ebEnv)
                {
                    Environment.SetEnvironmentVariable(keyVal.Key, keyVal.Value);
                }
            }
    

    The relevant (and redacted ;-)) JSON is as follows:

    {
        "plugins": {
            "rds": {
                "Description": "RDS Environment variables",
                "env": {
                    "RDS_PORT": "....",
                    "RDS_HOSTNAME": "....",
                    "RDS_USERNAME": "....",
                    "RDS_DB_NAME": "....",
                    "RDS_PASSWORD": "...."
                }
            }
        }
    }
    

    (This reply is separate since I don't have rep to comment...)

提交回复
热议问题