Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4252f2b

Browse files
committedJul 14, 2021
Reference Az func docs
1 parent 24c66a8 commit 4252f2b

10 files changed

+189
-285
lines changed
 

‎articles/digital-twins/how-to-create-azure-function.md

Lines changed: 4 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ titleSuffix: Azure Digital Twins
55
description: See how to create a function in Azure that can access and be triggered by digital twins.
66
author: baanders
77
ms.author: baanders # Microsoft employees only
8-
ms.date: 8/27/2020
8+
ms.date: 7/14/2021
99
ms.topic: how-to
1010
ms.service: digital-twins
1111

@@ -35,21 +35,7 @@ This article shows you how to create a function in Azure for use with Azure Digi
3535

3636
## Create a function app in Visual Studio
3737

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).
5339

5440
## Write a function that has an Event Grid trigger
5541

@@ -94,7 +80,7 @@ Now that your application is written, you can publish it to Azure.
9480

9581
## Publish the function app to Azure
9682

97-
[!INCLUDE [digital-twins-publish-azure-function.md](../../includes/digital-twins-publish-azure-function.md)]
83+
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).
9884

9985
### Verify the publication of your function
10086

@@ -114,132 +100,7 @@ To access Azure Digital Twins, your function app needs a system-managed identity
114100

115101
## Set up security access for the function app
116102

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.
123-
124-
### Assign an access role
125-
126-
[!INCLUDE [digital-twins-permissions-required.md](../../includes/digital-twins-permissions-required.md)]
127-
128-
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:
141-
>
142-
>```azurecli-interactive
143-
>az functionapp identity assign --resource-group <your-resource-group> --name <your-App-Service-function-app-name>
144-
>```
145-
>
146-
> 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/).
168-
169-
### Assign an access role
170-
171-
[!INCLUDE [digital-twins-permissions-required.md](../../includes/digital-twins-permissions-required.md)]
172-
173-
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.
227-
* **Name**: ADT_SERVICE_URL
228-
* **Value**: https://<your-Azure-Digital-Twins-host-name>
229-
230-
Select __OK__ to create an application setting.
231-
232-
:::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.":::
241-
242-
---
103+
[!INCLUDE [digital-twins-configure-function-app.md](../../includes/digital-twins-configure-function-app.md)]
243104

244105
## Next steps
245106

‎articles/digital-twins/how-to-ingest-iot-hub-data.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,26 @@ When the twin is created successfully, the CLI output from the command should lo
8181

8282
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.
8383

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).
8585

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.
86+
2. Add the following packages to your project:
87+
* [Azure.DigitalTwins.Core](https://www.nuget.org/packages/Azure.DigitalTwins.Core/)
88+
* [Azure.Identity](https://www.nuget.org/packages/Azure.Identity/)
89+
* [Microsoft.Azure.WebJobs.Extensions.EventGrid](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.EventGrid/)
8790

88-
#### Step 2: Fill in function code
91+
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:
8992

90-
Add the following packages to your project:
91-
* [Azure.DigitalTwins.Core](https://www.nuget.org/packages/Azure.DigitalTwins.Core/)
92-
* [Azure.Identity](https://www.nuget.org/packages/Azure.Identity/)
93-
* [Microsoft.Azure.WebJobs.Extensions.EventGrid](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.EventGrid/)
93+
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/IoTHubToTwins.cs":::
9494

95-
Rename the *Function1.cs* sample function that Visual Studio has generated to *IoTHubtoTwins.cs*. Replace the code in the file with the following code:
95+
Save your function code.
9696

97-
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/IoTHubToTwins.cs":::
97+
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).
9898

99-
Save your function code.
99+
### Configure the function app
100100

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.
102102

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.
103+
[!INCLUDE [digital-twins-configure-function-app.md](../../includes/digital-twins-configure-function-app.md)]
110104

111105
## Connect your function to IoT Hub
112106

‎articles/digital-twins/how-to-ingest-opcua-data.md

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -358,23 +358,18 @@ Next, create a [shared access signature for the container](../storage/common/sto
358358

359359
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.
360360

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).
362363

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
364365

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.
366367

367-
Publish the function project to a function app in Azure.
368+
[!INCLUDE [digital-twins-configure-function-app.md](../../includes/digital-twins-configure-function-app.md)]
368369

369-
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
370371

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.
378373

379374
Select Configuration from the function's left navigation menu. Use the **+ New application setting** button to start creating new settings.
380375

@@ -395,7 +390,7 @@ There are three application settings you need to create:
395390
396391
### Create event subscription
397392

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.
399394

400395
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*.
401396

@@ -405,9 +400,18 @@ The event subscription will have an Endpoint type of **Azure function**, and an
405400

406401
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.
407402

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+
408412
The next section provides some Azure CLI commands that you can run to monitor the events and verify everything is working successfully.
409413

410-
### Verify and monitor
414+
## Verify and monitor
411415

412416
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).
413417

@@ -425,16 +429,6 @@ Finally, you can use Azure Digital Twins Explorer to manually monitor twin prope
425429

426430
:::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":::
427431

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-
438432
## Next steps
439433

440434
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.

‎articles/digital-twins/how-to-integrate-azure-signalr.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ In this section, you will set up two Azure functions:
6666

6767
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:
6868

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).
7070

7171
1. Replace the contents of the class file with the following code:
7272

@@ -79,9 +79,11 @@ Start Visual Studio (or another code editor of your choice), and open the code s
7979
8080
This should resolve any dependency issues in the class.
8181
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).
8383
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.
8587
8688
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.
8789
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
99101
100102
:::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'.":::
101103
102-
#### Connect the function to Event Grid
104+
## Connect the function to Event Grid
103105
104106
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.
105107

