Skip to content

Instantly share code, notes, and snippets.

@icardosos
Created June 5, 2018 12:31
Show Gist options
  • Save icardosos/e2bd2805b21f25c94794e427a6319821 to your computer and use it in GitHub Desktop.
Save icardosos/e2bd2805b21f25c94794e427a6319821 to your computer and use it in GitHub Desktop.
Enable hosting environment to asp.net on AWS Elastic Beanstalk
public static class HostingEnvironmentExtensions
{
public static void ConfigureAWSEnvironment(this IHostingEnvironment env)
{
var awsEnvVariablePath = "C:\\Program Files\\Amazon\\ElasticBeanstalk\\config";
var awsEnvVariableFile = "containerconfiguration";
if (!File.Exists(awsEnvVariablePath + "\\" + awsEnvVariableFile))
return; //não executar em dev
var builder = new ConfigurationBuilder()
.SetBasePath(awsEnvVariablePath)
.AddJsonFile(awsEnvVariableFile, optional: true, reloadOnChange: true);
var configuration = builder.Build();
var envVariables = configuration.GetSection("iis:env").GetChildren();
var envKey = envVariables.FirstOrDefault(v => v.Value.StartsWith("ASPNETCORE_ENVIRONMENT="));
if (envKey != null)
env.EnvironmentName = envKey.Value.Split('=')[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment