| author | baanders |
|---|---|
| description | include file describing how to configure an Azure function to work with Azure Digital Twins - CLI instructions |
| ms.service | azure-digital-twins |
| ms.topic | include |
| ms.date | 03/13/2025 |
| ms.author | baanders |
Run the following commands in Azure Cloud Shell or a local Azure CLI.
[!INCLUDE digital-twins-permissions-required.md]
The Azure function requires a bearer token to be passed to it. To make sure the bearer token is passed, grant the function app the Azure Digital Twins Data Owner role for your Azure Digital Twins instance, which gives the function app permission to perform data plane activities on the instance.
-
Use the following command to create a system-managed identity for your function (if the function already has one, this command prints its details). Take note of the
principalIdfield in the output. You'll use this ID to refer to the function so that you can grant it permissions in the next step.az functionapp identity assign --resource-group <your-resource-group> --name <your-function-app-name> -
Use the
principalIdvalue in the following command to give the function the Azure Digital Twins Data Owner role for your Azure Digital Twins instance.az dt role-assignment create --dt-name <your-Azure-Digital-Twins-instance> --assignee "<principal-ID>" --role "Azure Digital Twins Data Owner"
Next, make the URL of your Azure Digital Twins instance accessible to your function by setting an environment variable for it.
Tip
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>.
The following command sets an environment variable for your instance's URL that your function uses whenever it needs to access the instance.
az functionapp config appsettings set --resource-group <your-resource-group> --name <your-function-app-name> --settings "ADT_SERVICE_URL=https://<your-Azure-Digital-Twins-instance-host-name>"