Connecting to Dataverse from Function App using Managed Identity – Using azd

I wrote a post about using Functions’s Managed Identity to connect with Dataverse back in Nov, 2021. In that post I used Bicep only. But the technology landscape has advanced since then. I have now rejigged the codebase and used Azure Developer CLI along with Power Platform CLI.

Azure Developer CLI makes it super easy to create/update resources into Azure and deploy Function code as well. You’ll just need to remember three commands.

  1. azd up – Creates/updates resources and deploys application code
  2. azd provision – Only creates/updates resources in Azure
  3. azd deploy – Only deploys application code

The amazing part about azd is that there are hooks. You can run PowerShell code for various events along the deployment. There are 16 hooks available, but in this repo I just had to use pre-deploy, post-deploy and post provision hooks. The hooks themselves get the azd environment variables injected into the context, so you can easily access them inside your PowerShell script.

In order to provision this template and Function code to your tenant you’ll first need to run

azd init -t rajyraman/PowerApps-Managed-Identity-Demo-Functions

This will get all the repo contents to your local machine (sort of like git clone), and then you can run

azd up

azd will now try to grab all the variables that are need for the deployment. The variables are declared in the main.parameters.json file.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "environmentName": {
      "value": "${AZURE_ENV_NAME}"
    },
    "location": {
      "value": "${AZURE_LOCATION}"
    },
    "serviceEndpointStorageLocations": {
      "value": "${SERVICE_ENDPOINT_STORAGE_LOCATIONS}"
    },
    "createVNet": {
      "value": "${CREATE_VNET}"
    },
    "createPrivateLink": {
      "value": "${CREATE_PRIVATE_LINK}"
    },
    "dataverseUrl": {
      "value": "${DATAVERSE_URL}"
    }
  }
}

This is a sample .env file I have as my dev config, so that you know what sort of values are expected.

Screenshot showing .env files for dev environment config in Azure Developer CLI

The repo can be deployed in three possible configurations.

  1. Function App in Consumption plan with no VNet
  2. Function App in Elastic Premium with just VNet and Storage Endpoint
  3. Function App in Elastic Premium with just VNet and Private Link

All these three configs are dictated by the CREATE_VNET and CREATE_PRIVATE_LINK environment variables. Creating the Function App’s Service Principal as an application user and giving it System Administrator role is run as part of post-provision hooks. So, there is no need to do this manually.

I have also tweaked the default workflow that azd pipeline config creates and added GitHub environments into the mix. The additional change this entails is that you’ll have to add the environments into the Federated Credentials.

Screenshot from Azure Portal showing Federated credentials setup by Azure Developer CLI
Screenshot from Azure AD portal showing Add a credentials into Federated Credentials on Application Registration

This means you can now create environment specific variables and secrets in GitHub.

Screenshot from GitHub Secrets and Variables settings showing environment variables for each managed environment,

I hope this post serves as a starting point to deploy Functions that interact with Dataverse.

Repo: https://github.com/rajyraman/PowerApps-Managed-Identity-Demo-Functions

References:

  1. https://github.com/Azure-Samples/function-app-arm-templates/
  2. https://github.com/Azure/azure-dev/discussions/2603
  3. https://aka.ms/azd

1 comments

Leave a comment