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
Copy file name to clipboardExpand all lines: articles/digital-twins/how-to-create-azure-function.md
+4-143Lines changed: 4 additions & 143 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ titleSuffix: Azure Digital Twins
5
5
description: See how to create a function in Azure that can access and be triggered by digital twins.
6
6
author: baanders
7
7
ms.author: baanders # Microsoft employees only
8
-
ms.date: 8/27/2020
8
+
ms.date: 7/14/2021
9
9
ms.topic: how-to
10
10
ms.service: digital-twins
11
11
@@ -35,21 +35,7 @@ This article shows you how to create a function in Azure for use with Azure Digi
35
35
36
36
## Create a function app in Visual Studio
37
37
38
-
In Visual Studio 2019, select **File** > **New** > **Project**. Search for the **Azure Functions** template. Select **Next**.
39
-
40
-
:::image type="content" source="media/how-to-create-azure-function/create-azure-function-project.png" alt-text="Screenshot of Visual Studio showing the new project dialog. The Azure Functions project template is highlighted.":::
41
-
42
-
Specify a name for the function app and then select __Create__.
43
-
44
-
:::image type="content" source="media/how-to-create-azure-function/configure-new-project.png" alt-text="Screenshot of Visual Studio showing the dialog to configure a new project, including project name, location, and the choice to create a new solution.":::
45
-
46
-
Select the function app type **Event Grid trigger** and then select __Create__.
47
-
48
-
:::image type="content" source="media/how-to-create-azure-function/event-grid-trigger-function.png" alt-text="Screenshot of Visual Studio showing the dialog to create a new Azure Functions application. The Event Grid trigger option is highlighted.":::
49
-
50
-
After your function app is created, Visual Studio generates a code sample in a *Function1.cs* file in your project folder. This short function is used to log events.
51
-
52
-
:::image type="content" source="media/how-to-create-azure-function/visual-studio-sample-code.png" alt-text="Screenshot of Visual Studio. The project window for the new project is shown. Code for a sample function is shown in a file called Function1." lightbox="media/how-to-create-azure-function/visual-studio-sample-code.png":::
38
+
For instructions on how to create a function app using Visual Studio, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#publish-to-azure).
53
39
54
40
## Write a function that has an Event Grid trigger
55
41
@@ -94,7 +80,7 @@ Now that your application is written, you can publish it to Azure.
For instructions on how to publish a function app, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#publish-to-azure).
98
84
99
85
### Verify the publication of your function
100
86
@@ -114,132 +100,7 @@ To access Azure Digital Twins, your function app needs a system-managed identity
114
100
115
101
## Set up security access for the function app
116
102
117
-
You can set up security access for the function app by using either the Azure CLI or the Azure portal. Follow the steps for your preferred option.
118
-
119
-
# [CLI](#tab/cli)
120
-
121
-
Run these commands in [Azure Cloud Shell](https://shell.azure.com) or a [local Azure CLI installation](/cli/azure/install-azure-cli).
122
-
You can use the function app's system-managed identity to give it the **Azure Digital Twins Data Owner** role for your Azure Digital Twins instance. The role gives the function app permission in the instance to perform data plane activities. Then make the URL of the instance accessible to your function by setting an environment variable.
The function skeleton in earlier examples requires a bearer token to be passed to it. If the bearer token isn't passed, the function app can't authenticate with Azure Digital Twins.
129
-
130
-
To make sure the bearer token is passed, set up [managed identities](../active-directory/managed-identities-azure-resources/overview.md) permissions so the function app can access Azure Digital Twins. You set up these permissions only once for each function app.
131
-
132
-
133
-
1. Use the following command to see the details of the system-managed identity for the function. Take note of the `principalId` field in the output.
134
-
135
-
```azurecli-interactive
136
-
az functionapp identity show --resource-group <your-resource-group> --name <your-App-Service-function-app-name>
137
-
```
138
-
139
-
>[!NOTE]
140
-
> If the result is empty instead of showing identity details, create a new system-managed identity for the function by using this command:
> The output displays details of the identity, including the `principalId` value required for the next step.
147
-
148
-
1. Use the `principalId` value in the following command to assign the function app's identity to the _Azure Digital Twins Data Owner_ role for your Azure Digital Twins instance.
149
-
150
-
```azurecli-interactive
151
-
az dt role-assignment create --dt-name <your-Azure-Digital-Twins-instance> --assignee "<principal-ID>" --role "Azure Digital Twins Data Owner"
152
-
```
153
-
154
-
### Configure application settings
155
-
156
-
Make the URL of your instance accessible to your function by setting an environment variable for it. For more information about environment variables, see [Manage your function app](../azure-functions/functions-how-to-use-azure-function-app-settings.md?tabs=portal).
157
-
158
-
> [!TIP]
159
-
> The Azure Digital Twins instance's URL is made by adding *https://* to the beginning of your instance's host name. To see the host name, along with all the properties of your instance, run `az dt show --dt-name <your-Azure-Digital-Twins-instance>`.
160
-
161
-
```azurecli-interactive
162
-
az functionapp config appsettings set --resource-group <your-resource-group> --name <your-App-Service-function-app-name> --settings "ADT_SERVICE_URL=https://<your-Azure-Digital-Twins-instance-host-name>"
163
-
```
164
-
165
-
# [Azure portal](#tab/portal)
166
-
167
-
Complete the following steps in the [Azure portal](https://portal.azure.com/).
A system-assigned managed identity enables Azure resources to authenticate to cloud services (for example, Azure Key Vault) without storing credentials in code. After you enable system-assigned managed identity, all necessary permissions can be granted through Azure role-based access control.
174
-
175
-
The lifecycle of this type of managed identity is tied to the lifecycle of this resource. Additionally, each resource can have only one system-assigned managed identity.
176
-
177
-
1. In the [Azure portal](https://portal.azure.com/), search for your function app by typing its name in the search box. Select your app from the results.
178
-
179
-
:::image type="content" source="media/how-to-create-azure-function/portal-search-for-function-app.png" alt-text="Screenshot of the Azure portal. The function app's name is in the portal search bar, and the search result is highlighted.":::
180
-
181
-
1. On the function app page, in the menu on the left, select __Identity__ to work with a managed identity for the function. On the __System assigned__ page, verify that the __Status__ is set to **On**. If it's not, set it now and then **Save** the change.
182
-
183
-
:::image type="content" source="media/how-to-create-azure-function/verify-system-managed-identity.png" alt-text="Screenshot of the Azure portal. On the Identity page for the function app, the Status option is set to On." lightbox="media/how-to-create-azure-function/verify-system-managed-identity.png":::
184
-
185
-
1. Select __Azure role assignments__.
186
-
187
-
:::image type="content" source="media/how-to-create-azure-function/add-role-assignment-1.png" alt-text="Screenshot of the Azure portal. On the Azure Function's Identity page, under Permissions, the button Azure role assignments is highlighted." lightbox="media/how-to-create-azure-function/add-role-assignment-1.png":::
188
-
189
-
Select __+ Add role assignment (Preview)__.
190
-
191
-
:::image type="content" source="media/how-to-create-azure-function/add-role-assignment-2.png" alt-text="Screenshot of the Azure portal. On the Azure role assignments page, the button Add role assignment (Preview) is highlighted." lightbox="media/how-to-create-azure-function/add-role-assignment-2.png":::
192
-
193
-
1. On the __Add role assignment (Preview)__ page, select the following values:
194
-
195
-
***Scope**: _Resource group_
196
-
***Subscription**: Select your Azure subscription.
197
-
***Resource group**: Select your resource group.
198
-
***Role**: _Azure Digital Twins Data Owner_
199
-
200
-
Save the details by selecting __Save__.
201
-
202
-
:::image type="content" source="media/how-to-create-azure-function/add-role-assignment-3.png" alt-text="Screenshot of the Azure portal, showing how to add a new role assignment. The dialog shows fields for Scope, Subscription, Resource group, and Role.":::
203
-
204
-
### Configure application settings
205
-
206
-
To make the URL of your Azure Digital Twins instance accessible to your function, you can set an environment variable. Application settings are exposed as environment variables to allow access to the Azure Digital Twins instance. For more information about environment variables, see [Manage your function app](../azure-functions/functions-how-to-use-azure-function-app-settings.md?tabs=portal).
207
-
208
-
To set an environment variable with the URL of your instance, first find your instance's host name:
209
-
210
-
1. Search for your instance in the [Azure portal](https://portal.azure.com).
211
-
1. In the menu on the left, select __Overview__.
212
-
1. Copy the __Host name__ value.
213
-
214
-
:::image type="content" source="media/how-to-create-azure-function/instance-host-name.png" alt-text="Screenshot of the Azure portal. On the instance's Overview page, the host name value is highlighted.":::
215
-
216
-
You can now create an application setting:
217
-
218
-
1. In the portal search bar, search for your function app and then select it from the results.
219
-
220
-
:::image type="content" source="media/how-to-create-azure-function/portal-search-for-function-app.png" alt-text="Screenshot of the Azure portal. The function app's name is being searched in the portal search bar. The search result is highlighted.":::
221
-
222
-
1. On the left, select __Configuration__. Then on the __Application settings__ tab, select __+ New application setting__.
223
-
224
-
:::image type="content" source="media/how-to-create-azure-function/application-setting.png" alt-text="Screenshot of the Azure portal. On the Configuration tab for the function app, the button to create a New application setting is highlighted.":::
225
-
226
-
1. In the window that opens, use the host name value you copied to create an application setting.
:::image type="content" source="media/how-to-create-azure-function/add-application-setting.png" alt-text="Screenshot of the Azure portal. On the Add/Edit application setting page, the Name and Value fields are filled out. The O K button is highlighted.":::
233
-
234
-
1. After you create the setting, it should appear on the __Application settings__ tab. Verify that **ADT_SERVICE_URL** appears on the list. Then save the new application setting by selecting __Save__.
235
-
236
-
:::image type="content" source="media/how-to-create-azure-function/application-setting-save-details.png" alt-text="Screenshot of the Azure portal. On the application settings tab, the new A D T SERVICE URL setting and the Save button are both highlighted.":::
237
-
238
-
1. Any changes to the application settings require an application restart, so select __Continue__ to restart your application when prompted.
239
-
240
-
:::image type="content" source="media/how-to-create-azure-function/save-application-setting.png" alt-text="Screenshot of the Azure portal. A note states that any changes to application settings will restart your application.":::
Copy file name to clipboardExpand all lines: articles/digital-twins/how-to-ingest-iot-hub-data.md
+12-18Lines changed: 12 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -81,32 +81,26 @@ When the twin is created successfully, the CLI output from the command should lo
81
81
82
82
In this section, you'll create an Azure function to access Azure Digital Twins and update twins based on IoT telemetry events that it receives. Follow the steps below to create and publish the function.
83
83
84
-
#### Step 1: Create a function app project
84
+
1. First, create a new function app project in Visual Studio. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#create-an-azure-functions-project).
85
85
86
-
First, create a new function app project in Visual Studio. For instructions on how to do this, see the [Create a function app in Visual Studio](how-to-create-azure-function.md#create-a-function-app-in-visual-studio) section of the *How-to: Set up a function for processing data* article.
3. Rename the *Function1.cs* sample function that Visual Studio has generated to *IoTHubtoTwins.cs*. Replace the code in the file with the following code:
Rename the *Function1.cs* sample function that Visual Studio has generated to *IoTHubtoTwins.cs*. Replace the code in the file with the following code:
4. Publish the project with the *IoTHubtoTwins.cs* function to a function app in Azure. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#publish-to-azure).
98
98
99
-
Save your function code.
99
+
### Configure the function app
100
100
101
-
#### Step 3: Publish the function app to Azure
101
+
Next, **assign an access role** for the function and **configure the application settings** so that it can access your Azure Digital Twins instance.
102
102
103
-
Publish the project with *IoTHubtoTwins.cs* function to a function app in Azure.
104
-
105
-
For instructions on how to do this, see the section [Publish the function app to Azure](how-to-create-azure-function.md#publish-the-function-app-to-azure) of the *How-to: Set up a function for processing data* article.
106
-
107
-
#### Step 4: Configure the function app
108
-
109
-
Next, **assign an access role** for the function and **configure the application settings** so that it can access your Azure Digital Twins instance. For instructions on how to do this, see the section [Set up security access for the function app](how-to-create-azure-function.md#set-up-security-access-for-the-function-app) of the *How-to: Set up a function for processing data* article.
Copy file name to clipboardExpand all lines: articles/digital-twins/how-to-ingest-opcua-data.md
+18-24Lines changed: 18 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -358,23 +358,18 @@ Next, create a [shared access signature for the container](../storage/common/sto
358
358
359
359
In this section, you'll publish an Azure function that you downloaded in [Prerequisites](#prerequisites) that will process the OPC UA data and update Azure Digital Twins.
360
360
361
-
#### Step 1: Open the function in Visual Studio
361
+
1. Navigate to the downloaded [OPC UA to Azure Digital Twins](https://github.com/Azure-Samples/opcua-to-azure-digital-twins) project on your local machine, and into the *Azure Functions/OPCUAFunctions* folder. Open the **OPCUAFunctions.sln** solution in Visual Studio.
362
+
2. Publish the project to a function app in Azure. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#publish-to-azure).
362
363
363
-
Navigate to the downloaded [OPC UA to Azure Digital Twins](https://github.com/Azure-Samples/opcua-to-azure-digital-twins) project on your local machine, and into the *Azure Functions/OPCUAFunctions* folder. Open the **OPCUAFunctions.sln** solution in Visual Studio.
364
+
#### Configure the function app
364
365
365
-
#### Step 2: Publish the function
366
+
Next, **assign an access role** for the function and **configure the application settings** so that it can access your Azure Digital Twins instance.
366
367
367
-
Publish the function project to a functionapp in Azure.
For instructions on how to do this, see the section [Publish the function app to Azure](how-to-create-azure-function.md#publish-the-function-app-to-azure) of the *How-to: Set up a function for processing data* article.
370
+
#### Add application settings
370
371
371
-
#### Step 3: Configure the function app
372
-
373
-
**Assign an access role** for the function and **configure the application settings** so that it can access your Azure Digital Twins instance. For instructions on how to do this, see the section [Set up security access for the function app](how-to-create-azure-function.md#set-up-security-access-for-the-function-app) of the *How-to: Set up a function for processing data* article.
374
-
375
-
#### Step 4: Add application settings
376
-
377
-
You'll also need to add some application settings to fully set up your environment. Go to the [Azure portal](https://portal.azure.com) and navigate to your newly created Azure function by searching for its name in the portal search bar.
372
+
You'll also need to add some application settings to fully set up your environment and the Azure function. Go to the [Azure portal](https://portal.azure.com) and navigate to your newly created Azure function by searching for its name in the portal search bar.
378
373
379
374
Select Configuration from the function's left navigation menu. Use the **+ New application setting** button to start creating new settings.
380
375
@@ -395,7 +390,7 @@ There are three application settings you need to create:
395
390
396
391
### Create event subscription
397
392
398
-
Lastly, create an event subscription to connect your function app and ProcessOPCPublisherEventsToADT function to your IoT Hub. The event subscription is needed so that data can flow from the gateway device into IoT Hub through the function, which then updates Azure Digital Twins.
393
+
Lastly, create an event subscription to connect your function app and *ProcessOPCPublisherEventsToADT* function to your IoT Hub. The event subscription is needed so that data can flow from the gateway device into IoT Hub through the function, which then updates Azure Digital Twins.
399
394
400
395
For instructions, follow the same steps used in [Connect the IoT hub to the Azure function](tutorial-end-to-end.md#connect-the-iot-hub-to-the-azure-function) from the Azure Digital Twins *Tutorial: Connect an end-to-end solution*.
401
396
@@ -405,9 +400,18 @@ The event subscription will have an Endpoint type of **Azure function**, and an
405
400
406
401
After this step, all required components should be installed and running. Data should be flowing from your OPC UA Simulation Server, through Azure IoT Hub, and into your Azure Digital Twins instance.
407
402
403
+
### Verify completion
404
+
405
+
In this section, you set up an Azure function to connect the OPC UA data to Azure Digital Twins. Verify that you've completed the following checklist:
406
+
> [!div class="checklist"]
407
+
> * Created and imported *opcua-mapping.json* file into a blob storage container.
408
+
> * Published the sample function *ProcessOPCPublisherEventsToADT* to a function app in Azure.
409
+
> * Added three new application settings to the Azure Functions app.
410
+
> * Created an event subscription to send IoT Hub events to the function app.
411
+
408
412
The next section provides some Azure CLI commands that you can run to monitor the events and verify everything is working successfully.
409
413
410
-
###Verify and monitor
414
+
## Verify and monitor
411
415
412
416
The commands in this section can be run in the [Azure Cloud Shell](https://shell.azure.com), or in a [local Azure CLI window](/cli/azure/install-azure-cli).
413
417
@@ -425,16 +429,6 @@ Finally, you can use Azure Digital Twins Explorer to manually monitor twin prope
425
429
426
430
:::image type="content" source="media/how-to-ingest-opcua-data/adt-explorer-2.png" alt-text="Screenshot of using azure digital twins explorer to monitor twin property updates":::
427
431
428
-
### Verify completion
429
-
430
-
In this section, you set up an Azure function to connect the OPC UA data to Azure Digital Twins. Verify that you've completed the following checklist:
431
-
> [!div class="checklist"]
432
-
> * Created and imported *opcua-mapping.json* file into a blob storage container.
433
-
> * Published the sample function ProcessOPCPublisherEventsToADT to a function app in Azure.
434
-
> * Added three new application settings to the Azure Functions app.
435
-
> * Created an event subscription to send IoT Hub events to the function app.
436
-
> * Used Azure CLI commands to verify the final data flow
437
-
438
432
## Next steps
439
433
440
434
In this article, you set up a full data flow for getting simulated OPC UA Server data into Azure Digital Twins, where it updates a property on a digital twin.
Copy file name to clipboardExpand all lines: articles/digital-twins/how-to-integrate-azure-signalr.md
+6-4Lines changed: 6 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ In this section, you will set up two Azure functions:
66
66
67
67
Start Visual Studio (or another code editor of your choice), and open the code solution in the *digital-twins-samples-master > ADTSampleApp* folder. Then do the following steps to create the functions:
68
68
69
-
1. In the *SampleFunctionsApp* project, create a new C# class called **SignalRFunctions.cs**.
69
+
1. In the *SampleFunctionsApp* project, create a new C# class called **SignalRFunctions.cs**. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#add-a-function-to-your-project).
70
70
71
71
1. Replace the contents of the class file with the following code:
72
72
@@ -79,9 +79,11 @@ Start Visual Studio (or another code editor of your choice), and open the code s
79
79
80
80
This should resolve any dependency issues in the class.
81
81
82
-
1. Publish your function to Azure, using the steps described in the [Publish the app section](tutorial-end-to-end.md#publish-the-app) of the *Connect an end-to-end solution* tutorial. You can publish it to the same app service/function app that you used in the end-to-end tutorial [prerequisite](#prerequisites), or create a new one—but you may want to use the same one to minimize duplication.
82
+
1. Publish your function to Azure. You can publish it to the same app service/function app that you used in the end-to-end tutorial [prerequisite](#prerequisites), or create a new one—but you may want to use the same one to minimize duplication. For instructions on how to publish a function using Visual Studio, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#publish-to-azure).
83
83
84
-
Next, configure the functions to communicate with your Azure SignalR instance. You'll start by gathering the SignalR instance's **connection string**, and then add it to the functions app's settings.
84
+
### Configure the function
85
+
86
+
Next, configure the function to communicate with your Azure SignalR instance. You'll start by gathering the SignalR instance's **connection string**, and then add it to the functions app's settings.
85
87
86
88
1. Go to the [Azure portal](https://portal.azure.com/) and search for the name of your SignalR instance in the search bar at the top of the portal. Select the instance to open it.
87
89
1. Select **Keys** from the instance menu to view the connection strings for the SignalR service instance.
@@ -99,7 +101,7 @@ Next, configure the functions to communicate with your Azure SignalR instance. Y
99
101
100
102
:::image type="content" source="media/how-to-integrate-azure-signalr/output-app-setting.png" alt-text="Screenshot of the output in a command window, showing a list item called 'AzureSignalRConnectionString'.":::
101
103
102
-
#### Connect the function to Event Grid
104
+
## Connect the function to Event Grid
103
105
104
106
Next, subscribe the *broadcast* Azure function to the **event grid topic** you created during the [tutorial prerequisite](how-to-integrate-azure-signalr.md#prerequisites). This will allow telemetry data to flow from the thermostat67 twin through the event grid topic and to the function. From here, the function can broadcast the data to all the clients.
Copy file name to clipboardExpand all lines: articles/digital-twins/how-to-provision-using-device-provisioning-service.md
+14-26Lines changed: 14 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -88,25 +88,18 @@ Inside your function app project that you created in the [Prerequisites section]
88
88
89
89
Start by opening the function app project in Visual Studio on your machine and follow the steps below.
90
90
91
-
#### Step 1: Add a new function
91
+
1. First, create a new function of type *HTTP-trigger* in the function app project in Visual Studio. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#add-a-function-to-your-project).
92
92
93
-
Add a new function of type *HTTP-trigger*to the function app project in Visual Studio.
93
+
2.Add a new NuGet package to the project: [Microsoft.Azure.Devices.Provisioning.Service](https://www.nuget.org/packages/Microsoft.Azure.Devices.Provisioning.Service/). You might need to add more packages to your project as well, if the packages used in the code aren't part of the project already.
94
94
95
-
:::image type="content" source="media/how-to-provision-using-device-provisioning-service/add-http-trigger-function-visual-studio.png" alt-text="Screenshot of the Visual Studio view to add Azure function of type Http Trigger to a function app project." lightbox="media/how-to-provision-using-device-provisioning-service/add-http-trigger-function-visual-studio.png":::
95
+
3. In the newly created function code file, paste in the following code, rename the function to *DpsAdtAllocationFunc.cs*, and save the file.
Add a new NuGet package to the project: [Microsoft.Azure.Devices.Provisioning.Service](https://www.nuget.org/packages/Microsoft.Azure.Devices.Provisioning.Service/). You might need to add more packages to your project as well, if the packages used in the code aren't part of the project already.
99
+
4. Publish the project with the *DpsAdtAllocationFunc.cs* function to a function app in Azure. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#publish-to-azure).
100
100
101
-
In the newly created function code file, paste in the following code, rename the function to *DpsAdtAllocationFunc.cs*, and save the file.
> When creating the function app for the first time in the [Prerequisites section](#prerequisites), you may have already assigned an access role for the function and configured the application settings for it to access your Azure Digital Twins instance. These need to be done once for the entire function app, so verify they've been completed in your app before continuing. You can find instructions in the [Set up security access for the function app](how-to-create-azure-function.md#set-up-security-access-for-the-function-app) section of the *How-to: Set up a function in Azure to process data* article.
110
103
111
104
### Create Device Provisioning enrollment
112
105
@@ -249,23 +242,18 @@ For more about lifecycle events, see [IoT Hub Non-telemetry events](../iot-hub/i
249
242
250
243
Start by opening the function app project in Visual Studio on your machine and follow the steps below.
251
244
252
-
#### Step 1: Add a new function
253
-
254
-
Add a new function of type *Event Hub Trigger* to the function app project in Visual Studio.
255
-
256
-
:::image type="content" source="media/how-to-provision-using-device-provisioning-service/create-event-hub-trigger-function.png" alt-text="Screenshot of the Visual Studio window showing how to add an Azure function of type Event Hub Trigger in a function app project." lightbox="media/how-to-provision-using-device-provisioning-service/create-event-hub-trigger-function.png":::
245
+
1. First, create a new function of type *Event Hub Trigger* in the function app project in Visual Studio. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#add-a-function-to-your-project).
257
246
258
-
#### Step 2: Fill in function code
247
+
2. Add a new NuGet package to the project: [Microsoft.Azure.Devices.Provisioning.Service](https://www.nuget.org/packages/Microsoft.Azure.Devices.Provisioning.Service/). You might need to add more packages to your project as well, if the packages used in the code aren't part of the project already.
259
248
260
-
In the newly created function code file, paste in the following code, rename the function to `DeleteDeviceInTwinFunc.cs`, and save the file.
249
+
3. In the newly created function code file, paste in the following code, rename the function to *DeleteDeviceInTwinFunc.cs*, and save the file.
4. Publish the project with the *DeleteDeviceInTwinFunc.cs* function to a function app in Azure. For instructions on how to do this, see [Develop Azure Functions using Visual Studio](../azure-functions/functions-develop-vs.md#publish-to-azure).
265
254
266
-
Publish the project with *DeleteDeviceInTwinFunc.cs* function to the function app in Azure.
> When creating the function app for the first time in the [Prerequisites section](#prerequisites), you may have already assigned an access role for the function and configured the application settings for it to access your Azure Digital Twins instance. These need to be done once for the entire function app, so verify they've been completed in your app before continuing. You can find instructions in the [Set up security access for the function app](how-to-create-azure-function.md#set-up-security-access-for-the-function-app) section of the *How-to: Set up a function in Azure to process data* article.
description: include file describing how to configure an Azure function to work with Azure Digital Twins
4
+
ms.service: digital-twins
5
+
ms.topic: include
6
+
ms.date: 7/14/2021
7
+
ms.author: baanders
8
+
---
9
+
10
+
You can set up security access for the function app by using either the Azure CLI or the Azure portal. Follow the steps for your preferred option.
11
+
12
+
# [CLI](#tab/cli)
13
+
14
+
Run these commands in [Azure Cloud Shell](https://shell.azure.com) or a [local Azure CLI installation](/cli/azure/install-azure-cli).
15
+
You can use the function app's system-managed identity to give it the **Azure Digital Twins Data Owner** role for your Azure Digital Twins instance. The role gives the function app permission in the instance to perform data plane activities. Then make the URL of the instance accessible to your function by setting an environment variable.
The Azure function requires a bearer token to be passed to it. If the bearer token isn't passed, the function app can't authenticate with Azure Digital Twins.
22
+
23
+
To make sure the bearer token is passed, set up [managed identities](../articles/active-directory/managed-identities-azure-resources/overview.md) permissions so the function app can access Azure Digital Twins. You only need to set up these permissions once for each function app.
24
+
25
+
26
+
1. Use the following command to see the details of the system-managed identity for the function. Take note of the `principalId` field in the output.
27
+
28
+
```azurecli-interactive
29
+
az functionapp identity show --resource-group <your-resource-group> --name <your-App-Service-function-app-name>
30
+
```
31
+
32
+
>[!NOTE]
33
+
> If the result is empty instead of showing identity details, create a new system-managed identity for the function by using this command:
> The output displays details of the identity, including the `principalId` value required for the next step.
40
+
41
+
1. Use the `principalId` value in the following command to assign the function app's identity to the _Azure Digital Twins Data Owner_ role for your Azure Digital Twins instance.
42
+
43
+
```azurecli-interactive
44
+
az dt role-assignment create --dt-name <your-Azure-Digital-Twins-instance> --assignee "<principal-ID>" --role "Azure Digital Twins Data Owner"
45
+
```
46
+
47
+
### Configure application settings
48
+
49
+
Make the URL of your instance accessible to your function by setting an environment variable for it. For more information about environment variables, see [Manage your function app](../articles/azure-functions/functions-how-to-use-azure-function-app-settings.md?tabs=portal).
50
+
51
+
> [!TIP]
52
+
> The Azure Digital Twins instance's URL is made by adding *https://* to the beginning of your instance's host name. To see the host name, along with all the properties of your instance, run `az dt show --dt-name <your-Azure-Digital-Twins-instance>`.
53
+
54
+
```azurecli-interactive
55
+
az functionapp config appsettings set --resource-group <your-resource-group> --name <your-App-Service-function-app-name> --settings "ADT_SERVICE_URL=https://<your-Azure-Digital-Twins-instance-host-name>"
56
+
```
57
+
58
+
# [Azure portal](#tab/portal)
59
+
60
+
Complete the following steps in the [Azure portal](https://portal.azure.com/).
A system-assigned managed identity enables Azure resources to authenticate to cloud services (for example, Azure Key Vault) without storing credentials in code. After you enable system-assigned managed identity, all necessary permissions can be granted through Azure role-based access control.
67
+
68
+
The lifecycle of this type of managed identity is tied to the lifecycle of this resource. Additionally, each resource can have only one system-assigned managed identity.
69
+
70
+
1. In the [Azure portal](https://portal.azure.com/), search for your function app by typing its name in the search box. Select your app from the results.
71
+
72
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/portal-search-for-function-app.png" alt-text="Screenshot of the Azure portal. The function app's name is in the portal search bar, and the search result is highlighted.":::
73
+
74
+
1. On the function app page, in the menu on the left, select __Identity__ to work with a managed identity for the function. On the __System assigned__ page, verify that the __Status__ is set to **On**. If it's not, set it now and then **Save** the change.
75
+
76
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/verify-system-managed-identity.png" alt-text="Screenshot of the Azure portal. On the Identity page for the function app, the Status option is set to On." lightbox="../articles/digital-twins/media/how-to-create-azure-function/verify-system-managed-identity.png":::
77
+
78
+
1. Select __Azure role assignments__.
79
+
80
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/add-role-assignment-1.png" alt-text="Screenshot of the Azure portal. On the Azure Function's Identity page, under Permissions, the button Azure role assignments is highlighted." lightbox="../articles/digital-twins/media/how-to-create-azure-function/add-role-assignment-1.png":::
81
+
82
+
Select __+ Add role assignment (Preview)__.
83
+
84
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/add-role-assignment-2.png" alt-text="Screenshot of the Azure portal. On the Azure role assignments page, the button Add role assignment (Preview) is highlighted." lightbox="../articles/digital-twins/media/how-to-create-azure-function/add-role-assignment-2.png":::
85
+
86
+
1. On the __Add role assignment (Preview)__ page, select the following values:
87
+
88
+
***Scope**: _Resource group_
89
+
***Subscription**: Select your Azure subscription.
90
+
***Resource group**: Select your resource group.
91
+
***Role**: _Azure Digital Twins Data Owner_
92
+
93
+
Save the details by selecting __Save__.
94
+
95
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/add-role-assignment-3.png" alt-text="Screenshot of the Azure portal, showing how to add a new role assignment. The dialog shows fields for Scope, Subscription, Resource group, and Role.":::
96
+
97
+
### Configure application settings
98
+
99
+
To make the URL of your Azure Digital Twins instance accessible to your function, you can set an environment variable. Application settings are exposed as environment variables to allow access to the Azure Digital Twins instance. For more information about environment variables, see [Manage your function app](../articles/azure-functions/functions-how-to-use-azure-function-app-settings.md?tabs=portal).
100
+
101
+
To set an environment variable with the URL of your instance, first find your instance's host name:
102
+
103
+
1. Search for your instance in the [Azure portal](https://portal.azure.com).
104
+
1. In the menu on the left, select __Overview__.
105
+
1. Copy the __Host name__ value.
106
+
107
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/instance-host-name.png" alt-text="Screenshot of the Azure portal. On the instance's Overview page, the host name value is highlighted.":::
108
+
109
+
You can now create an application setting:
110
+
111
+
1. In the portal search bar, search for your function app and then select it from the results.
112
+
113
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/portal-search-for-function-app.png" alt-text="Screenshot of the Azure portal. The function app's name is being searched in the portal search bar. The search result is highlighted.":::
114
+
115
+
1. On the left, select __Configuration__. Then on the __Application settings__ tab, select __+ New application setting__.
116
+
117
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/application-setting.png" alt-text="Screenshot of the Azure portal. On the Configuration tab for the function app, the button to create a New application setting is highlighted.":::
118
+
119
+
1. In the window that opens, use the host name value you copied to create an application setting.
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/add-application-setting.png" alt-text="Screenshot of the Azure portal. On the Add/Edit application setting page, the Name and Value fields are filled out. The O K button is highlighted.":::
126
+
127
+
1. After you create the setting, it should appear on the __Application settings__ tab. Verify that **ADT_SERVICE_URL** appears on the list. Then save the new application setting by selecting __Save__.
128
+
129
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/application-setting-save-details.png" alt-text="Screenshot of the Azure portal. On the application settings tab, the new A D T SERVICE URL setting and the Save button are both highlighted.":::
130
+
131
+
1. Any changes to the application settings require an application restart, so select __Continue__ to restart your application when prompted.
132
+
133
+
:::image type="content" source="../articles/digital-twins/media/how-to-create-azure-function/save-application-setting.png" alt-text="Screenshot of the Azure portal. A note states that any changes to application settings will restart your application.":::
0 commit comments