‎articles/digital-twins/how-to-provision-using-device-provisioning-service.md

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,18 @@ Inside your function app project that you created in the [Prerequisites section]
8888

8989
Start by opening the function app project in Visual Studio on your machine and follow the steps below.
9090

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).
9292

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.
9494

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.
9696

97-
#### Step 2: Fill in function code
97+
:::code language="csharp" source="~/digital-twins-docs-samples-dps/functions/DpsAdtAllocationFunc.cs":::
9898

99-
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).
100100

101-
In the newly created function code file, paste in the following code, rename the function to *DpsAdtAllocationFunc.cs*, and save the file.
102-
103-
:::code language="csharp" source="~/digital-twins-docs-samples-dps/functions/DpsAdtAllocationFunc.cs":::
104-
105-
#### Step 3: Publish the function app to Azure
106-
107-
Publish the project with *DpsAdtAllocationFunc.cs* function to the function app in Azure.
108-
109-
[!INCLUDE [digital-twins-publish-and-configure-function-app.md](../../includes/digital-twins-publish-and-configure-function-app.md)]
101+
> [!IMPORTANT]
102+
> 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.
110103
111104
### Create Device Provisioning enrollment
112105

@@ -249,23 +242,18 @@ For more about lifecycle events, see [IoT Hub Non-telemetry events](../iot-hub/i
249242
250243
Start by opening the function app project in Visual Studio on your machine and follow the steps below.
251244
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).
257246
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.
259248
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.
261250
262-
:::code language="csharp" source="~/digital-twins-docs-samples-dps/functions/DeleteDeviceInTwinFunc.cs":::
251+
:::code language="csharp" source="~/digital-twins-docs-samples-dps/functions/DeleteDeviceInTwinFunc.cs":::
263252
264-
#### Step 3: Publish the function app to Azure
253+
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).
265254
266-
Publish the project with *DeleteDeviceInTwinFunc.cs* function to the function app in Azure.
267-
268-
[!INCLUDE [digital-twins-publish-and-configure-function-app.md](../../includes/digital-twins-publish-and-configure-function-app.md)]
255+
> [!IMPORTANT]
256+
> 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.
269257
270258
### Create an IoT Hub route for lifecycle events
271259
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
author: baanders
3+
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.
16+
17+
### Assign an access role
18+
19+
[!INCLUDE [digital-twins-permissions-required.md](digital-twins-permissions-required.md)]
20+
21+
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:
34+
>
35+
>```azurecli-interactive
36+
>az functionapp identity assign --resource-group <your-resource-group> --name <your-App-Service-function-app-name>
37+
>```
38+
>
39+
> 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/).
61+
62+
### Assign an access role
63+
64+
[!INCLUDE [digital-twins-permissions-required.md](digital-twins-permissions-required.md)]
65+
66+
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.
120+
* **Name**: ADT_SERVICE_URL
121+
* **Value**: https://<your-Azure-Digital-Twins-host-name>
122+
123+
Select __OK__ to create an application setting.
124+
125+
:::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.":::
134+
135+
---

‎includes/digital-twins-publish-and-configure-function-app.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎includes/digital-twins-publish-azure-function.md

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.