I deployed .net core 2 application on aws eb. I need staging, qa and prod servers, and don't how to handle connection strings? Is it possible to store it in env variables on eb? Or is it possible to make env for these three instances and to have different application.json file for each?
4 Answers
You can use Options pattern in ASP.NET Core
Example you have appsetting.json like this
"WebAppUrl": {
"Staging": "someurl",
"Qa": "someurl",
"Prod": "someurl"
}
I will need to define a model class like this to store information
public class WebAppUrl
{
public string Staging { get; set; }
public string Qa { get; set; }
public string Prod { get; set; }
}
So we have the the config structure. The last thing we need is register inside Startup.cs
services.Configure(configuration.GetSection("WebAppUrl"));
So you can use like this
private readonly IOptions<WebAppUrl> _webAppUrl;
public EmailSender(IOptions<WebAppUrl> _webAppUrl)
{
_webAppUrl = _webAppUrl;
}
_webAppUrl.Value.Staging;
_webAppUrl.Value.Qa;
_webAppUrl.Value.Prod;
Ok this is how Microsoft setup multiple appsetting.json file. You can take a look at because it's kinda long post
Here is how I config using json file base on enviroment
public Startup(
IConfiguration configuration,
IHostingEnvironment hostingEnvironment)
{
_configuration = configuration;
_hostingEnvironment = hostingEnvironment;
var builder = new ConfigurationBuilder();
if (_hostingEnvironment.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
else
{
builder
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
}
}

- 19,166
- 4
- 38
- 60
-
This is part of the possible solution. The main problem is with making env variables on elastic beanstalk for .net core application, somehow eb cannot handle it. P. S. As I read best practice is to store it in different `appsettings.json` files (`appsettings.Stanging.json`, `appsettings.Qa.json`...) – Miroslav Sekulic Aug 16 '19 at 08:07
Here's an approach using ebextensions & instance metadata:
Use ebextensions to manage customisation of the server before your app starts.
- Your instance should have a role which has the
ec2:DescribeTags
permission. - Configure your EB application to set an environment tag.
- You either have an application config file with connection strings per environment, or you use something like AWS Secrets manager (your instance role will need permission for this service) to hold your connection strings (better security).
- The application code uses an environment variable to select the correct connection string from the config file, or Secrets manager.
- You deploy your application with an
init.config
file inside.ebextensions
folder off the root. - The init.config reads instance metadata to determine the environment and set the environment variable.
- Your app starts, reads the variable, pulls the connection string and connect to correct db.
Below example is init.config for windows instance with powershell, but can be adapted to bash + aws cli.
The first step writes a powershell script out. The script reads the Environment tag and writes the value to a machine wide variable for the app to pickup. Second step invokes the script.
files:
"c:/cfn/init.ps1":
content: |
$instanceDoc = Invoke-RestMethod 'http://169.254.169.254/latest/dynamic/instance-identity/document'
$nameTagList = Get-EC2Tag -Region ($instanceDoc.region) -Filter @{name='resource-type';value='instance'},@{name='resource-id';value=$instanceDoc.instanceId},@{name='tag:Environment';value='*'} | Select -First 1
$envName = $nameTagList.Value
[Environment]::SetEnvironmentVariable("APP_ENV", $envName, [EnvironmentVariableTarget]::Machine
container_commands:
00-invoke-init:
command: powershell.exe -nologo -noprofile -file "c:\cfn\init.ps1"

- 4,286
- 2
- 35
- 42
So, there is a problem with handling env variables from elastic beanstalk for .net core application.
Here is the answer (hack) how to force application to get your env variables AWS Elastic Beanstalk environment variables in ASP.NET Core 1.0
So, all sensitive data like connection string should be on elastic beanstalk env variables. Others can be in appsettings.Staging.json
, appsettings.Qa.json
, etc.
Just don't forget to add ASPNETCORE_ENVIRONMENT
var in env variables.

- 65
- 7
-
1Just like what I just update with my answer. Can you accepted that. Thanks – Tony Ngo Aug 16 '19 at 08:13
I would suggest putting configuration into AWS System Manager's Parameter Store. The Amazon.Extensions.Configuration.SystemsManager NuGet package adds Parameter Store as a configuration provider to the .NET Configuration system. Here is a blog post for more info. https://aws.amazon.com/blogs/developer/net-core-configuration-provider-for-aws-systems-manager/

- 2,964
- 14
- 13