We have already learned that resources can have parent resources, and that child resources can be provisioned only after a parent resource is already provisioned. In the next example, we will provision a parent resource (sites) using ARM template. You will notice that the sites resource has an internal resources element. This element can declare multiple child resources within it. A resource of connectionstrings type is created within this resources section. This particular resource acts like a child resource for the sites parent resource. If you think about it, a connectionstrings resource can only be contained within a web app (sites); it does not make sense to deploy a connectionstrings resource without a web app, because connectionstrings are eventually placed within the web.config file of a web app.
The complete code for the following template is available in the chapter-3 - listing2.txt file in the accompanying chapter code.
The following template defines a number of parameters that will configure the App Services Plan. Notice how default values, minimum values, and metadata elements are used to augment the behavior of the skuCapacity parameter, as follows:
"parameters": {
"skuName": {
"type": "string",
"defaultValue": "F1"
},
"skuCapacity": {
"type": "int",
"defaultValue": 1,
"minValue": 1,
"metadata": {
"description": "Describes plan's instance count"
}
}
}
There are a couple of variables declared in the variables section, as shown in the next code listing. These variables relate to the name of the App Service Plan and Web App resource.
"variables": {
"hostingPlanName": "[concat('hostingplan', uniqueString(resourceGroup().id))]",
"webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]"
}
This template contains two top-level resources - Microsoft.Web/serverfarms and Microsoft.Web/sites. The Microsoft.Web/sites resource contains an inner resource of type config.
The code shown next relates to resource of type Microsoft.Web/serverfarms:
{
"apiVersion": "2016-03-01",
"name": "[variables('hostingPlanName')]",
"type": "Microsoft.Web/serverfarms",
"location": "[parameters('location')]",
"tags": {
"displayName": "HostingPlan"
},
"sku": {
"name": "[parameters('skuName')]",
"capacity": "[parameters('skuCapacity')]"
},
"properties": {
"name": "[variables('hostingPlanName')]"
}
}
The code related to Microsoft.Web/sites resource is shown in the next code listing.
As mentioned before, this resource contains an inner-resource. The inner-resource is dependent on its parent resource and should not be provisioned before provisioning the parent resource. The dependsOn element in the inner-resource ensures that the inner resource starts provisioning only after the parent resource has been provisioned. The inner-resource has access to the parameters and variables defined within the template.
{
"apiVersion": "2016-03-01",
"name": "[variables('webSiteName')]",
"type": "Microsoft.Web/sites",
"location": "[parameters('location')]",
"dependsOn": [
"[variables('hostingPlanName')]"
],
"properties": {
"name": "[variables('webSiteName')]",
"severFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
},
"resources": [
{
"apiVersion": "2016-03-01",
"type": "config",
"name": "connectionstrings",
"dependsOn": [
"[variables('webSiteName')]"
],
"properties": {
"DefaultConnection": {
"value": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', variables('databaseName'), ';User Id=', parameters('sqlAdministratorLogin'), '@', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ';Password=', parameters('sqlAdministratorLoginPassword'), ';')]",
"type": "SQLAzure"
}
}
}
]
}
The final segment of this template contains an outputs element that returns back data, as shown in the next code snippet.
"outputs": {
"siteUri": {
"type": "string",
"value": "[reference(concat('Microsoft.Web/sites/', variables('webSiteName'))).hostnames[0]]"
}
}