Linked ARM templates with _artifactsLocation and _artifactsLocationSasToken

Tanguy Schoubert
Published by Tanguy
Category : Azure / Logic Apps
03/11/2020

Why _artifactsLocation and _artifactsLocationSasToken?

 

As described in the Microsoft documentation, it is possible to use these parameters to reference linked ARM templates. There are two reasons to do this, in that there is no need to provision a storage account, nor to upload ARM templates manually before each deployment.

In this example, we want to deploy a Logic App and a Service Bus API Connection. Therefore, we are going to build a master template referencing both of these templates.

 

Visual Studio

 

An Azure Resource Group with no specific template is created in Visual Studio. This enables an auto-generated PowerShell script to be retrieved, named Deploy-AzureResourceGroup.ps1. This script is then used to deploy our resources, with or without Visual Studio.

 

ARM templates

 

The contents of the master ARM template are shown below:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "_artifactsLocation": { "type": "string" },
    "_artifactsLocationSasToken": { "type": "securestring" },
    "logicapp_name": { "type": "string" },
    "connections_servicebus_name": { "type": "string" },
    "connections_servicebus_connectionstring": { "type": "securestring" }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2017-05-10",
      "name": "linkedTemplate_logicapp",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[concat(parameters('_artifactsLocation'), '/my_logicapp_template.json', parameters('_artifactsLocationSasToken'))]",
          "contentVersion": "1.0.0.0"
        },
        "parameters": { "logicapp_name": { "value": "[parameters('logicapp_name')]" } }
      },
      "dependsOn": [ "linkedTemplate_apiconnection" ]
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2017-05-10",
      "name": "linkedTemplate_apiconnection",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[concat(parameters('_artifactsLocation'), '/my_apiconnection_template.json', parameters('_artifactsLocationSasToken'))]",
          "contentVersion": "1.0.0.0"
        },
        "parameters": {
          "connections_servicebus_name": { "value": "[parameters('connections_servicebus_name')]" },
          "connections_servicebus_connectionstring": { "value": "[parameters('connections_servicebus_connectionstring')]" }
        }
      }
    }
  ],
  "outputs": {}
}

 

It is important to note that _artifactsLocation and _artifactsLocationSasToken, of type string and securestring respectively, have been added to the parameter list. These parameters are used to form the URI of the linked template with the following syntax: [concat(parameters(‘_artifactsLocation’), ‘/my_template.json’, parameters(‘_artifactsLocationSasToken’))].

There is no need to specify their values. Values are assigned automatically when the PowerShell script is run. However, the parameter file does need to be initialized:

 

"parameters": {
  "_artifactsLocation": { "value": "" },
  "_artifactsLocationSasToken": { "value": "" } // other parameters }

 

Deployment

 

The start of the Deploy-AzureResourceGroup.ps1 script contains the parameters to be replaced using the correct values. There is one other edit to make to the script, namely replacing the condition

null -eq $OptionalParameters[$ArtifactsLocationName]

with

"" -eq $OptionalParameters[$ArtifactsLocationName]

 

The same applies to $ArtifactsLocationSasTokenName.

Remember to initialize a PowerShell session connected to your account and subscription:

Connect-AzAccount Set-AzContext -SubscriptionId $subscriptionId

 

Lastly, run the script, using the option -UploadArtifacts:

./Deploy-AzureResourceGroup.ps1 -UploadArtifacts

 

The runtime log shows that both templates are uploaded before being deployed using the master template.