Search code examples
azure-resource-managerazure-rm-template

Azure ARM Templates - How to pass output variable from linked template as parameters to another template?


Lets say we have 2 linked arm templates with their associated *.params.json files:

  1. createAppServiceTemplate.json
  2. createAppServiceTemplate.params.json
  3. createSqlDbTemplate.json
  4. createSqlDbTemplate.params.json

The second template createSqlDbTemplate.json has outputs:

 "outputs": {
        "connectionString": {
            "type": "string",
            "value": "[concat('Database=', parameters('databaseName'), ';Server=', reference(resourceId('Microsoft.DBforPostgreSQL/servers',parameters('serverName')), '2017-12-01').fullyQualifiedDomainName, ';User Id=', parameters('administratorLogin'),'@', parameters('serverName'),';Password=', parameters('administratorPassword'))]"
        }
    }

I wan't to extract this output variable and pass it as parameter to createAppServiceTemplate.params.json.

I have tried to add this to my :

"parameters": {
    "usersDbConnectionString": {
          "value": "[reference('createSqlDbTemplate').outputs.connectionString.value]"
    }
 }

But I think parameters can't evaluate expressions? How can I achieve the desired behaviour ?


Solution

  • Assuming you had the linked createSqlServerTemplate.json template below:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {},
      "variables": {},
      "resources": [],
      "outputs": {
        "greetingMessage": {
          "value": "Hello World",
          "type" : "string"
        }
      }
    }
    

    The main template deploys the linked template and gets the returned value. Notice that it references the deployment resource by name, and it uses the name of the property returned by the linked template.

    main-template.json:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {},
      "variables": {},
      "resources": [
        {
          "type": "Microsoft.Resources/deployments",
          "apiVersion": "2019-10-01",
          "name": "createAppServiceTemplate",
          "properties": {
            "mode": "Incremental",
            "templateLink": {
              "uri": "[uri(deployment().properties.templateLink.uri, 'createAppServiceTemplate.json')]",
              "contentVersion": "1.0.0.0"
            },
            "parameters": {
               "userDbConnectionString: {
                  value: "[reference('createSqlServerTemplate').outputs.greetingMessage.value]"
               }
            }
          }
        },
        {
          "type": "Microsoft.Resources/deployments",
          "apiVersion": "2019-10-01",
          "name": "createSqlServerTemplate",
          "properties": {
            "mode": "Incremental",
            "templateLink": {
              "uri": "[uri(deployment().properties.templateLink.uri, 'createSqlServerTemplate.json')]",
              "contentVersion": "1.0.0.0"
            }
          }
        }
      ]
    }
    

    Reference