0
votes

I would like to deploy ARM templates individually rather than linked templates/nested templates/multiple resources.

Ex: Deploy a Sql Server and database

I created individual templates for Sql Server and Sql database . I deployed successfully and its working fine.

  1. Deploy Sql Server ARM template(Specifically designed only for sql server)
  2. Deploy Sql Server Database(Specifically designed only for sql database with above sql server name mentioning in parameters file)

While deploying Sql database arm template , I had specified accurate sql server name(deployed in Step1) in parameters file of step2 but I did not mentioned "dependson" parameter in resource section and directly deployed .Database creation successfully under the resource group I selected while deployment process.

My Query:

How can I make sure Sql database arm template is deploying under specific server name(Step1) only without dependson parameter?

or

How to use existing reference resource id in resources section without dependson?

Will output of step1(resourceid) of sql server will be any helpful?

Sql ARM Template Deploy JSON:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "collation": {
            "type": "string",
            "defaultValue": "SQL_Latin1_General_CP1_CI_AS",
            "metadata": {
                "description": "The collation of the database."
            }
        },
        "edition": {
            "type": "string",
            "metadata": {
                "description": "The edition of the database. The DatabaseEditions enumeration contains all the valid editions. e.g. Basic, Premium."
            },
            "defaultValue": "Basic"
        },
        "sqlservername": {
            "type": "string",
            "metadata": {
                "description": "The name of the sql server."
            }
        },
        "databasename": {
            "type": "string",
            "metadata": {
                "description": "The name of the database to be operated on (updated or created)."
            },
            "minLength": 7,
            "maxLength": 128
        },
        "maxSizeBytes": {
            "type": "string",
            "metadata": {
                "description": "The max size of the database expressed in bytes."
            }
        },
        "serviceobjectivename": {
            "type": "string",
            "metadata": {
                "description": "The configured service level objective ID of the database. This is the service level objective that is in the process of being applied to the database."
            },
            "defaultValue": "Basic"
        },
        "tagsArray": {
            "type": "object",
            "metadata": {
                "description": "Resource Tags helps to indentify the use of service"
            }
        }
    },
    "functions": [],
    "variables": {
        "sqldatabasename": "[concat(parameters('sqlservername'),'/',parameters('databasename'))]"
    },
    "resources": [
        {
            "name": "[variables('sqldatabasename')]",
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2014-04-01",
            "location": "[parameters('location')]",
            "tags": "[parameters('tagsArray')]",
            "properties": {
                "collation": "[parameters('collation')]",
                "edition": "[parameters('edition')]",
                "maxSizeBytes": "[parameters('maxSizeBytes')]",
                "requestedServiceObjectiveName": "[parameters('serviceobjectivename')]"
            }
        }
    ],
    "outputs": {
        "sqldatabaseresourceId": {
            "type": "object",
            "value": "[reference(resourceId('Microsoft.Sql/servers/databases',parameters('sqlservername'), parameters('databasename')),'2014-04-01')]"
        }
    }
}

Template Parameters:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "tagsArray": {
            "value": {
                "Environment": "POC"
            }
        },
        "servername": {
            "value": "sql-test"
        },
        "sqlAdministratorLogin": {
            "value": "sqladmin"
        },
        "sqlAdministratorLoginPassword": {
            "value": "myPassword586@"
        },
        "firewallIpAddresses": {
            "value": [
                {
                    "start": "1.1.1.0",
                    "end": "1.1.1.1",
                    "clientName": "Clienttest1"
                },
                {
                    "start": "1.2.3.4",
                    "end": "1.2.3.16",
                    "clientName": "Clienttest2"
                }
            ]
        },
        "location": {
            "value": ""
        }
    }
}
1
According to the template you provided, it is right. It will deploy the database to the special SQL server. What do you want to know?Jim Xu
@JimXu I want to know how can we use sql server resource id (not sql parameter name that mentioned in parameters file ) without "dependson" parameter in resource section or adding other another resource for sql server in arm templatePavanKumar GVVS
Is that you want to know how to use resource id to get SQL server information?Jim Xu
@JimXu Yes and also I want to make sure sql database arm is deploying under specific sql server resource id without mentioning "dependson" parameter. To get resource id of Sql Server : "[resourceId('Microsoft.Sql/Sqlservers/', parameters('sqlservername'))]", But if i use same under "dependson" , i have to create another resource which I dont want to. What i am trying is without "dependson" section , I want to make sure sql database arm is deploying under the sql server resourceidPavanKumar GVVS
You can use the function reference with SQL server resource id to get server information in the resource sectionJim Xu

1 Answers

1
votes

It is important to understand that dependsOn enables you to define one resource as dependent on one or more resources within your template only, and should not be used to map relationships between your resources. As explained in the ARM Template documentation, dependsOn isn't the right approach to document how resources are interconnected.

How can I make sure Sql database arm template is deploying under specific server name(Step1) only without dependson parameter?

Microsoft.Sql/servers/databases is a child resource of Microsoft.Sql/servers, and it is the name of the child resource that defines the connection with the parent resource.

You would have had to set the dependsOn property if you were deploying the child resource after the parent resource in the same ARM template, since an implicit deployment dependency isn't automatically created between them. Specifying dependsOn in this case would ensure that the parent resource is deployed (and exists) before the child resource.

That said, when defined outside of the parent resource, you format the type and name with slashes to include the parent type and name.

"type": "{resource-provider-namespace}/{parent-resource-type}/{child-resource-type}",
"name": "{parent-resource-name}/{child-resource-name}",

So, a SQL Database might be defined as:

{
  "type": "Microsoft.Sql/servers/databases",
  "name": "[concat(variables('sqlServerName'), '/', parameters('databaseName'))]",
  ...

Coming to your next question:

How to use existing reference resource id in resources section without dependson?

Will output of step1(resourceid) of sql server will be any helpful?

As you have already deployed the parent resource (DB Server) in a different template, don't set a dependency. Instead, deploy the child resource (Database) to the same resource group and provide the name of the parent resource. That should suffice.

Example:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sqlServerName": {
            "type": "string",
            "defaultValue": "sqlserver"
        },
        "databaseName": {
            "type": "string",
            "defaultValue": "mydb"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2014-04-01",
            "name": "[concat(parameters('sqlServerName'), '/', parameters('databaseName'))]",
            "location": "[resourceGroup().location]",
            "properties": {
                "collation": "SQL_Latin1_General_CP1_CI_AS",
                "edition": "Basic"
            }
        }
    ],
    "outputs": {}
}