Get Cognitive Services Keys and Endpoint in an ARM Template

For a demo Azure Logic App I need to setup an Azure Cognitive Services account for the Computer Vision API.
I set up the resources in Azure using an ARM template so that everything is created in a reproducible way.
My logic app needs a connection to the Cognitive Services, which consists in an API Key and an endpoint.

I was not able to find a template in the Azure Quickstart Templates repo to get this information, so I find it out myself.

Here is an ARM Template where in the outputs section it will display the Keys and the Endpoint (at the end of the template):

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {
    "name":  "Test",
    "cognitiveservicesid": "[concat(resourceGroup().id,'/providers/','Microsoft.CognitiveServices/accounts/', variables('name'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Web/connections",
      "apiVersion": "2016-06-01",
      "name": "[variables('name')]",
      "location": "westeurope",
      "properties": {
        "api": {
          "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', 'westeurope', '/managedApis/', 'cognitiveservicescomputervision')]"
        },
        "displayName": "[variables('name')]",
        "parameterValues": {
          "siteUrl": "[reference(variables('cognitiveservicesid'),'2016-02-01-preview').endpoint]",
          "apiKey": "[listKeys(variables('cognitiveservicesid'),'2016-02-01-preview').key1]"
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.CognitiveServices/accounts', variables('name'))]"
      ]
    },
    {
      "type": "Microsoft.CognitiveServices/accounts",
      "sku": {
        "name": "S1"
      },
      "kind": "ComputerVision",
      "name": "[variables('name')]",
      "apiVersion": "2016-02-01-preview",
      "location": "westeurope",
      "scale": null,
      "properties": {},
      "dependsOn": []
    }
  ],
  "outputs": {
    "cognitivekeys": {
      "type": "object",
      "value": "[listKeys(variables('cognitiveservicesid'),'2016-02-01-preview')]"
    },
    "cognitivekey1": {
      "type": "string",
      "value": "[listKeys(variables('cognitiveservicesid'),'2016-02-01-preview').key1]"
    },
    "cognitivekey2": {
      "type": "string",
      "value": "[listKeys(variables('cognitiveservicesid'),'2016-02-01-preview').key2]"
    },
    "endpoint": {
      "type": "string",
      "value": "[reference(variables('cognitiveservicesid'),'2016-02-01-preview').endpoint]"
    }
  }
}

The template also creates a resource¬†“Microsoft.Web/connections” where I use the Key1 and Endpoint from the Cognitive Service Resource.

I have created a GitHub repo where you can see how it works.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: