Get Azure key-value pairs from App configuration | Serverless360
← Return To Home

How to get Key-values from Azure App Configuration within Logic Apps

Tags:  
Azure Logic Apps

Last week, I worked on a POC – how can we dynamically set SQL Server Connector configurations within Logic Apps? – that became a real project and a real architect solution. And one of the critical pieces was Azure App Configuration. Azure App Configuration stores configuration data as key-values. Key-values are a flexible and straightforward representation of developers’ application settings to make these configurations settings dynamic without redeploying your application if changes are required. 

Of course, I could use Key Vault to accomplish that same goal, but for me, you should use Key Vault to store the sensitive data as secrets like username and passwords, connection strings, etc. And App Configuration to store non-sensitive data that you want to make dynamic in your application. Combining Azure App Configuration to store and manage your configurations and Azure Key Vault to store your secrets, we will obtain an extremely powerful configuration management nearly for free.

App Configuration complements Azure Key Vault, which is used to store application secrets. App Configuration makes it easier to implement the following scenarios:

  • Centralize management and distribution of hierarchical configuration data for different environments and geographies
  • Dynamically change application settings without the need to redeploy or restart an application
  • Control feature availability in real-time

The only problem was that unlike Key Vault, which has an available connector to be used inside Logic Apps, App Configuration doesn’t have a connector available.

So I knew that my first and favorite option would be to create a Function App to close this gap. Luckily, I found a great blog post from Shankar Chandrasekar doing similar stuff, but there were small issues with that sample:

  • The author was using Function App V1 at that time, and I am using the latest version, so that sample didn’t work anymore (not compatible).
  • And there wasn’t, in my opinion, an elegant way to control failures if configuration and Key-values didn’t exist.

For those reasons, but follow the same approach, I decided to create a new Function App function with the following requirements:

  • This is a read operation, and according to RESTful Services best practices, we should only be using the HTTP verb Get
public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
  • The key that we want to read should be passed by query parameter
string appKey = req.Query["appKey"];
  • And finally, the function should raise proper HTTP response status codes according to the situation:
    • 200 Ok if successful returns a value for that key
    • 500 Internal Server Error if something fails, for example, there is no connection string to the App Configuration defined
    • and 404 Not Found if the key is not found

This way, I can control the failures inside Logic Apps.

And the end result was this:

    public static class GetAzureAppConfigurationValue
    {
        [FunctionName("GetAzureAppConfigurationValue")]
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            //log.LogInformation("C# HTTP trigger function processed a request.");
            //Read appkey Query parameter from the GET operation
            string appKey = req.Query["appKey"];

            if(string.IsNullOrEmpty(appKey))
                return new BadRequestObjectResult("Mandatory parameter 'appKey' not found or empty.");

            try
            {
                string connectionString = Environment.GetEnvironmentVariable("AppConfigConnectionString");

                var builder = new ConfigurationBuilder();
                builder.AddAzureAppConfiguration(connectionString);
                var build = builder.Build();

                string keyValue = build[appKey.ToString()];

                if (string.IsNullOrEmpty(keyValue))
                {
                    var result = new ObjectResult("Azure Configuration Key not found - " + appKey);
                    result.StatusCode = StatusCodes.Status404NotFound;
                    return result;
                }
                else return new OkObjectResult(keyValue);
            }
            catch(Exception ex)
            {
                var result = new ObjectResult(ex.Message);
                result.StatusCode = StatusCodes.Status500InternalServerError;
                return result;
            }
        }
    }

Once you create your Function App and all you key-values inside App Configuration, you have to:

  • Go to your Function App Configuration option under Settings 
  • And create a new application settings call AppConfigConnectionString containing the connection string to your App Configuration resource.
Azure key value
Once you have that done, you can go ahead and call it inside your Logic Apps
Azure key value

You can find the sample code of this function here: Azure Functions: Get Azure App Configuration Value

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.