Search code examples
azureazure-web-app-serviceappsettingsazure-deployment

Environment variables for app settings in Azure App Service


I have a Web API in .NET 6 that uses the standard IConfiguration to get config/settings values from appsettings.json and is deployed to Azure App Service (Windows). I'm deploying it with Visual Studio and I can see the code being updated in App Service. However, my changes in the appsettings.json were not reflected in the App Settings tab of the Environment variables page (in the Settings section). Note that the appsettings.json inside App Service was updated (I verified this with Kudu). I don't know how the previous developer created those environment variables (one for each value in appsettings.json).

  • Is that something that I have to manually add/update?
  • Is there a tool to automate the mirroring of the new entries in appsettings.json and create the corresponding environment variables?
  • I tried publishing the appsettings.json by right-clicking on it in Visual Studio Solution Explorer and then clicking on the Publishing appsettings.json menu item, but that didn't seem to work. Am I using that menu item incorrectly?
  • I see that there is a "Pull reference values" button in the Environment variables page, from the few Google results, it seems like that would be to pull updated values from Key Vault. Is this correct?
  • This is the first time that I see environment variables being used in lieu of settings that are already defined in appsettings.json. I know that for some settings (e.g., sensitive values like tokens/API keys), it might be a way to avoid having those values on disk, but in this project, every single setting is being duplicated. Is this a common/standard thing to do? I usually just copy/deploy all the appsettings.<env name>.json for each environment and just set the ASPNETCORE_ENVIRONMENT variable to match the <env name>.

Answers to any/all questions above will be appreciated, I've read several documentation pages of Azure App Service and none mention of a tool/way to automate creating as may env vars as I have in this project. I want to think that the previous developer was not crazy enough to manually create/keep updated each one manually.


Solution

  • The configuration which is set in the appsettings.json will not be added/seen in the Environment variables section of App Service.

    • You can only see the appsettings.json file value in KUDU => site/wwwroot folder.

    I don't know how the previous developer created those environment variables

    • One way is to add manually in the Application and Connection Settings of Environment Variables.

    enter image description here

    • You need to add those only if you want to override the values of the existing appsettings.json file

    appsettings.json in Local:

      "SampleConfig": "Value from Development appsettings.json"
    
    • If you want to continue with the existing settings itself, you can directly retrieve the values from IConfiguration.
     private readonly ILogger<WeatherForecastController> _logger;
     private readonly IConfiguration myconfig;
     public WeatherForecastController(ILogger<WeatherForecastController> logger,IConfiguration config)
     {
         _logger = logger;
         myconfig = config;
     }
    var mysetting= myconfig["SampleConfig"];
    

    Output of my app with values from appsettings.json:

    enter image description here

    Is there a tool to automate the mirroring of the new entries in appsettings.json and create the corresponding environment variables?

    • If you have more settings to be added or don't want to add it manually, you can set them by using Azure CLI Command.

    I have followed this MSDoc for CLI Commands.

    • Open Command Prompt locally, navigate to the App root folder and login to Azure CLI.
    az login
    
    • You can add the settings directly as below.
    az webapp config appsettings set --name AppSettings6June --resource-group YourRGName --settings "SampleConfig=Value from Deployed Azure App Service" "Name=Harshitha" "Title=Azure App Service"
    

    enter image description here

    OR

    • Can set the values in a json file(create new file or edit the existing appsettings.json) and run the below command.
    az webapp config appsettings set --name AppSettings6June --resource-group YourRGName --settings "@appsettings.json"
    

    My appsettings.json:

    {
      "SampleConfig": "Value from Deployed Azure App Service",
      "Name": "Harshitha",
      "Title": "Azure App Service"
    }
    

    enter image description here

    • If you run the same command multiple times, existing keys will be updated and new keys will be added.

    Azure Environment Variables :

    enter image description here

    App Service Output :

    enter image description here

    I see that there is a "Pull reference values" button in the Environment variables page, from the few Google results, it seems like that would be to pull updated values from Key Vault. Is this correct?

    • That option is used when you set the KeyVault values in the App Service Settings as Key Vault reference.

    Refer this MSDoc and my SOThread for more details.