Amal Sebastian
Amal Sebastian

Reputation: 193

aws Environment Variables to replace connection string in ASP.net MVC

I was trying to deploy an application in AWS elastic beanstalk with connection string inside web.config file everything works fine. But when I tried to implement by setting environment variables in AWS application it did not work. What I did was I added AWS tags key value pair in aws like RDS_DATABASENAME - admin then i added those in web.config like

    <add key="RDS_DB_NAME"  value="RDS_DB_NAME"/>
    <add key="RDS_USERNAME" value="RDS_USERNAME"/>
    <add key="RDS_PASSWORD" value="RDS_PASSWORD"/>
    <add key="RDS_HOSTNAME" value="RDS_HOSTNAME"/>
    <add key="RDS_PORT"     value="*RDS_PORT" /> 

Then while building connectionString I used this:

        var appConfig = ConfigurationManager.AppSettings; // trying  to get connection details from enviornment varibales 

        string dbname = appConfig["RDS_DB_NAME"];

        if (string.IsNullOrEmpty(dbname)) return null;

        string username = appConfig["RDS_USERNAME"];
        string password = appConfig["RDS_PASSWORD"];
        string hostname = appConfig["RDS_HOSTNAME"];
        string port = appConfig["RDS_PORT"];

        SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder()
        {
            DataSource = hostname + "," + port, 
            InitialCatalog = dbname, 
            UserID = username,          
            Password = password   
        };

        return sqlString.ToString();

I followed the aws doc itself somehow I missed something!

Upvotes: 0

Views: 1151

Answers (1)

John C
John C

Reputation: 8415

The idea with Elastic Beanstalk is that any Environment Properties configured for the Environment will be automatically passed to your application. In a .NET application, this means that they are appended automatically to the end of the <appSettings> section of the Web.config.

So you shouldn't need to make any changes to your Web.config (I would leave those variables out entirely), and your implementation of building the connectionString appears fine.

To troubleshoot, try launching the application with the Environment Properties set. Then log into the instance and verify that the variables have been added to the Web.config correctly. If they are you might need to do some more testing around making sure your application is reading them in correctly.

Upvotes: 1

Related Questions