Azure Function Connection Property

Karl Gardner 165 Reputation points
2024-08-25T17:19:59.5666667+00:00

Hello,

I keep reading in the documentation for Azure Functions that you can set a connection property to define a different connection (defined in the application settings). The Connections documentation states:

"Application settings in Azure are stored encrypted and can be accessed at runtime by your app as environment variable name value pairs. For triggers and bindings that require a connection property, you set the application setting name instead of the actual connection string. You can't configure a binding directly with a connection string or key.

For example, consider a trigger definition that has a connection property. Instead of the connection string, you set connection to the name of an environment variable that contains the connection string. Using this secrets access strategy both makes your apps more secure and makes it easier for you to change connections across environments. For even more security, you can use identity-based connections."

I'm wondering where would this connection property be? Let's take the simple blob triggers for example. Seeing the documentation for blob triggers and the sdk reference for BlobAttribute and BlobTriggerAttribute I don't see a way to set this connection through the constructor. However, the BlobAttribute and BlobTriggerAttribute do have a property of Connection but their would be no way of setting this with a C# Attribute. Therefore, I am wondering where would connection property be? Would it be in the host.json or function.json file?

Thanks,

Karl Gardner

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,869 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,639 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 21,595 Reputation points MVP
    2024-08-25T20:12:49.2033333+00:00

    In Azure Functions, the connection property for triggers and bindings (like Blob Trigger) is set in the function's configuration, typically inside the function.json file. This file defines the bindings and triggers for your function and includes the necessary properties for these connections, including the connection string, which is specified by referencing an environment variable.

    Here's how it works for a Blob Trigger:

    1. In function.json: The connection property would be set in the function.json file under the "bindings" section. Instead of providing the connection string directly, you provide the name of the application setting (environment variable) that contains the connection string.
    {
      "bindings": [
        {
          "type": "blobTrigger",
          "name": "myBlob",
          "direction": "in",
          "path": "samples-workitems/{name}",
          "connection": "AzureWebJobsStorage"
        }
      ]
    }
    
    

    In this case, the connection is set to AzureWebJobsStorage, which is an application setting (environment variable) that contains the actual connection string for the Azure Storage Account.

    1. In local.settings.json (for local development): When developing locally, the actual connection string for AzureWebJobsStorage is stored in the local.settings.json file:
    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true"
      }
    }
    
    
    1. In Azure (when deployed): When your function is deployed to Azure, you will store the connection string in the Application Settings section under the function app's configuration, where AzureWebJobsStorage is an environment variable. This ensures the actual connection string remains secure and can be changed easily across environments.
    2. In C# Attributes: While the connection property exists on attributes like BlobTriggerAttribute, it’s not set directly in code using the constructor. The function.json handles this configuration, while the attribute's constructor deals with other aspects like the blob path and trigger.

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.