You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Create your first durable function in Azure using JavaScript
3
3
description: Create and publish an Azure Durable Function using Visual Studio Code.
4
-
author: ColbyTresness
4
+
author: anthonychu
5
5
6
6
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
9
9
---
10
10
11
11
# Create your first durable function in JavaScript
@@ -24,126 +24,161 @@ To complete this tutorial:
24
24
25
25
* Install [Visual Studio Code](https://code.visualstudio.com/download).
26
26
27
+
* Install the [Azure Functions](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) VS Code extension
28
+
27
29
* Make sure you have the latest version of the [Azure Functions Core Tools](../functions-run-local.md).
28
30
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.
30
32
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.
## <aname="create-an-azure-functions-project"></a>Create your local project
38
38
39
39
In this section, you use Visual Studio Code to create a local Azure Functions project.
40
40
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...`.
42
42
43
-
1. Choose a directory location for your project workspace and choose **Select**.
> 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**.
47
46
48
-
1. Following the prompts, provide the following information for your desired language:
47
+
1. Following the prompts, provide the following information:
49
48
50
49
| Prompt | Value | Description |
51
50
| ------ | ----- | ----------- |
52
51
| 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. |
58
55
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.
60
57
61
58
A package.json file is also created in the root folder.
62
59
63
-
##Install the Durable Functions npm package
60
+
### Enable Azure Functions V2 compatibility mode
64
61
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.
66
63
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
+
```
68
78
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
70
80
71
-
### HTTP starter
81
+
To work with Durable Functions in a Node.js function app, you use a library called `durable-functions`.
72
82
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.
74
84
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.
76
86
77
-

87
+
## Creating your functions
88
+
89
+
The most basic Durable Functions app contains three functions:
78
90
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.
80
94
81
-

95
+
### Orchestrator function
82
96
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.
1. In the command palette, search for and select `Azure Functions: Create Function...`.
86
100
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 |
88
107
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`.
90
109
91
-
Now, we'll create an orchestrator to coordinate activity functions.
110
+
Next, you'll add the referenced `Hello` activity function.
92
111
93
-
1. From *Azure: Functions*, choose the **Create Function** icon.
112
+
### Activity function
94
113
95
-

114
+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
96
115
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:
98
117
99
-

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 |
100
122
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.
102
124
103
-
### Activity
125
+
Finally, you'll add an HTTP triggered function that starts the orchestration.
104
126
105
-
Now, we'll create an activity function to actually carry out the work of the solution.
127
+
### Client function (HTTP starter)
106
128
107
-
1.From *Azure: Functions*, choose the **Create Function** icon.
129
+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
108
130
109
-

131
+
1. Following the prompts, provide the following information:
110
132
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 |
112
138
113
-

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.
114
140
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.
116
142
117
143
## Test the function locally
118
144
119
145
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.
120
146
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.
122
148
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.
124
151
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**.
126
153
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`
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 |
131
165
132
-

166
+
1. In the **Terminal** panel, copy the URL endpoint of your HTTP-triggered function.
133
167
134
-
4. Replace `{functionName}` with `DurableFunctionsOrchestrator`.
168
+

135
169
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`.
137
171
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.
139
173
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.
141
175
142
176
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:
143
177
144
178
```json
145
179
{
146
-
"instanceId": "d495cb0ac10d4e13b22729c37e335190",
180
+
"name": "HelloOrchestrator",
181
+
"instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
147
182
"runtimeStatus": "Completed",
148
183
"input": null,
149
184
"customStatus": null,
@@ -152,24 +187,36 @@ Azure Functions Core Tools lets you run an Azure Functions project on your local
152
187
"Hello Seattle!",
153
188
"Hello London!"
154
189
],
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"
157
192
}
158
193
```
159
194
160
-
7. To stop debugging, press **Shift + F5** in VS Code.
195
+
1. To stop debugging, press **Shift + F5** in VS Code.
161
196
162
197
After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.
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`
173
220
174
221
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.
0 commit comments