When you’re working with building applications or services there’s always a need to store configuration. For Azure there’s a great service called Azure App Configuration that allows you to securely store, manage and retrieve configuration settings. It’s a perfect service for both smaller and larger projects and it keeps your configuration in control, and of course secured and audited.
When I’m building solutions using node I typically start with storing my configuration in a local .env
file and then use the
dotenv package to import those settings into process.env
properties. That makes it super
easy to work with configurations, settings and change them as needed without fiddling through all the code and replace. Since I always
add the .env
file to my .gitignore
I also reduce the risk of sharing secrets and passwords. When moving this from my local dev machine
into Azure I have historically just used the Web App application settings. That works really great and is a simple solution to have these
settings being read runtime, the web app only needs to be restarted to pick the new changes up.
As of recently I’ve moved most of my configuration over to Azure App Configuration as that allows me to have more control of the actual settings as well as of the management of them such as history and auditing. This service also works perfect together with Key Vault for your secrets. If you have not started using this service I highly recommend it to you!
To make my way of working with these app configurations in node a little bit easier and to allow me to still use the same way of
working with settings - using a local .env
file when starting the project and running it locally - I created a helper package for Azure
App Configuration that I call azure-env-app-configuration. This package is very
lightweight and allows you to combine the powers of the web app application settings, the .env
file and Azure App Configuration.
The package essentially contains one method that you run in order to read configuration settings from Azure App Configuration into your
node process.env
variables and you keep on using them as previously. As an example, this is my new standard setup for configurations:
import { envAppConfiguration } from "azure-env-app-configuration";
require("dotenv").config();
console.log(process.env.MY_SETTING); // value from .env
await envAppConfiguration({
appConfigConnectionString: "...",
labelFilter: "env"
});
console.log(process.env.MY_SETTING); // value from App Config
In this sample we will first import any settings from the local .env
file and then it will add or override those settings with
configurations, marked with the label env
, from Azure App Configuration. This allows for a great flexibility in your solutions,
specifically when working with multiple environments. For instance we could use a more dynamic filter for the labels - using a
setting in the .env
file or in the web app settings, like this:
import { envAppConfiguration } from "azure-env-app-configuration";
require("dotenv").config();
if(process.env.AZURE_APP_CONFIG) {
await envAppConfiguration({
appConfigConnectionString: process.env.AZURE_APP_CONFIG,
labelFilter: process.env.HOST_ENVIRONMENT
});
}
If you like this approach, feel free to download the npm package and/or contribute to the solution which is available on Github.