14

I'm currently using the git push deployment option to deploy a few copies of an azure-function. The function's function.json file has multiple "connection" entries linking to different storage accounts (i.e. for a blob trigger & table output). In different copies of the deployed function I'd like to connect to different storage accounts. Is there any special syntax that can be used in function.json to populate the "connection" string from an environment variable?

I guess an alternative would be to edit function.json as part of a custom kudu step, but environment variables seems more consistent with the other azure app service offerings.

Rufus Nelson
  • 207
  • 2
  • 5

1 Answers1

27

This already works, and is actually the recommended way for you to handle connection strings, since you don't want those checked in with your source code. You can use an app setting name for the connection value, and we'll resolve it. In the following EventHub triggered function, the values MyEventHubReceiver, MyEventHubSender and MyEventHubPath will be auto resolved from app settings:

    "bindings": [
        {
            "type": "eventHubTrigger",
            "name": "input",
            "direction": "in",
            "connection": "MyEventHubReceiver",
            "path": "%MyEventHubPath%"
        },
        {
            "type": "eventHub",
            "name": "output",
            "direction": "out",
            "connection": "MyEventHubSender",
            "path": "%MyEventHubPath%"
        }
    ]
}

In general, most of the binding properties support the %% resolution syntax, allowing you to store the actual values in app settings for both security as well as configurability.

mathewc
  • 13,312
  • 2
  • 45
  • 53
  • 10
    To clarify for anyone else looking at this: "connection" parameters = dont include the "%" symbols, values will be resolved to app settings (define an app setting with a connection string as its value https://azure.microsoft.com/en-gb/documentation/articles/storage-configure-connection-string/#create-a-connection-string-to-an-azure-storage-account). For other parameters = use the "%" symbols to resolve an app setting. – Rufus Nelson Jun 20 '16 at 10:08
  • @RufusNelson, I don't understand your comment – Thomas Jun 22 '16 at 20:32
  • 7
    The syntax for using environment variables in function.json is inconsistent. For 'connection' properties you dont need the '%', but for the rest you do. – Rufus Nelson Jun 23 '16 at 15:47
  • 2
    Yes, this has caused people some confusion in the past. I've created an issue in our repo [here](https://github.com/Azure/azure-webjobs-sdk-script/issues/454) for us to decide if we should make any changes. That issue also describes the rationale for the current behavior:) – mathewc Jun 23 '16 at 17:08
  • Do I have to add MyEventHubPath to both input and output? Can you also add C# code example how it is used? Thanks! – Manish Jain Jul 18 '16 at 16:29
  • I get error Microsoft.Azure.WebJobs.Host: Exception binding parameter 'path'. Microsoft.Azure.WebJobs.Host: No value was provided for parameter 'path'. – Manish Jain Jul 18 '16 at 16:42
  • @mathewc is this still valid? – ansario Jan 19 '17 at 22:16
  • Yes the above answer is still valid - are you having issues? – mathewc Jan 19 '17 at 22:18
  • @mathewc Yes when using Azure CLI Tools for VS. When trying the function locally, using beta 9, I get "a ScriptHost error has occurred." – ansario Jan 19 '17 at 22:28
  • Recommend you create a new SO question for that - sounds like a separate CLI / tools issue. – mathewc Jan 19 '17 at 22:30
  • Here are the [MS Docs pertaining to dynamic application settings](https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#resolving-app-settings) – SliverNinja - MSFT Oct 31 '17 at 19:05