terraformazure-functionsgitlab-citerraform-provider-azureblue-green-deployment

App-Settings of Function App are getting swapped in between function and slot


I am trying to deploy a Azure Windows Function App using Terraform AzureRM 3.66, over GitLab-CI pipeline. I am not able to add the exact code, but I can share almost the same sample. When I deploy the app-setttings of main function are getting deployed to Slot and app-settings of slot is getting deployed on to main. Let me know if I am missing anything. Any correct reference will be very helpful on this.

resource "azurerm_windows_function_app" "example" {
  name                 = "example-windows-function-app"
  resource_group_name  = azurerm_resource_group.example.name
  location             = azurerm_resource_group.example.location
  storage_account_name = azurerm_storage_account.example.name
  service_plan_id      = azurerm_service_plan.example.id

app_settings = {
    "FUNCTIONS_WORKER_RUNTIME" = "dotnet"
    "SOME_APP_SETTING_KEY"    = "main-func-value"
    "ANOTHER_APP_SETTING_KEY" = "main-func-value"
  }

}

resource "azurerm_windows_function_app_slot" "example" {
  name                 = "example-slot"
  function_app_id      = azurerm_windows_function_app.example.id
  storage_account_name = azurerm_storage_account.example.name

app_settings = {
    "FUNCTIONS_WORKER_RUNTIME" = "dotnet"
    "SOME_APP_SETTING_KEY"    = "staging_value"
    "ANOTHER_APP_SETTING_KEY" = "staging_another_value"
  }
}

After this to swap slot below running on a separate slot-swap-job: az functionapp deployment slot swap -g MyResourceGroup -n MyUniqueApp --slot staging --target-slot production

But this is setting the main app settings on to slot and slot-app-settings on to main.


Solution

  • You need to add sticky_settings to azurerm_windows_function_app

    resource "azurerm_windows_function_app" "example" {
      name                 = "example-windows-function-app"
      resource_group_name  = azurerm_resource_group.example.name
      location             = azurerm_resource_group.example.location
      storage_account_name = azurerm_storage_account.example.name
      service_plan_id      = azurerm_service_plan.example.id
    
    app_settings = {
        "FUNCTIONS_WORKER_RUNTIME" = "dotnet"
        "SOME_APP_SETTING_KEY"    = "main-func-value"
        "ANOTHER_APP_SETTING_KEY" = "main-func-value"
      }
    sticky_settings = {
        app_setting_names = [ "SOME_APP_SETTING_KEY", "ANOTHER_APP_SETTING_KEY" ]
      }
    }
    

    It will mark these settings as slot settings and do not swap them during deployment.

    You need this only in azurerm_windows_function_app resource.