1
votes

I have creating some Azure Functions in a C# project that are working fine locally. An example of the definition of a function is the following:

[FunctionName("createBankTransactionFromServiceBus")]
public async Task Run(
       [ServiceBusTrigger("vspan.sbus.xerobanktransaction.requests", "requests", 
       Connection = "AccountingServiceBusConnection")] string myQueueItem)
{
}

Nothing different than usual. The problem is when I deploy this function on Azure. On Azure, the Azure Functions can't find the connection string. So, I added a new one in the local.settings.json but now I have two AccountingServiceBusConnection with the same value, one for my local machine and one for Azure.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AccountingServiceBusConnection": "connectionString"
  },
  "AccountingServiceBusConnection": "connectionString"
}

I tried to replace the connection in the signature of the function like:

[FunctionName("createBankTransactionFromServiceBus")]
public async Task Run(
       [ServiceBusTrigger("vspan.sbus.xerobanktransaction.requests", "requests", 
       Connection = "%Values:AccountingServiceBusConnection%")] string myQueueItem)
{
}

but locally I have a warning (with or without %).

Warning: Cannot find value named 'Values:AccountingServiceBusConnection' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger' in 'C:\Projects\fun\bin\Debug\netcoreapp3.1\createBankTransactionFromServiceBus\function.json'. You can run 'func azure functionapp fetch-app-settings ' or specify a connection string in local.settings.json.

enter image description here

Also, I tried to move AccountingServiceBusConnection under ConnectionStrings with the same result.

Update

Screenshot of Kudu and local.settings.json

enter image description here

Screenshot of Azure Functions configuration

enter image description here

How can you configure a pipeline in DevOps? How do you store the configuration from DevOps in the configuration in your Azure Functions?

2

2 Answers

0
votes

There's no local.settings.json on Azure, you must add the settings to your Azure App Services settings:

enter image description here

https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings

EDIT:

for Key Vault Integration you must assign a managed identity to your function:

enter image description here

Then use Key Vault Ingegration:

@Microsoft.KeyVault(SecretUri={theSecretUri})

enter image description here

More info:

https://medium.com/statuscode/getting-key-vault-secrets-in-azure-functions-37620fd20a0b

0
votes

By default, the local.settings.json file is NOT deployed with your code to an Azure Function. See the documentation here:

By default, these settings are not migrated automatically when the project is published to Azure. Use the --publish-local-settings switch when you publish to make sure these settings are added to the function app in Azure. Note that values in ConnectionStrings are never published.

You have a few options:

  1. Explicitly publish your local.settings.json file with the aforementioned command line arg.
  2. Add this setting (and any other settings needed) to your Azure Function's Configuration. Those values defined in your app settings in the Azure Portal take precedence over everything else.

I don't recommend option #1, because it requires you to place production values in your source code, which is in general a bad idea.

Updated - how to configure with Azure DevOps For Azure DevOps, we've taken a two pronged approach.

  1. We place the bare minimum key/value pairs in the Azure Function configuration. These are added into our yaml deployment pipeline. Some variable values (like connection strings) are read from other resources at deploy time so that sensitive info isn't included in our yaml script that is checked into revision control. Here's some example yaml for deploying an Azure Function:

    {
       "apiVersion": "2016-03-01",
       "type": "Microsoft.Web/sites",
       "name": "FooBarFunction",
       "location": "[resourceGroup().location]",
       "kind": "functionapp",
       "dependsOn": [
         "[resourceId('Microsoft.Web/serverfarms', "YourHostingPlanName")]"
       ],
       "properties": {
         "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', "YourHostingPlanName)]",
         "siteConfig": {
           "appSettings": [
             {
               "name": "WEBSITE_CONTENTSHARE",
               "value": "FooBarFunctionContentShare"
             },
             {
               "name": "FUNCTIONS_WORKER_RUNTIME",
               "value": "dotnet"
             }
           ]
         }
       }
     }
    
  2. We use Azure App Configuration service to hold all of our other app settings. This gives us the advantage of defining different config profiles, and also having hot reload of our app settings without having to recycle the Azure Function. It also plays nicely with Keyvault for sensitive settings.