Deploying an Azure Recommendation Service Using an ARM Template

August 06, 2017

Azure provides a number of AI services out of the box. The recommendation service is one of these, and it’s part of the Azure Cognitive Services.

Why?

Deploying a new service to Azure is quite straightforward; for recommendations, you Navigate to the Portal and select a new service:

Azure Rec

Then you select the various options one by one, and finally, you create the resource.

Azure Rec 2

But, what if you want to create this in development, and then in test, and then again in production, or what if you want to deploy it again multiple times? Although it’s straightforward, putting data in this kind of form is prone to error - and it’s time consuming.

ARM Templates

Azure allows you to create a template, and to create your resource based on that. There are a number of ways to do this; ultimately, it’s just a JSON document, so you could open up notepad and just type one.

Here’s how I created it initially:

Azure Rec 3

Create a new resource group:

Azure Rec 4

However, this doesn’t seem to give you too much out of the box (there are templates, but recommendations isn’t one of them):

Azure Rec 5

Fortunately, you can reverse engineer the deployment that you’ve already made:

Azure Rec 6 1

Downloading this gives you everything you need to re-deploy:

Azure Rec 7

Running the template

So, now you’ve got a JSON file, how do you tell Azure what to do? Powershell seems to be the answer of choice (as it is for so many things these days) for Microsoft.

You’ll need to change the execution policy first:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted

Azure Rec 8

Then run the script:

Azure Rec 9

Azure Rec 10 1

Success

When it works, you’ll get something like this:

Azure Rec 11

And here’s the service:

Azure Rec 12

Errors

It would be a gross exaggeration to say this worked straight away for me; here’s the errors that I encountered, and how I resolved them.

Resource Group Name is null

New-AzureRmResourceGroupDeployment : 18:23:28 - Error: Code=InvalidDeploymentParameterValue; Message=The value of deployment parameter ‘accounts_TestRecommendations_name’ is null. Please specify the value or use the parameter reference. See https://aka.ms/arm-deploy/#parameter-file for details. At C:\Users\Paul\Downloads\ExportedTemplate-pcm-dev\deploy.ps1:104 char:5

  • New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGr ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
    • FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

Resolution

This is caused because the parameter is set to null by default. Change parameters.json:



{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "accounts\_TestRecommendations\_name": {
            "value": "testRecommendations1"
        }
    }
}

No connection

Login-AzureRmAccount : The browser based authentication dialog failed to complete. Reason: The server or proxy was not found. At C:\Users\Paul\Downloads\ExportedTemplate-pcm-dev\deploy.ps1:71 char:1

  • Login-AzureRmAccount;
  •   + CategoryInfo          : CloseError: (:) [Add-AzureRmAccount], AadAuthenticationFailedException
      + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.AddAzureRMAccountCommand

Resolution

This is caused by not having a connection to Azure… so the resolution is to connect.

Invalid parameter value

C:\Users\Paul\Downloads\ExportedTemplate-pcm-dev\deploy.ps1 : Cannot retrieve the dynamic parameters for the cmdlet. Error parsing boolean value. Path ‘parameters.accounts_TestRecommendations_name.value’, line 6, position 22. At line:1 char:1

  • .\deploy.ps1
  •   + CategoryInfo          : InvalidArgument: (:) [deploy.ps1], ParameterBindingException
      + FullyQualifiedErrorId : GetDynamicParametersException,deploy.ps1

Resolution

In my first attempt to resolve the first error, I specified a name without quotes; i.e.:



            "value": testRecommendations1

This seems to cause Azure to consider this a boolean; the fix is pretty straightforward once you’ve worked out what it’s actually saying:



            "value": "testRecommendations1"

Error

New-AzureRmResourceGroupDeployment : 07:58:51 - Resource Microsoft.CognitiveServices/accounts ‘testRecommendations1’ failed with message ’{ “error”: { “code”: “CanNotCreateMultipleFreeAccounts”, “message”: “Operation failed. Only one free account is allowed for account type ‘Recommendations’.” } }’ At C:\Users\Paul\Downloads\ExportedTemplate-pcm-dev\deploy.ps1:104 char:5

  • New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGr ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
    • FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

Resolution

This was caused because my account would only allow me to have a single recommendation service at any time. So the fix is to delete existing recommendation account:

Azure Rec 13

References

https://blogs.endjin.com/2015/07/using-azure-resource-manager-and-powershell-dsc-to-create-and-provision-a-vm/

https://blogs.endjin.com/2016/01/azure-resource-manager-authentication-from-a-powershell-script/



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024