.NET Core AWS RDS CONNECTION

I am creating .NET Core API for Amazons AWS, Elastic Beanstalk. I am trying to add a database but their guide on adding a database does not work for .Net Core http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_NET.rds.html

He says to get the relevant information using "ConfigurationManager.AppSettings;", but this is not possible in .NET Core.

Can anyone provide some information on how to get information about the database? ("RDS_DB_NAME" "RDS_USERNAME" "RDS_PASSWORD" "RDS_HOSTNAME")

UPDATE I tried to read https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration But I didn't solve the problem. I still cannot get values ​​from AWS.

It just returns whatever I have set in my own appsettings.json Here is my code: MyOptions.cs

public class MyOptions
{
    public MyOptions()
    {
        // Set default value.
    }
    public string RDS_HOSTNAME { get; set; }
    public string RDS_PORT { get; set; }
    public string RDS_DB_NAME { get; set; }
    public string RDS_USERNAME { get; set; }
    public string RDS_PASSWORD { get; set; }
}

      

StartUp.cs

public void ConfigureServices(IServiceCollection services)
{
    // Register the IConfiguration instance which MyOptions binds against.
    services.Configure<MyOptions>(Configuration);
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();
}

      

HomeController.cs

namespace WebApplication2.Controllers
{
    [Route("")]
    public class HomeController : Controller
    {
        private readonly MyOptions _options;

        public HomeController(IOptions<MyOptions> optionsAccessor)
        {
            _options = optionsAccessor.Value;
        }

        [HttpGet]
        public IActionResult Index()
        {
            var RDS_DB_NAME = _options.RDS_DB_NAME;
            var RDS_HOSTNAME = _options.RDS_HOSTNAME;
            var RDS_PASSWORD = _options.RDS_PASSWORD;
            var RDS_PORT = _options.RDS_PORT;
            var RDS_USERNAME = _options.RDS_USERNAME;
            return Content($"RDS_DB_NAME = {RDS_DB_NAME}, RDS_HOSTNAME = {RDS_HOSTNAME}, RDS_PASSWORD = { RDS_PASSWORD}, RDS_PORT = {RDS_PORT}, RDS_USERNAME = { RDS_USERNAME}");
        }
    }
}

      

+2


source to share


1 answer


I ran into this exact problem and it was much more complicated than I expected.

Step 1 . I modified this answer from another Stack Overflow question. My code in Startup.cs looked like this:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .AddJsonFile(@"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();

    // This adds EB environment variables.
    builder.AddInMemoryCollection(GetAwsDbConfig(builder.Build()));

    Configuration = builder.Build();
}

private Dictionary<string, string> GetAwsDbConfig(IConfiguration configuration)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();

        foreach (IConfigurationSection pair in configuration.GetSection("iis:env").GetChildren())
        {
            string[] keypair = pair.Value.Split(new[] { '=' }, 2);
            dict.Add(keypair[0], keypair[1]);
        }

        return dict;
    }

private string GetRdsConnectionString()
{
    string hostname = Configuration.GetValue<string>("RDS_HOSTNAME");
    string port = Configuration.GetValue<string>("RDS_PORT");
    string dbname = Configuration.GetValue<string>("RDS_DB_NAME");
    string username = Configuration.GetValue<string>("RDS_USERNAME");
    string password = Configuration.GetValue<string>("RDS_PASSWORD");

    return $"Data Source={hostname},{port};Initial Catalog={dbname};User ID={username};Password={password};";
}

      

Step 2 . You will need to navigate to the RDS service in the AWS console. Select the instance you want to connect to β†’ Instance Actions β†’ See Details. You will be able to find RDS_HOSTNAME (endpoint) and RDS_PORT (port).



RDS_DB_NAME is the name of the database that your code should work with.

RDS_USERNAME and RDS_PASSWORD are the main usernames and passwords that you used when creating the database in Elastic Beanstalk. If you go to Elastic Beanstalk -> Configuration -> Data Layer -> (Click on Gear) you will see your Master username and can change your password if you forget it.

Step 3 . Now that you have all the data, you will need to enter these parameters as environmental properties in the elastic beanstalk. Navigate to Elastic Beanstalk β†’ Software Configuration β†’ (click on Gear). At the bottom of this page are Environment Properties. This is where you will want to enter the names in your code from the first step and the values ​​from RDS in the second step.

For more information on this from the AWS documentation, check here and here .

+2


source







All Articles