App Configuration Service with Isolated Function
App Configuration Service with Isolated Function

Azure App Configuration Service with Isolated Azure Functions

In last article, we have seen how to use Azure App Configuration Service with in-process Azure functions. We created the function app project locally and connected it with Azure App Configuration service hosted in Azure.

In this article, we are going to create an Isolated Azure Functions project. Then we will try to consume the application settings from Azure App Configuration Service.

Prerequisites

For following all steps in this article, we will need Azure Subscription. If you don’t have an Azure subscription, create a free account before you begin.

Create Azure App Configuration Service

We can use the bicep file to create the Azure App Configuration Service. All the required files can be found in my previous post. Also, we can run the CLI commands to see the readonly connection string.

Refer my GitHub repository to create the resource and insert the keys in the Azure App Configuration service. The below mentioned keys will be inserted by the script:

Key = TestApi:GetEndpoint:Message, Value = Hello

Key = TestApi:PostEndpoint:Message, Value = Good Morning

Key = TestApi:KeyWithLabel, Value = Value For Key With Label

Create Azure Functions Project

We can create an Azure Functions project using Visual Studio. Let’s say the name of the project is IsolatedFunctionApp. The snapshot given below shows the settings that I have selected while creating the project. You can refer this article for detailed steps to create Azure Functions project using Visual Studio.

Creating Isolated Azure Function using Visual Studio

Make sure Authorization level is set to Anonymous. Otherwise we may have to send the function keys to allow making requests to the Azure Function.

Manage NuGet Packages

Once the project is created, right click on the project and select Manage NuGet Packages option. Then a new window opens. Select Browse tab and search for Microsoft.Azure.AppConfiguration.Functions.Worker package. Install the latest stable version of this package.

Startup Configurations

Once the packages are installed, the ConfigureAppConfiguration should be added. The Action delegate here would read the connections string from environment variable and then it will use that connection string to setup connection with Azure App Configuration service instance.

The code from Program.cs file is shown in the code snippet given below.

Modify Functions

Now, let’s go to the file which contain functions. Delete all the default code. We need to add three different things:

  • Constructor, where ILogger and IConfiguration should be injected in the constructor
  • Get Endpoint, which expects a name in query string. This endpoint will use one key-value pair to get greeting prefix.
  • Post Endpoint, which expects a name in query string. This endpoint will use the other key-value pair to get greeting prefix.

The code for this file is shown in the code snippet given below:

Set Environment Variable

Before running the project, we need to set an environment variable, ConnectionString. For that, let’s run any of the commands given below:

## Connection String Environment Variable via Command Prompt
setx ConnectionString "{app-config-store-connection-string}"

## Set Connection String Env. Variable via PowerShell
$Env:ConnectionString = "{app-config-store-connection-string}"

Run and Verify

After setting this variable, we can run the application and it should run successfully. Then we can call the endpoints via PowerShell script to ensure that the values from App Configurations are being read appropriately.

Wrapping Up

We have seen how to connect with Azure App Configuration Service from In-Process Azure Functions in previous article. In this article, we have seen that the isolated Azure Functions need different NuGet package, but otherwise the code needed to access the Azure App Configuration Service is almost the same.

I hope you find this information helpful. Let me know your thoughts.

Leave a Reply Cancel reply