title | description | ms.date | ms.topic | ms.devlang | ms.custom | zone_pivot_groups |
---|---|---|---|---|---|---|
Connect Azure Functions to Azure Storage using Visual Studio Code |
Learn how to connect Azure Functions to an Azure Queue Storage by adding an output binding to your Visual Studio Code project. |
04/25/2024 |
quickstart |
csharp |
devx-track-python, devx-track-js, mode-ui, devdivchpfy22, devx-track-extended-java, devx-track-ts |
programming-languages-set-functions |
[!INCLUDE functions-add-storage-binding-intro]
In this article, you learn how to use Visual Studio Code to connect Azure Storage to the function you created in the previous quickstart article. The output binding that you add to this function writes data from the HTTP request to a message in an Azure Queue storage queue.
Most bindings require a stored connection string that Functions uses to access the bound service. To make it easier, you use the storage account that you created with your function app. The connection to this account is already stored in an app setting named AzureWebJobsStorage
.
::: zone pivot="programming-language-javascript"
Note
This article currently supports Node.js v4 for Functions.
::: zone-end
Before you begin, you must meet the following requirements:
-
Install the Azure Storage extension for Visual Studio Code.
-
Install Azure Storage Explorer. Storage Explorer is a tool that you'll use to examine queue messages generated by your output binding. Storage Explorer is supported on macOS, Windows, and Linux-based operating systems.
::: zone pivot="programming-language-csharp"
-
Install .NET Core CLI tools.
-
Complete the steps in part 1 of the Visual Studio Code quickstart. ::: zone-end
::: zone pivot="programming-language-javascript" -
Complete the steps in part 1 of the Visual Studio Code quickstart. ::: zone-end ::: zone pivot="programming-language-java"
-
Complete the steps in part 1 of the Visual Studio Code quickstart. ::: zone-end ::: zone pivot="programming-language-typescript"
-
Complete the steps in part 1 of the Visual Studio Code quickstart. ::: zone-end ::: zone pivot="programming-language-python"
-
Complete the steps in part 1 of the Visual Studio Code quickstart. ::: zone-end ::: zone pivot="programming-language-powershell"
-
Complete the steps in part 1 of the Visual Studio Code quickstart. ::: zone-end
This article assumes that you're already signed in to your Azure subscription from Visual Studio Code. You can sign in by running Azure: Sign In
from the command palette.
In the previous quickstart article, you created a function app in Azure along with the required storage account. The connection string for this account is stored securely in the app settings in Azure. In this article, you write messages to a Storage queue in the same account. To connect to your storage account when running the function locally, you must download app settings to the local.settings.json file.
-
Press F1 to open the command palette, then search for and run the command
Azure Functions: Download Remote Settings...
. -
Choose the function app you created in the previous article. Select Yes to all to overwrite the existing local settings.
[!IMPORTANT]
Because the local.settings.json file contains secrets, it never gets published, and is excluded from the source control. -
Copy the value
AzureWebJobsStorage
, which is the key for the storage account connection string value. You use this connection to verify that the output binding works as expected.
Because you're using a Queue storage output binding, you must have the Storage bindings extension installed before you run the project.
::: zone pivot="programming-language-python,programming-language-powershell,programming-language-java"
Your project has been configured to use extension bundles, which automatically installs a predefined set of extension packages.
Extension bundles is already enabled in the host.json file at the root of the project, which should look like the following example:
:::code language="json" source="~/functions-docs-python/functions-add-output-binding-storage-queue-cli/host.json":::
Now, you can add the storage output binding to your project.
::: zone-end
::: zone pivot="programming-language-javascript,programming-language-typescript"
Your project has been configured to use extension bundles, which automatically installs a predefined set of extension packages.
Extension bundles is already enabled in the host.json file at the root of the project, which should look like the following example:
:::code language="json" source="~/functions-docs-javascript/functions-add-output-binding-storage-queue-cli-v4-programming-model/host.json":::
Now, you can add the storage output binding to your project.
::: zone-end
::: zone pivot="programming-language-csharp"
[!INCLUDE functions-register-storage-binding-extension-csharp]
::: zone-end
::: zone pivot="programming-language-javascript"
To write to an Azure Storage queue:
-
Add an
extraOutputs
property to the binding configuration{ methods: ['GET', 'POST'], extraOutputs: [sendToQueue], // add output binding to HTTP trigger authLevel: 'anonymous', handler: () => {} }
-
Add a
output.storageQueue
function above theapp.http
call:::code language="javascript" source="~/functions-docs-javascript/functions-add-output-binding-storage-queue-cli-v4-programming-model/src/functions/httpTrigger1.js" range="3-6"::: ::: zone-end
::: zone pivot="programming-language-typescript"
To write to an Azure Storage queue:
-
Add an
extraOutputs
property to the binding configuration{ methods: ['GET', 'POST'], extraOutputs: [sendToQueue], // add output binding to HTTP trigger authLevel: 'anonymous', handler: () => {} }
-
Add a
output.storageQueue
function above theapp.http
call:::code language="typescript" source="~/functions-docs-javascript/functions-add-output-binding-storage-queue-cli-v4-programming-model-ts/src/functions/httpTrigger1.ts" range="10-13":::
::: zone-end
::: zone pivot="programming-language-powershell"
In Functions, each type of binding requires a direction
, type
, and unique name
. The way you define these attributes depends on the language of your function app.
[!INCLUDE functions-add-output-binding-json]
::: zone-end
::: zone pivot="programming-language-python"
[!INCLUDE functions-add-storage-binding-python]
::: zone-end
::: zone pivot="programming-language-csharp"
[!INCLUDE functions-add-storage-binding-csharp-library]
::: zone-end
::: zone pivot="programming-language-java"
[!INCLUDE functions-add-output-binding-java]
::: zone-end
After the binding is defined, you can use the name
of the binding to access it as an attribute in the function signature. By using an output binding, you don't have to use the Azure Storage SDK code for authentication, getting a queue reference, or writing data. The Functions runtime and queue output binding do those tasks for you.
::: zone pivot="programming-language-javascript"
[!INCLUDE functions-add-output-binding-js]
::: zone-end
::: zone pivot="programming-language-typescript"
[!INCLUDE functions-add-output-binding-ts]
::: zone-end
::: zone pivot="programming-language-powershell"
[!INCLUDE functions-add-output-binding-powershell]
::: zone-end
::: zone pivot="programming-language-python"
[!INCLUDE functions-add-output-binding-python]
::: zone-end
::: zone pivot="programming-language-csharp"
[!INCLUDE functions-add-storage-binding-csharp-library-code]
::: zone-end
::: zone pivot="programming-language-java"
[!INCLUDE functions-add-storage-binding-java-code]
[!INCLUDE functions-add-output-binding-java-test]
::: zone-end
::: zone pivot="programming-language-csharp"
[!INCLUDE functions-run-function-test-local-vs-code-csharp]
::: zone-end
::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-python,programming-language-powershell,programming-language-java"
-
As in the previous article, press F5 to start the function app project and Core Tools.
-
With the Core Tools running, go to the Azure: Functions area. Under Functions, expand Local Project > Functions. Right-click (Ctrl-click on Mac) the
HttpExample
function and select Execute Function Now....:::image type="content" source="../../includes/media/functions-run-function-test-local-vs-code/execute-function-now.png" alt-text="Screenshot of executing function from Visual Studio Code.":::
-
In the Enter request body, you see the request message body value of
{ "name": "Azure" }
. Press Enter to send this request message to your function. -
After a response is returned, press Ctrl + C to stop Core Tools.
Because you're using the storage connection string, your function connects to the Azure storage account when running locally. 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 was created along with the new message.
::: zone-end
[!INCLUDE functions-storage-explorer-connect.md]
-
In Visual Studio Code, press F1 to open the command palette, then search for and run the command
Azure Storage: Open in Storage Explorer
and choose your storage account name. Your storage account opens in the Azure Storage Explorer. -
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.:::image type="content" source="./media/functions-add-output-binding-storage-queue-vs-code/function-queue-storage-output-view-queue.png" alt-text="Screenshot of the queue message shown in Azure Storage Explorer.":::
-
Run the function again, send another request, and you see a new message in the queue.
Now, it's time to republish the updated function app to Azure.
-
In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select
Azure Functions: Deploy to function app...
. -
Choose the function app that you created in the first article. Because you're redeploying your project to the same app, select Deploy to dismiss the warning about overwriting files.
-
After the deployment completes, you can again use the Execute Function Now... feature to trigger the function in Azure.
-
Again view the message in the storage queue to verify that the output binding generates a new message in the queue.
In Azure, resources refer to function apps, functions, storage accounts, and so forth. They're grouped into resource groups, and you can delete everything in a group by deleting the group.
You've 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:
[!INCLUDE functions-cleanup-resources-vs-code-inner.md]
You've updated your HTTP triggered function to write data to a Storage queue. Now you can learn more about developing Functions using Visual Studio Code:
-
Azure Functions triggers and bindings. ::: zone pivot="programming-language-csharp"
-
Azure Functions C# developer reference
::: zone-end ::: zone pivot="programming-language-javascript" -
Azure Functions JavaScript developer guide
::: zone-end
::: zone pivot="programming-language-java" -
Azure Functions Java developer guide
::: zone-end
::: zone pivot="programming-language-typescript" -
Azure Functions TypeScript developer guide
::: zone-end
::: zone pivot="programming-language-python" -
Azure Functions Python developer guide
::: zone-end
::: zone pivot="programming-language-powershell" -
Azure Functions PowerShell developer guide ::: zone-end