Skip to content

Commit 032a7b1

Browse files
authored
Merge pull request #108750 from anthonychu/20200320-update-durable-js-quickstart
Update JS Durable quickstart
2 parents c5fac5d + 5913a70 commit 032a7b1

File tree

4 files changed

+110
-63
lines changed

4 files changed

+110
-63
lines changed
77.7 KB
Loading
10.3 KB
Loading
186 KB
Loading

articles/azure-functions/durable/quickstart-js-vscode.md

Lines changed: 110 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Create your first durable function in Azure using JavaScript
33
description: Create and publish an Azure Durable Function using Visual Studio Code.
4-
author: ColbyTresness
4+
author: anthonychu
55

66
ms.topic: quickstart
7-
ms.date: 11/07/2018
8-
ms.reviewer: azfuncdf, cotresne
7+
ms.date: 03/24/2020
8+
ms.reviewer: azfuncdf, antchu
99
---
1010

1111
# Create your first durable function in JavaScript
@@ -24,126 +24,161 @@ To complete this tutorial:
2424

2525
* Install [Visual Studio Code](https://code.visualstudio.com/download).
2626

27+
* Install the [Azure Functions](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) VS Code extension
28+
2729
* Make sure you have the latest version of the [Azure Functions Core Tools](../functions-run-local.md).
2830

29-
* On a Windows computer, verify you have the [Azure Storage Emulator](../../storage/common/storage-use-emulator.md) installed and running. On a Mac or Linux computer, you must use an actual Azure storage account.
31+
* Durable Functions require an Azure storage account. You need an Azure subscription.
3032

31-
* Make sure that you have version 8.0 or a later version of [Node.js](https://nodejs.org/) installed.
33+
* Make sure that you have version 10.x or 12.x of [Node.js](https://nodejs.org/) installed.
3234

3335
[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
3436

35-
[!INCLUDE [functions-install-vs-code-extension](../../../includes/functions-install-vs-code-extension.md)]
36-
3737
## <a name="create-an-azure-functions-project"></a>Create your local project
3838

3939
In this section, you use Visual Studio Code to create a local Azure Functions project.
4040

41-
1. In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select `Azure Functions: Create new project...`.
41+
1. In Visual Studio Code, press F1 (or Ctrl/Cmd+Shift+P) to open the command palette. In the command palette, search for and select `Azure Functions: Create New Project...`.
4242

43-
1. Choose a directory location for your project workspace and choose **Select**.
43+
![Create function](media/quickstart-js-vscode/functions-create-project.png)
4444

45-
> [!NOTE]
46-
> These steps were designed to be completed outside of a workspace. In this case, do not select a project folder that is part of a workspace.
45+
1. Choose an empty folder location for your project and choose **Select**.
4746

48-
1. Following the prompts, provide the following information for your desired language:
47+
1. Following the prompts, provide the following information:
4948

5049
| Prompt | Value | Description |
5150
| ------ | ----- | ----------- |
5251
| Select a language for your function app project | JavaScript | Create a local Node.js Functions project. |
53-
| Select a version | Azure Functions v2 | You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app. |
54-
| Select a template for your project's first function | HTTP trigger | Create an HTTP triggered function in the new function app. |
55-
| Provide a function name | HttpTrigger | Press Enter to use the default name. |
56-
| Authorization level | Function | The `function` authorization level requires you to supply an access key when calling your function's HTTP endpoint. This makes it more difficult to access an unsecured endpoint. To learn more, see [Authorization keys](../functions-bindings-http-webhook-trigger.md#authorization-keys). |
57-
| Select how you would like to open your project | Add to workspace | Creates the function app in the current workspace. |
52+
| Select a version | Azure Functions v3 | You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app. |
53+
| Select a template for your project's first function | Skip for now | |
54+
| Select how you would like to open your project | Open in current window | Reopens VS Code in the folder you selected. |
5855

59-
Visual Studio Code installs the Azure Functions Core Tools, if needed. It also creates a function app project in a new workspace. This project contains the [host.json](../functions-host-json.md) and [local.settings.json](../functions-run-local.md#local-settings-file) configuration files. It also creates an HttpExample folder that contains the [function.json definition file](../functions-reference-node.md#folder-structure) and the [index.js file](../functions-reference-node.md#exporting-a-function), a Node.js file that contains the function code.
56+
Visual Studio Code installs the Azure Functions Core Tools, if needed. It also creates a function app project in a folder. This project contains the [host.json](../functions-host-json.md) and [local.settings.json](../functions-run-local.md#local-settings-file) configuration files.
6057

6158
A package.json file is also created in the root folder.
6259

63-
## Install the Durable Functions npm package
60+
### Enable Azure Functions V2 compatibility mode
6461

65-
1. Install the `durable-functions` npm package by running `npm install durable-functions` in the root directory of the function app.
62+
Currently, JavaScript Durable Functions require Azure Functions V2 compatibility mode to be enabled.
6663

67-
## Creating your functions
64+
1. Open *local.settings.json* to edit the settings used when running the app locally.
65+
66+
1. Add a setting named `FUNCTIONS_V2_COMPATIBILITY_MODE` with a value of `true`.
67+
68+
```json
69+
{
70+
"IsEncrypted": false,
71+
"Values": {
72+
"AzureWebJobsStorage": "",
73+
"FUNCTIONS_WORKER_RUNTIME": "node",
74+
"FUNCTIONS_V2_COMPATIBILITY_MODE": "true"
75+
}
76+
}
77+
```
6878

69-
We'll now create the three functions you need to get started with Durable Functions: an HTTP starter, an orchestrator, and an activity function. The HTTP starter will initiate your entire solution, and the orchestrator will dispatch work to various activity functions.
79+
## Install the Durable Functions npm package
7080

71-
### HTTP starter
81+
To work with Durable Functions in a Node.js function app, you use a library called `durable-functions`.
7282

73-
First, create an HTTP triggered function that starts a durable function orchestration.
83+
1. Use the *View* menu or Ctrl+Shift+` to open a new terminal in VS Code.
7484

75-
1. From *Azure: Functions*, choose the **Create Function** icon.
85+
1. Install the `durable-functions` npm package by running `npm install durable-functions` in the root directory of the function app.
7686

77-
![Create a function](./media/quickstart-js-vscode/create-function.png)
87+
## Creating your functions
88+
89+
The most basic Durable Functions app contains three functions:
7890

79-
2. Select the folder with your function app project and select the **Durable Functions HTTP Starter** function template.
91+
* *Orchestrator function* - describes a workflow that orchestrates other functions.
92+
* *Activity function* - called by the orchestrator function, performs work, and optionally returns a value.
93+
* *Client function* - a regular Azure Function that starts an orchestrator function. This example uses an HTTP triggered function.
8094

81-
![Choose the HTTP starter template](./media/quickstart-js-vscode/create-function-choose-template.png)
95+
### Orchestrator function
8296

83-
3. Leave the default name as `DurableFunctionsHttpStart` and press ****Enter**, then select **Anonymous** authentication.
97+
You use a template to create the durable function code in your project.
8498

85-
![Choose anonymous authentication](./media/quickstart-js-vscode/create-function-anonymous-auth.png)
99+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
86100

87-
We've now created an entry-point into our Durable Function. Let's add an orchestrator.
101+
1. Following the prompts, provide the following information:
102+
103+
| Prompt | Value | Description |
104+
| ------ | ----- | ----------- |
105+
| Select a template for your function | Durable Functions orchestrator | Create a Durable Functions orchestration |
106+
| Provide a function name | HelloOrchestrator | Name of your durable function |
88107

89-
### Orchestrator
108+
You've added an orchestrator to coordinate activity functions. Open *HelloOrchestrator/index.js* to see the orchestrator function. Each call to `context.df.callActivity` invokes an activity function named `Hello`.
90109

91-
Now, we'll create an orchestrator to coordinate activity functions.
110+
Next, you'll add the referenced `Hello` activity function.
92111

93-
1. From *Azure: Functions*, choose the **Create Function** icon.
112+
### Activity function
94113

95-
![Create a function](./media/quickstart-js-vscode/create-function.png)
114+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
96115

97-
2. Select the folder with your function app project and select the **Durable Functions orchestrator** function template. Leave the name as the default "DurableFunctionsOrchestrator"
116+
1. Following the prompts, provide the following information:
98117

99-
![Choose the orchestrator template](./media/quickstart-js-vscode/create-function-choose-template.png)
118+
| Prompt | Value | Description |
119+
| ------ | ----- | ----------- |
120+
| Select a template for your function | Durable Functions activity | Create an activity function |
121+
| Provide a function name | Hello | Name of your activity function |
100122

101-
We've added an orchestrator to coordinate activity functions. Let's now add the referenced activity function.
123+
You've added the `Hello` activity function that is invoked by the orchestrator. Open *Hello/index.js* to see that it's taking a name as input and returning a greeting. An activity function is where you'll perform actions such as making a database call or performing a computation.
102124

103-
### Activity
125+
Finally, you'll add an HTTP triggered function that starts the orchestration.
104126

105-
Now, we'll create an activity function to actually carry out the work of the solution.
127+
### Client function (HTTP starter)
106128

107-
1. From *Azure: Functions*, choose the **Create Function** icon.
129+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
108130

109-
![Create a function](./media/quickstart-js-vscode/create-function.png)
131+
1. Following the prompts, provide the following information:
110132

111-
2. Select the folder with your function app project and select the **Durable Functions activity** function template. Leave the name as the default "Hello".
133+
| Prompt | Value | Description |
134+
| ------ | ----- | ----------- |
135+
| Select a template for your function | Durable Functions HTTP starter | Create an HTTP starter function |
136+
| Provide a function name | DurableFunctionsHttpStart | Name of your activity function |
137+
| Authorization level | Anonymous | For demo purposes, allow the function to be called without authentication |
112138

113-
![Choose the activity template](./media/quickstart-js-vscode/create-function-choose-template.png)
139+
You've added an HTTP triggered function that starts an orchestration. Open *DurableFunctionsHttpStart/index.js* to see that it uses `client.startNew` to start a new orchestration. Then it uses `client.createCheckStatusResponse` to return an HTTP response containing URLs that can be used to monitor and manage the new orchestration.
114140

115-
We've now added all components needed to start off an orchestration and chain together activity functions.
141+
You now have a Durable Functions app that can be run locally and deployed to Azure.
116142

117143
## Test the function locally
118144

119145
Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. You're prompted to install these tools the first time you start a function from Visual Studio Code.
120146

121-
1. On a Windows computer, start the Azure Storage Emulator and make sure that the **AzureWebJobsStorage** property of *local.settings.json* is set to `UseDevelopmentStorage=true`.
147+
1. To test your function, set a breakpoint in the `Hello` activity function code (*Hello/index.js*). Press F5 or select `Debug: Start Debugging` from the command palette to start the function app project. Output from Core Tools is displayed in the **Terminal** panel.
122148

123-
For Storage Emulator 5.8 make sure that the **AzureWebJobsSecretStorageType** property of local.settings.json is set to `files`. On a Mac or Linux computer, you must set the **AzureWebJobsStorage** property to the connection string of an existing Azure storage account. You create a storage account later in this article.
149+
> [!NOTE]
150+
> Refer to the [Durable Functions Diagnostics](durable-functions-diagnostics.md#debugging) for more information on debugging.
124151

125-
2. To test your function, set a breakpoint in the function code and press F5 to start the function app project. Output from Core Tools is displayed in the **Terminal** panel. If this is your first time using Durable Functions, the Durable Functions extension is installed and the build might take a few seconds.
152+
1. Durable Functions requires an Azure Storage account to run. When VS Code prompts you to select a storage account, choose **Select storage account**.
126153

127-
> [!NOTE]
128-
> JavaScript Durable Functions require version **1.7.0** or greater of the **Microsoft.Azure.WebJobs.Extensions.DurableTask** extension. Run the following command from the root folder of your Azure Functions app to install the Durable Functions extension `func extensions install -p Microsoft.Azure.WebJobs.Extensions.DurableTask -v 1.7.0`
154+
![Create storage account](media/quickstart-js-vscode/functions-select-storage.png)
129155

130-
3. In the **Terminal** panel, copy the URL endpoint of your HTTP-triggered function.
156+
1. Following the prompts, provide the following information to create a new storage account in Azure.
157+
158+
| Prompt | Value | Description |
159+
| ------ | ----- | ----------- |
160+
| Select subscription | *name of your subscription* | Select your Azure subscription |
161+
| Select a storage account | Create a new storage account | |
162+
| Enter the name of the new storage account | *unique name* | Name of the storage account to create |
163+
| Select a resource group | *unique name* | Name of the resource group to create |
164+
| Select a location | *region* | Select a region close to you |
131165

132-
![Azure local output](../media/functions-create-first-function-vs-code/functions-vscode-f5.png)
166+
1. In the **Terminal** panel, copy the URL endpoint of your HTTP-triggered function.
133167

134-
4. Replace `{functionName}` with `DurableFunctionsOrchestrator`.
168+
![Azure local output](media/quickstart-js-vscode/functions-f5.png)
135169

136-
5. Using a tool like [Postman](https://www.getpostman.com/) or [cURL](https://curl.haxx.se/), send an HTTP POST request to the URL endpoint.
170+
1. Using a tool like [Postman](https://www.getpostman.com/) or [cURL](https://curl.haxx.se/), send an HTTP POST request to the URL endpoint. Replace the last segment with the name of the orchestrator function (`HelloOrchestrator`). The URL should be similar to `http://localhost:7071/api/orchestrators/HelloOrchestrator`.
137171

138-
The response is the initial result from the HTTP function letting us know the durable orchestration has started successfully. It is not yet the end result of the orchestration. The response includes a few useful URLs. For now, let's query the status of the orchestration.
172+
The response is the initial result from the HTTP function letting you know the durable orchestration has started successfully. It is not yet the end result of the orchestration. The response includes a few useful URLs. For now, let's query the status of the orchestration.
139173

140-
6. Copy the URL value for `statusQueryGetUri` and paste it in the browser's address bar and execute the request. Alternatively you can also continue to use Postman to issue the GET request.
174+
1. Copy the URL value for `statusQueryGetUri` and paste it in the browser's address bar and execute the request. Alternatively you can also continue to use Postman to issue the GET request.
141175

142176
The request will query the orchestration instance for the status. You should get an eventual response, which shows us the instance has completed, and includes the outputs or results of the durable function. It looks like:
143177

144178
```json
145179
{
146-
"instanceId": "d495cb0ac10d4e13b22729c37e335190",
180+
"name": "HelloOrchestrator",
181+
"instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
147182
"runtimeStatus": "Completed",
148183
"input": null,
149184
"customStatus": null,
@@ -152,24 +187,36 @@ Azure Functions Core Tools lets you run an Azure Functions project on your local
152187
"Hello Seattle!",
153188
"Hello London!"
154189
],
155-
"createdTime": "2018-11-08T07:07:40Z",
156-
"lastUpdatedTime": "2018-11-08T07:07:52Z"
190+
"createdTime": "2020-03-18T21:54:49Z",
191+
"lastUpdatedTime": "2020-03-18T21:54:54Z"
157192
}
158193
```
159194

160-
7. To stop debugging, press **Shift + F5** in VS Code.
195+
1. To stop debugging, press **Shift + F5** in VS Code.
161196

162197
After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.
163198

164199
[!INCLUDE [functions-create-function-app-vs-code](../../../includes/functions-sign-in-vs-code.md)]
165200

166201
[!INCLUDE [functions-publish-project-vscode](../../../includes/functions-publish-project-vscode.md)]
167202

168-
## Test your function in Azure
203+
### Enable Azure Functions V2 compatibility mode
204+
205+
The same Azure Functions V2 compatibility that you enabled locally needs to be enabled in the app in Azure.
206+
207+
1. Using the command palette, search for and select `Azure Functions: Edit Setting...`.
208+
209+
1. Follow the prompts to locate your function app in your Azure subscription.
169210

170-
1. Copy the URL of the HTTP trigger from the **Output** panel. The URL that calls your HTTP-triggered function should be in the following format:
211+
1. Select `Create new App Setting...`.
212+
213+
1. Enter a new setting key of `FUNCTIONS_V2_COMPATIBILITY_MODE`.
214+
215+
1. Enter a setting value of `true`.
216+
217+
## Test your function in Azure
171218

172-
http://<functionappname>.azurewebsites.net/orchestrators/<functionname>
219+
1. Copy the URL of the HTTP trigger from the **Output** panel. The URL that calls your HTTP-triggered function should be in this format: `http://<functionappname>.azurewebsites.net/orchestrators/HelloOrchestrator`
173220

174221
2. Paste this new URL for the HTTP request into your browser's address bar. You should get the same status response as before when using the published app.
175222

0 commit comments

Comments
 (0)