Add messages to an Azure Storage queue using Functions

In Azure Functions, input and output bindings provide a declarative way to make data from external services available to your code. In this quickstart, you use an output binding to create a message in a queue when a function is triggered by an HTTP request. You use Azure Storage Explorer to view the queue messages that your function creates:

Queue message shown in Storage Explorer

Prerequisites

To complete this quickstart:

Add an output binding

In this section, you use the portal UI to add a queue storage output binding to the function you created earlier. This binding will make it possible to write minimal code to create a message in a queue. You don't have to write code for tasks such as opening a storage connection, creating a queue, or getting a reference to a queue. The Azure Functions runtime and queue output binding take care of those tasks for you.

  1. In the Azure portal, open the function app page for the function app that you created in Create your first function from the Azure portal. To do this, select All services > Function Apps, and then select your function app.

  2. Select the function that you created in that earlier quickstart.

  3. Select Integrate > New output > Azure Queue storage.

  4. Click Select.

    Add a Queue storage output binding to a function in the Azure portal.

  5. If you get an Extensions not installed message, choose Install to install the Storage bindings extension in the function app. This may take a minute or two.

    Install the Storage binding extension

  6. Under Azure Queue Storage output, use the settings as specified in the table that follows this screenshot:

    Add a Queue storage output binding to a function in the Azure portal.

    Setting Suggested value Description
    Message parameter name outputQueueItem The name of the output binding parameter.
    Storage account connection AzureWebJobsStorage You can use the storage account connection already being used by your function app, or create a new one.
    Queue name outqueue The name of the queue to connect to in your Storage account.
  7. Click Save to add the binding.

Now that you have an output binding defined, you need to update the code to use the binding to add messages to a queue.

Add code that uses the output binding

In this section, you add code that writes a message to the output queue. The message includes the value that is passed to the HTTP trigger in the query string. For example, if the query string includes name=Azure, the queue message will be Name passed to the function: Azure.

  1. Select your function to display the function code in the editor.

  2. Update the function code depending on your function language:

    Add an outputQueueItem parameter to the method signature as shown in the following example.

    public static async Task<IActionResult> Run(HttpRequest req,
        ICollector<string> outputQueueItem, ILogger log)
    {
        ...
    }
    

    In the body of the function just before the return statement, add code that uses the parameter to create a queue message.

    outputQueueItem.Add("Name passed to the function: " + name);
    
  3. Select Save to save changes.

Test the function

  1. After the code changes are saved, select Run.

    Add a Queue storage output binding to a function in the Azure portal.

    Notice that the Request body contains the name value Azure. This value appears in the queue message that is created when the function is invoked.

    As an alternative to selecting Run here, you can call the function by entering a URL in a browser and specifying the name value in the query string. The browser method is shown in the previous quickstart.

  2. Check the logs to make sure that the function succeeded.

A new queue named outqueue is created in your Storage account by the Functions runtime when the output binding is first used. You'll use Storage Explorer to verify that the queue and a message in it were created.

Connect Storage Explorer to your account

Skip this section if you have already installed Storage Explorer and connected it to the storage account that you're using with this quickstart.

  1. Run the Microsoft Azure Storage Explorer tool, select the connect icon on the left, choose Use a storage account name and key, and select Next.

    Run the Storage Account Explorer tool.

  2. In the Azure portal, on the function app page, select your function and then select Integrate.

  3. Select the Azure Queue storage output binding that you added in an earlier step.

  4. Expand the Documentation section at the bottom of the page.

    The portal shows credentials that you can use in Storage Explorer to connect to the storage account.

    Get the Storage account connection credentials.

  5. Copy the Account Name value from the portal and paste it in the Account name box in Storage Explorer.

  6. Click the show/hide icon next to Account Key to display the value, and then copy the Account Key value and paste it in the Account key box in Storage Explorer.

  7. Select Next > Connect.

    Paste the storage credentials and connect.

Examine the output queue

  1. In Storage Explorer, select the storage account that you're using for this quickstart.

  2. Expand the Queues node, and then select the queue named outqueue.

    The queue contains the message that the queue output binding created when you ran the HTTP-triggered function. If you invoked the function with the default name value of Azure, the queue message is Name passed to the function: Azure.

    Queue message shown in Storage Explorer

  3. Run the function again, and you'll see a new message appear in the queue.

Clean up resources

Other quick starts in this collection build upon this quick start. If you plan to work with subsequent quick starts, tutorials, or with any of the services you have created in this quick start, do not clean up the resources.

Resources in Azure refers to function apps, functions, storage accounts, and so forth. They are grouped into resource groups, and you can delete everything in a group by deleting the group.

You created resources to complete these quickstarts. You may be billed for these resources, depending on your account status and service pricing. If you don't need the resources anymore, here's how to delete them:

  1. In the Azure portal, go to the Resource group page.

    To get to that page from the function app page, select the Overview tab and then select the link under Resource group.

    Select the resource group to delete from the function app page.

    To get to that page from the dashboard, select Resource groups, and then select the resource group that you used for this quickstart.

  2. In the Resource group page, review the list of included resources, and verify that they are the ones you want to delete.

  3. Select Delete resource group, and follow the instructions.

    Deletion may take a couple of minutes. When it's done, a notification appears for a few seconds. You can also select the bell icon at the top of the page to view the notification.

Next steps

In this quickstart, you added an output binding to an existing function. For more information about binding to Queue storage, see Azure Functions Storage queue bindings.

Learn how to create functions with other kinds of triggers or how to integrate functions with other Azure services.