Jaa


Azure API Apps – Configuration with Environment Variables

I’ve been digging into API Apps (and Logic Apps) in Azure App Service recently. If you’ve not looked at API Apps, they build on top of Web Apps (formerly known as Web Sites) but add a few extra bits of goodness, one example is that they are added as API Connectors in your subscription and can be orchestrated using Logic Apps Smile

I’m a big fan of Web Apps, so like the fact that API Apps builds on top of this as a platform. When working with config settings I often put connection strings, keys, etc in environment variables in the environment that they relate to. This not only simplifies the deployment (as there’s no config file transformation to worry about), but means that I don’t have to put secrets into a config file that I’m commiting to source control!

API Apps are Web Apps

I mentioned that API Apps build on Web Apps. All good there. Except that I when I was deploying my app and trying to set the environment variables for my config, I couldn’t find the magic page in the portal.

In Web Apps, I navigate to the blade for the Web App, click Settings and then Appliaction Settings to get to the App Settings:

image

However, if you do the same for an API App you get to a rather different set of options with no App Settings in site!

image

At this point I scratched my head a little, and started to wonder whether the underlying Web App had been abstracted away Sad smile

Fortunately I was set straight by the gastermeister who showed me a small, but important, link on the API App blade:

image

Just in case you missed it (like me), I’ve circled the “API app host” link. This link takes you to the blade for the underlying Web App, and from there you can drill into all the settings for your Web App (e.g. the Application Settings). Happy times!

Aside: Working with environment variables

In ASP.NET 5 working with environment variable config is wonderful as there is built in support for this:

 Configuration = new Configuration()
             .AddJsonFile("config.json")
             .AddEnvironmentVariables();

In Web Apps, custom settings you create (in the portal or the API) end up as environment variables with an “APPSETTING_” prefix. So a setting of “MyConfig” ends up as “APPSETTING_MyConfig”. ASP.NET 5 also makes this easy to deal with as we can specify a prefix for the environment variables!

 Configuration = new Configuration()
               .AddJsonFile("config.json")
               .AddEnvironmentVariables( "APPSETTING_" ); 

When you do this the config system only uses environment variables that have the prefix, and it strips off the prefix. So asking for “MyConfig” will use the value of the “APPSETTING_MyConfig” environment variable if it exists.

When I’m working with pre-ASP.NET 5 then I tend to just use a small helper that wraps the AppSettings.