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?
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();
}
}
Here's an approach using ebextensions & instance metadata:
Use ebextensions to manage customisation of the server before your app starts.
ec2:DescribeTags
permission.init.config
file inside .ebextensions
folder off the root.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"
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.
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/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.