Azure Functions App Settings

Understanding App Settings in Azure Functions

Azure Functions allow you to configure your function apps using application settings. These settings are crucial for managing configuration values, connection strings, and other parameters that your functions need to operate correctly. They provide a secure and flexible way to customize your function app's behavior without modifying your code.

Key Concepts

Accessing App Settings

You can access app settings in your function code using platform-specific methods. Here are examples for common languages:

C#


// Using IConfiguration in .NET Core/5+
var connectionString = _configuration["MyConnectionString"];

// Using ConfigurationManager (older .NET Framework)
// var settingValue = System.Configuration.ConfigurationManager.AppSettings["MySetting"];
            

JavaScript/TypeScript (Node.js)


// Accessing process.env in Node.js
const apiKey = process.env.MyApiKey;
            

Python


import os

# Accessing environment variables
connection_string = os.environ.get('CosmosDbConnectionString')
            

PowerShell


# Accessing environment variables
$storageAccountName = $env:StorageAccountName
            

Managing App Settings

You can manage app settings through the Azure portal, Azure CLI, or ARM templates.

Azure Portal

  1. Navigate to your Function App in the Azure portal.
  2. Under "Settings", select "Configuration".
  3. Go to the "Application settings" tab.
  4. Click "New application setting" to add a new key-value pair.
  5. Click "Save" to apply your changes.

Azure CLI

To add or update an app setting using Azure CLI:


az functionapp config appsettings set --name <YourFunctionAppName> --resource-group <YourResourceGroupName> --settings MySetting=MyValue OtherSetting=AnotherValue
            

To delete an app setting:


az functionapp config appsettings delete --name <YourFunctionAppName> --resource-group <YourResourceGroupName> --setting-names MySetting
            

Important Note:

Changes to application settings often require your function app to restart. While Azure handles this automatically for many scenarios, be aware that a brief interruption might occur. For critical applications, consider deployment strategies that minimize downtime.

Common Use Cases

Best Practices

Example: Storing a Database Connection String

Azure Portal:

Under "Configuration" -> "Application settings", add a new setting:

  • Name: DatabaseConnectionString
  • Value: Server=tcp:yourserver.database.windows.net,1433;Database=yourdatabase;User ID=youruser;Password=yourpassword;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Function Code (C#):

public static class MyFunction {
    [FunctionName("GetData")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,
        ILogger log)
    {
        string connectionString = Environment.GetEnvironmentVariable("DatabaseConnectionString");

        // Use the connectionString to connect to your database
        // ...

        return new OkObjectResult("Data fetched successfully!");
    }
}