How to assign value for connection in EventHubTrigger.

Chandan Kumar Singh 41 Reputation points
2022-09-28T05:28:48.143+00:00

Hi Team,

There is requirement to set the connection string in EventHubTrigger dynamically. Either using KeyVault, CI/CD pipeline or other config file. I tried multiple way but its not working. Somehow we can not expose connection string in function app configuration as well. What are the possible way to implement this.

sample code:
[Function("HubTriggerFunction")]
public void Run([EventHubTrigger("demoeventhubname", Connection = "demoeventhunconnection")] string[] input)
{
_logger.LogInformation($"Test Event Hub: {input[0]}");
}

My Local Setting file:

"demoeventhunconnection": "@Microsoft.KeyVault(VaultName=demo-dev-kv;SecretName=AzureEventHubConnectionString)"

Thanks,
Chandan Singh

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
{count} votes

Answer accepted by question author
  1. Bruno Lucas 4,436 Reputation points MVP
    2022-09-28T09:33:32.377+00:00

    Hi @Chandan Kumar Singh

    Rewinding back to before adding the connection string to the vault, did that work if you add the hub connection string direct in local.settings.json?

    Try to add like this first and hit F5 to make sure you can first make work like this and be sure the value you will add to the vault is correct
    245501-image.png

    Once you are sure it works without the vault:

    1. make sure you are logged in in Visual Studio with the same login you use to access azure (will use different account for test/prod in the cloud)

    2. go to the vault , create the secret and add the connection string to the value

    245502-image.png

    3. in the left menu in the vault, click access policies

    245495-image.png

    4. select a template. we are only reading secrets:

    245428-image.png

    5.We are just reading, so get and list should be enough

    245506-image.png

    6. step 2 we add the same user we have in Visual studio, I login in VS as myself, so I grant the permission to myself. Different story when you deploy to test or prod in the cloud. there you can use a proper service account

    245496-image.png

    7.skipp all to way to the last step and create. You should now have a policy that will allow you to use the vault reference

    245456-image.png

    make sure you are using the correct sintax for vault reference: https://learn.microsoft.com/en-us/azure/app-service/app-service-key-vault-references?tabs=azure-cli#reference-syntax

    This worked for me:

    245437-image.png

    Case that information helps, don't forget to vote or mark as Answer to help the community. Cheers!

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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