user2058904
user2058904

Reputation:

How To Configure Queue Name for Queue Trigger In Azure Function App

I'm creating a function app in Azure and want to use a queue trigger. I know how to configure the queue name at design time, e.g:

[FunctionName("MyTestFunction")]
public static void Run([QueueTrigger("myqueue-items", Connection = "testdelete")]string myQueueItem, TraceWriter log)

However, I'd like to be able to define and reference it in a configuration file. I'm aware of the existence of function.json (Probably this one), host.json and local.settings.json, but I don't know how to set a queue name in there and have it be referenced in the function.

If I deploy a freshly created function created in visual studio (With the new 15.3 update), I can see the following in the function.json file post deployment (even though the file doesn't exist when i develop locally):

  "bindings": [
    {
      "type": "queueTrigger",
      "queueName": "myqueue-items",
      "connection": "testdelete",
      "name": "myQueueItem"
    }

I've found that if I create that file, and change the "queueName" to something that doesn't match the value in the actual function, it unfortunately doesn't override it (That would have been too easy I guess).

How can I reference the bindings in the function.json in the functions QueueTrigger attribute?

Presumably whatever the solution is will allow me to do the same with poison queue handling?

The reason I want to do this, is because I need to deploy multiple instances of the exact same function, but pointing each one at a different queue (In order to get around max memory limitations).

Thanks.

Upvotes: 43

Views: 20795

Answers (2)

Zenuka
Zenuka

Reputation: 3250

I came here googling for a way to dynamically change the queue name and finally figured out how to change the queue name on the start of the function app.

You can leverage the environment variable like Garth proposed:

public static void Run([QueueTrigger("%MyQueueName%", Connection = "testdelete")]string myQueueItem, TraceWriter log)

In the Startup class (that inherits from FunctionsStartup) override the following method and set the queue name to anything you like:

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
    Environment.SetEnvironmentVariable("MyQueueName", $"personal-queue-{Environment.UserName}");
    
    base.ConfigureAppConfiguration(builder);
}

I tried setting this in public override void Configure(IFunctionsHostBuilder builder) but it seems the environment was already read and the MyQueueName variable was not updated. Doing this in the ConfigureAppConfiguration method worked for me on .NET 6.0 (in-process function app)

Edit: after I posted this, I saw camelCase's comment that INameResolver could also do the trick and he's correct!

Upvotes: 2

Garth Mason
Garth Mason

Reputation: 8001

Could you not just reference the queue name as a setting (using the %settingName% syntax) for your App Function? Then in each function app you deploy have change the setting to the required queue name.

[FunctionName("MyTestFunction")]
public static void Run([QueueTrigger("%MyQueueName%", Connection = "testdelete")]string myQueueItem, TraceWriter log)

And specify the setting in local.settings.json for running locally

{
  "Values: {
     "MyQueueName": "myqueue-items"
   }
}

Upvotes: 105

Related Questions