AWS Developer Tools Blog

Environment Variables with .NET Core and Elastic Beanstalk

Along with Elastic Beanstalk’s recent release of adding Linux support for .NET Core, the Beanstalk team has also been working to standardize the support for environment variables across both the Linux and Windows .NET Core Beanstalk platforms. This means using the latest platform version for either the Linux or Windows, you can set environment variables on the Beanstalk environment. The applications that have been deployed to the environment will be able to access the new values without redeploying the application.

Setting Environment Variables

Environment variables can be set in the AWS management console under the configuration section of your environment.

Elastic Beanstalk console environment properties

Environment variables can also be set using any of the AWS SDKs, CLI or PowerShell. Below is an example using the AWS.Tools.ElasticBeanstalk PowerShell module for setting the same environment variables as seen in the console above.

$env1 = New-Object Amazon.ElasticBeanstalk.Model.ConfigurationOptionSetting
$env1.Namespace = "aws:elasticbeanstalk:application:environment"
$env1.OptionName = "EnvName1"
$env1.Value = "EnvValue1"

$env2 = New-Object Amazon.ElasticBeanstalk.Model.ConfigurationOptionSetting
$env2.Namespace = "aws:elasticbeanstalk:application:environment"
$env2.OptionName = "EnvName2"
$env2.Value = "EnvValue2"

$connectionStringEnv = New-Object Amazon.ElasticBeanstalk.Model.ConfigurationOptionSetting
$connectionStringEnv.Namespace = "aws:elasticbeanstalk:application:environment"
$connectionStringEnv.OptionName = "ConnectionStrings__Default"
$connectionStringEnv.Value = "this_is_my_conn_string"

Update-EBEnvironment -Region us-west-2 -ApplicationName "EnvTest" -EnvironmentName "EnvTest-dev" -OptionSetting @($env1, $env2, $connectionStringEnv)

Access environment variables through IConfiguration

.NET Core applications typically use the IConfiguration interface to access configuration values. IConfiguration pulls the config information from a variety of sources like appsettings.json and environment variables.

In the example above for setting environment variables the default connection string environment variable had __ (double underscores) in the name. This is the convention that .NET Core uses to create nested configuration values inside .NET Core’s IConfiguration framework. In my case I want to set the Default value in the ConnectionString section of IConfiguration. In appsettings.json this would be a nested JSON object but for environment variables the nesting is represented by using the __ token.


Environment variable support is available with the new Linux .NET Core platform released last week and with version 2.5.7 of the Windows .NET platform released on June 29, 2020. For more information about these platforms check out the Elastic Beanstalk developer guide.