Now a days, it is very common to use serverless APIs using Azure Functions. In this article, we are going to discuss about how to use App Configuration Service with Azure Functions. Azure Functions support running in-process or isolated-process. We are going to use the in-process function app in this article.
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 Function
We can create an Azure Functions project using Visual Studio. Let’s say the name of the project is FunctionApp. 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.
NuGet Package References
Right click on the project and select Manage NuGet Packages. Click on the Browse tab and then search for the below mentioned packages.
- Microsoft.Extensions.Configuration.AzureAppConfiguration version 4.1.0 or later
- Microsoft.Azure.Functions.Extensions version 1.1.0 or later
Modify Function App Startup
Now, let’s add a new file, Startup.cs, with the code given in the snapshot given below. It defines a class named Startup
that implements the FunctionsStartup
abstract class. An assembly attribute is used to specify the type name used during Azure Functions startup.
The ConfigureAppConfiguration
method is overridden and Azure App Configuration provider is added as an extra configuration source by calling AddAzureAppConfiguration()
.
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:
Run and Verify
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}"
After setting this variable, we can run the application and it should run successfully. We can navigate to swagger UI page and from there we can call Function App endpoints to ensure that the values from App Configurations are being read appropriately.
I hope you find this information helpful. Let me know your thoughts.