Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Elastic Beanstalk Environment Properties in .net?

How can I read the Environment Properties from my AWS Elastic Beanstalk Application found here:

Configuration > Software Configuration > Environment Properties

enter image description here

None of the following approaches work:

ConfigurationManager.AppSettings["MyServiceUrl"]
ConfigurationManager.AppSettings["aws:elasticbeanstalk:application:environment.MyServiceUrl"]
Environment.GetEnvironmentVariable("MyServiceUrl")
Environment.GetEnvironmentVariable("aws:elasticbeanstalk:application:environment.MyServiceUrl")

The 'fully qualified' name attempt comes from the AWS EB documentation.

Any ideas?

like image 761
Nick Avatar asked Nov 01 '15 14:11

Nick


People also ask

How do I access Elastic Beanstalk environment variables?

Elastic Beanstalk lets you enter the environment variables for each environment using the management panel. On AWS, open Elastic Beanstalk. Go to your Application > Environment > Configuration > Software Configuration . Under Environment Properties you will find a list of properties you can configure.

How do you view the Elastic Beanstalk environment?

To view eventsOpen the Elastic Beanstalk console , and in the Regions list, select your AWS Region. In the navigation pane, choose Environments, and then choose the name of your environment from the list. If you have many environments, use the search bar to filter the environment list.

Where are environment variables stored Elastic Beanstalk?

Environment properties are written to the /opt/python/current/env file, which is sourced into the virtualenv stack where the application runs. For more information, see Using the Elastic Beanstalk Python platform.

What are the environment types on AWS Elastic Beanstalk?

In AWS Elastic Beanstalk, you can create a load-balanced, scalable environment or a single-instance environment. The type of environment that you require depends on the application that you deploy.


2 Answers

In your .ebextensions/myoptions.config file:

option_settings:
  - option_name: MyServiceUrl
    value: change me

This will add the "MyServiceUrl" option in your EB Environment Properties section (as you're seeing already). When deployed, this will add the following to your Web.Config file:

<appSettings>
  <add key="MyServiceUrl" value="change me" />
</appSettings>

If you RDP into your EC2 instance, you'll see this.

When you change the property using the EB console, the setting will be modified in your Web.Config file.

So you access this property using the standard AppSettings method:

string value = ConfigurationManager.AppSettings["MyServiceUrl"];

The Catch:

You need to ensure that your Web.Config file does not contain this setting, otherwise EB does not replace it. If your Visual Studio deployment package includes this setting, then EB will not replace it and you will always receive the deployed value when you access the property via your code.

The Solution:

In you Web.Release.config file, have the setting removed during Visual Studio deployment:

<appSettings>
  <add key="MyServiceUrl" xdt:Transform="Remove" xdt:Locator="Match(key)" />
</appSettings>

This will remove the setting from Web.Config during Visual Studio deployment and will allow EB to add the value into the file during EB deployment.

like image 126
Matt Houser Avatar answered Sep 26 '22 20:09

Matt Houser


It looks like this behavior has changed in Elastic Beanstalk. The docs now say

Settings applied in the AWS Management Console override the same settings in configuration files, if they exist. This lets you have default settings in configuration files, and override them with environment specific settings in the console.

So you can now use the same config names in your web.config and in the Elastic Beanstalk config, and the Elastic Beanstalk values will override any in your web.config. It looks like EB simply adds new entries to the web.config file, so there will be two entries for any values defined in both places. Since the EB-added entries are later in the file they take precedence.

like image 21
Kevin Tighe Avatar answered Sep 25 '22 20:09

Kevin Tighe