Azure Function with Storage Queue Trigger
Tags: azure, serverless, azurefunctions, faas
Published: 2022-10-03 15:13:07

---
title: Azure Function with Storage Queue Trigger
published: true
date: 2022-10-03 11:29:39 UTC
tags: #azure #serverless #azurefunctions #faas
canonical_url: https://blog.cloudbuff.in/azure-functions-part-4
---

In the 4th article of this series, we will create a function that gets triggered when a message arrives in a Storage Queue. We will run a storage emulator and test our function locally.

> Previous articles in this series: [Azure Functions: overview of concepts](https://dev.to/sjejurkar/up-and-running-with-azure-functions-part-1-1j22), [Creating our first Azure Function](https://dev.to/sjejurkar/up-and-running-with-azure-functions-part-2-150d), and [Deploying our first function to Azure](https://dev.to/sjejurkar/deploying-our-first-function-to-azure-1kcb)

Before we can create and test our function locally, we need to specify the queue that will be used to trigger our function. Since we don't have any queues currently, we need to create one. We want to run and test our function locally without connecting to Azure cloud, so our queue must exist locally.

## Install Azurite extension for VS Code

To create a Storage account locally on our machine, we will use [Azurite](https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=visual-studio), a free, open-source emulator that provides local environment for testing Azure Blob, Queue Storage, and Table Storage applications. The emulator provides cross-platform support on Windows, Linux, and macOS. There are number of ways to install Azurite. Since we are using VS Code as our IDE, we will install it as a VS Code extension.

Click the Extensions icon on the vertical left menu. Type `azurite` in the search field, click on `Azurite` in the search results. Click on Install button to install this extension.

![VSCode_azurite_extension.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664631784838/B-JI-YNch.png)

## Start Azurite

After the extension is installed, we will start up `Azurite` so we can create a storage queue. Open the Command Palette using keyboard shortcut `Ctrl + Shift + P` (use `Cmd + Shift + P` on Mac) or `View > Command Palette...` menu option. Type command `Azurite: Start` and click on `Azurite: Start` option.

![00_VSCode_azurite_start.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664731785680/fAIkx8Zop.png)

Once the Azurite service starts, we should see it's status in the bottom toolbar of VS Code saying that it is running on `http://127.0.0.1:10000`.

![00_VSCode_azurite_running_status.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664732110094/YMY7x327G.png)

## Azure Storage Explorer

Now that we have the Storage emulator running, we need to create our queue. For this, let's download and install [Azure Storage Explorer](https://azure.microsoft.com/en-us/products/storage/storage-explorer). Azure Storage Explorer is a desktop application to conveniently manage Azure cloud storage resources. It can also connect to Azurite and help us manage local storage.

Let's start Azure Storage Explorer and click on the `Explorer` icon on the menu on the left. We will then navigate to `Emulator & Attached > LocalStorage (Key) and Queues`. Right click on the `Queues` menu and click on `Create Queue` from the pop-up menu to create a new queue.

![01_Az_Storage_Explorer_local.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664725865957/JAy2qPz5n.png)

Let's name the new queue `blog-queue`. We will use this queue as the trigger for our function.

![02_Az_Storage_Exp_local_queue.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664725875076/k6C01hu2l.png)

## Create a function with Storage Queue trigger

Our environment is now set up, so it is time to create our function. Let's go back to VS Code, open the Command Palette and type `Azure Functions: Create` into text box and select `Azure Functions: Create Function...` option.

![03_VSCode_new_fn.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664724924826/iMLGAWeNd.png)

Next, we need to choose the function trigger. We want this function to be triggered when a message is received in a Storage Queue, so type `Queue` in the text box and choose `Azure Queue Storage trigger`.

![04_VSCode_fn_Store_Q_trg.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664724934580/3osSyXcCO.png)

We will name this function `SimpleQueueFunction`.

![05_VSCode_fn_name.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664724944134/7tVeG1EaW.png)

We will continue to use CloudBuff.Function as the namespace.

![06_VSCode_fn_name_space.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664724953751/-Nkp47KW0.png)

In the next step, we will choose `AzureWebJobsStorage`.

![07_VSCode_fn_storage.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664732586378/-1BnSf12T.png)

Next, we will provide name of the queue we created `blog-queue` since we want the function to run when a message arrives in this queue. ![08_VSCode_fn_queue_name.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664725475045/Oge46ajBd.png)

We will then be asked to select a Storage account for internal use by the Azure Functions runtime. We will go with `Use local emulator` option. ![09_VSCode_fn_store_emulator.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664725488750/v084Epqs_.png)

Our function will be created as `SimpleQueueFunction.cs`. Note that `Run` function has binding for `blog-queue` and `AzureWebJobsStorage` as the connection string.

![10_VSCode_fn_code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664732796059/Kl3K-30Pn.png)

As we will be using Azurite emulator for local testing, we need to verify that it is associated with our function. To do that, we can check the `AzureWebJobsStorage` property in file `local.settings.json`. It should be set to `UseDevelopmentStorage=true` as we chose the `Use local emulator` option earlier.

![11_VSCode_fn_local_settings.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664732916595/X8zVTdibU.png)

## Test the function

To run the function, click the option `Run Without Debugging` under the `Run` menu. VS Code will build the project and run it. Since we have two functions - SimpleHttpFunction that we created previously and SimpleQueueFunction that we just created - we should see both come up.

![12_VSCode_fn_run.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664732930578/TxC1rpr9-.png)

Now, lets add a message to `blog-queue`. Go to Azure Storage Explorer, click on `blog-queue` and click `Add Message` button on the tab that opens.

![13_Az_Storage_Q_add.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664733267567/Gek6YL3d3.png)

The Add Message window will pop up. Let's use a simple text message `Blog Message #1`. Add the message text and click OK to add it to the queue ![14_Az_Storage_Q_new_msg.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664733281444/nAnhlgpGq.png)

Adding the message to the queue should trigger our function and we should see the message content being logged in the VS Code terminal window. ![15_VSCode_fn_msg_read_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664733293828/y7l82vdjL.png)

Now, let's add another message to the queue. This one is a JSON message `{ "message": "This is a JSON blog message" }`. We should also see the message being logged in the terminal window. ![15_VSCode_fn_msg_read_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664733305059/OB8OUEoPM.png)

And that's it!! We have successfully created and tested our second function.

## Conclusion

In this article, we created a function that gets triggered when a message arrives in a Storage Queue and tested it locally. We saw how Azurite can be used to emulate a Storage account locally. We used Azure Storage Explorer tool to create a storage queue and add test messages to it.

In the next article, we will deploy this function to Azure cloud and use an actual Azure Storage queue to test it.