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 a Azure Queue Storage by adding an output binding to your Visual Studio Code project. |
02/07/2020 |
quickstart |
csharp, java, javascript, powershell, python, typescript |
devx-track-python, devx-track-js, mode-ui |
programming-languages-set-functions |
[!INCLUDE functions-add-storage-binding-intro]
This article shows you 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
.
Before you start this article, you must meet the following requirements:
-
Install the Azure Storage extension for Visual Studio Code.
-
Install Azure Storage Explorer. Storage Explorer is a tool 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. ::: zone-end
::: zone pivot="programming-language-csharp"
- 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 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 the F1 key 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 it contains secrets, the local.settings.json file never gets published, and is excluded from 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-javascript,programming-language-typescript,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 usage is enabled in the host.json file at the root of the project, which appears as follows:
:::code language="json" source="~/functions-quickstart-java/functions-add-output-binding-storage-queue/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
In Functions, each type of binding requires a direction
, type
, and a unique name
to be defined in the function.json file. The way you define these attributes depends on the language of your function app.
::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-python,programming-language-powershell"
[!INCLUDE functions-add-output-binding-json]
::: 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 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 choose Execute Function Now....:::image type="content" source="../../includes/media/functions-run-function-test-local-vs-code/execute-function-now.png" alt-text="Execute function now from Visual Studio Code":::
-
In 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 are 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
Skip this section if you have already installed Azure Storage Explorer and connected it to your Azure account.
-
Run the Azure Storage Explorer tool, select the connect icon on the left, and select Add an account.
-
In the Connect dialog, choose Add an Azure account, choose your Azure environment, and select Sign in....
After you successfully sign in to your account, you see all of the Azure subscriptions associated with your account.
-
In Visual Studio Code, press the F1 key 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 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. -
Run the function again, send another request, and you'll see a new message appear 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 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 again 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 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