Configuring Application Settings and Connection Strings
Last updated: July 20, 2023
Azure App Service allows you to configure various application settings and connection strings that are crucial for your application's runtime behavior. These settings are injected as environment variables into your application's process, making them accessible without hardcoding sensitive information directly into your code.
Application Settings
Application settings are key-value pairs that control aspects of your application's configuration. They are often used for:
- Feature flags
- Custom configurations specific to your application logic
- Environment-specific parameters (e.g., API endpoints, logging levels)
You can manage application settings through the Azure portal, Azure CLI, or PowerShell.
Using the Azure Portal
- Navigate to your App Service instance in the Azure portal.
- In the left-hand menu, under Settings, select Configuration.
- Go to the Application settings tab.
- Click New application setting.
- Enter the Name (the environment variable name) and Value for your setting.
- Click OK and then Save at the top of the blade.
# Example: Accessing an application setting in C#
string apiKey = Environment.GetEnvironmentVariable("MY_API_KEY");
# Example: Accessing an application setting in Node.js
const logLevel = process.env.LOG_LEVEL || 'info';
Connection Strings
Connection strings are used to establish connections to external data sources, such as Azure SQL Database, Cosmos DB, or other databases. App Service provides a dedicated section for managing these.
When you define a connection string, App Service automatically creates two environment variables:
- A variable with the name you provide (e.g.,
SQLAZURECONNSTR_MyDatabase
). - A variable with the name prefixed by
CUSTOMCONNSTR_
followed by your name (e.g.,CUSTOMCONNSTR_MyDatabase
).
It's recommended to use the named variable (e.g., SQLAZURECONNSTR_MyDatabase
) as it follows a standard convention.
Using the Azure Portal for Connection Strings
- Navigate to your App Service instance in the Azure portal.
- In the left-hand menu, under Settings, select Configuration.
- Go to the Connection strings tab.
- Click New connection string.
- Select the Name (e.g.,
MyDatabase
) and the Type of database. - Provide the connection string details. The portal often pre-fills these for you if you select an Azure service.
- Click OK and then Save.
# Example: Accessing a connection string in C#
string connectionString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_MyDatabase");
// Use connectionString to configure your DbContext or database client
Important Note on Secrets Management
For enhanced security, consider using Azure Key Vault to store and manage your secrets, including connection strings and API keys. You can then reference Key Vault secrets directly within your App Service's application settings.
Deployment Slots
Configuration settings (both application settings and connection strings) can be configured independently for different deployment slots (e.g., staging, production). This allows you to test settings in a staging environment before swapping to production.
When you swap slots, you have the option to "swap application configuration settings". If you choose this, settings marked with a "Deployment slot setting" toggle set to "On" in the portal will be swapped along with the code. If it's "Off", the setting will remain with the slot it was originally configured for.
Best Practices
- Avoid hardcoding secrets: Always use application settings or connection strings for sensitive information.
- Use Azure Key Vault: For production environments, centralize your secrets in Azure Key Vault.
- Environment-specific settings: Utilize different settings for development, staging, and production environments.
- Naming conventions: Use clear and consistent names for your settings.
- Document your settings: Maintain documentation that explains the purpose of each setting.