Wednesday, 29 May 2024

Creating an Azure App Configuration

 

Creating an Azure App Configuration

 

After logging in to your Azure account, go to the Marketplace and select “App Configuration,” then “create.” Next, you must choose where and in which resource group the app configuration should be created.

You can also pick the Pricing tier (Free or Standard) and the replication and recovery options in case you delete something.

Creating an Azure App Configuration

 

The next step is to choose the component’s network and access security settings.

You can make your component available to the public or just to people in your private network.

Create your app configuration

 

Keys and Values

 

You can store simple key-value pair objects less than 10,000 characters in size in Azure App Configuration. It can be used to define objects such as shared URLs, connection strings, and even authentication passwords or secrets.

To simplify things for users, Azure App Configuration lets you add a content type to the MIME Media type list in addition to the predefined types, such as JSON, Key Vault reference, and feature flags.

 

Keys

 

Keys are Unicode strings delimited by a character like “/” or “:”. They are used to store and organize pairs in hierarchical namespaces.

You can use any Unicode character to name a key, except for “*,” “\,” and “,” which are reserved. If you need to use a reserved character, you must escape it with “\{ reserved character }.”

Here are some examples of how to set up a hierarchy with your key names:

AppName:ServiceName:ApiEndpoint
AppName:Region:DbEndpoint

 

Values

 

Values store information about pairs. They can be as simple as a single value or as complex as a JSON object.

Values are not designed to store application secrets. Instead, Azure App Configuration allows the use of Azure Key Vault references. When creating a new pair, you can choose between a key-value or a key-value reference.

 

Labels

 

Labels filter and differentiate values with the same key. This attribute is optional and can be empty or “null.”

The label system lets you create multiple versions of the same key, such as one for each zone, environment, service type, or even functional object.

 

Querying Key Values

 

Each key value is uniquely identified by its key and a label, which can be “null.” You must specify a pattern to query key values from an App Configuration store. The App Configuration store returns all the keys that match the pattern with their corresponding values and attributes.

 

Creating a Key-Value Pair

 

In the Azure App Configuration interface, go to the Configuration explorer tab and click Create. You can create either a key-value or a key-value reference. An editor will open so you can fill in the new key information.

If you create a key-value reference, you must select a Key Vault and the name and version of the key you want to link to it.

Créer une paire clé-valeurs

 

Managing Features with Features Management

 

Azure App Configuration provides features management to enable and disable new features in production without deploying all your application bricks.

This allows you to quickly change the visibility of a feature by enabling it for a clearly defined group.

There are several ways to use features management:

  • Feature flag: This variable with a binary state (On or Off) allows you to specify whether to execute a block of code or display the UI components for the user.
  • Feature filterA feature flag with additional rules that can show or hide the feature based on criteria such as a specific user group, a time window, or a percentage of users.

To create a feature flag, navigate to the Feature manager tab in the Azure App Configuration interface and click Create. An editor opens so you can enter the new key information.

Gérer les fonctionnalités avec les features management

 

Connecting to Azure App Configuration

 

You need to configure App Configuration access before you can use its values. To do this, you need to give the identity of the component that will access the App Configuration the role of App Configuration Data Reader at a minimum.

 

Managing Access to the App Configuration

 

Go to the Access control (IAM) tab in the Azure App Configuration interface, click Add role assignment, and then choose the component identity with the App Configuration Data Reader or App Configuration Data Owner role.

 

Managing the App Configuration Identity

 

If the App Configuration contains references to the Azure Key Vault, the App Configuration must be authorized to access the Key Vault using the component’s managed identity or a user-assigned identity.

Go to the Identity tab in the Azure App Configuration interface and click the Status button to enable the component’s managed identity.

Go to User assigned and add your identity to add the user-assigned identity.

Naviguer vers User assigned puis ajouter votre identité

 

Now that the App Configuration has an identity, you need to go to the Key Vault interface and create an Access Policy for the App Configuration identity.

 

Using Azure App Configuration

 

Now that the accesses are configured in the App Configuration, we need to get the connection string and URL from the Access keys tab, as shown in the image below:

Naviguer vers User assigned puis ajouter votre identité

 

Example with a .NET Core Application

 

First, you need to add the connection string to the application environment settings.

Next, add the package to the project using the NuGet Package Manager Microsoft.Extensions.Configuration.AzureAppConfiguration, ou avec la ligne de commande suivante :

dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration

 

Import the following Namespaces into Startup:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;

 

Then, you’ll need to configure value retrieval from the App Configuration:

builder.AddAzureAppConfiguration(options =>
options
.Connect(connectionstring)
.ConfigureKeyVault(kv =>
{
kv.SetCredential(new DefaultAzureCredential());
})
// Load configuration values with specific label
.Select(KeyFilter.Any, labelFilter: "myservice")
.Select(KeyFilter.Any, labelFilter: "common")
.ConfigureRefresh(
refreshOptions =>
refreshOptions.Register("demo", refreshAll: true)
.SetCacheExpiration(TimeSpan.FromSeconds(40))
)
.UseFeatureFlags()
);
view rawgistfile1.txt hosted with ❤ by GitHub

 

The Select clause lets you add filters to retrieve a subset of keys with a specific label. In the example, we only want to retrieve myservice and common keys.

The ConfigureRefresh clause lets you define criteria for updating keys and feature flags. In the example, the whole configuration is reloaded if the “demo” key changes. Otherwise, the keys are retrieved when the cache defined using the SetCacheExpiration method expires.

Finally, the UseFeatureFlags method lets you retrieve both feature flags and keys.

 

To use the App Configuration values, simply add the IConfiguration interface to your classes and controllers, then retrieve the value from the configuration table:

String variable = configuration["demo"];

 

Azure App Configuration: Key Takeaways

 

Configuration management is vital to how applications work, especially in microservices architectures. With Azure App Configuration, you can centralize your configuration and reduce the number of updates and key rotations.

It also makes it easier to manage access to values and reference passwords, strings, and certificates stored in the Key Vault, which is more secure.

 


 

No comments:

Post a Comment