Deploy two Azure App Services to the same App Service Plan using idempotent ARM TEMPLATE

牧云@^-^@ 提交于 2019-12-04 15:35:01

Your requirement is default supported by ARM template. Basically if an ARM template encounters a resource which is exist, it will update the resource if the properties do not match. Otherwise it will create the resource using the properties which you set in ARM template.

SOLUTION

The solution in my case was to create three templates:

  • Template 1 to create the app service plan. This ARM template is stored in a BLOB container and is accessible to the release pipeline via a SAS URI.
  • Template 2 to create web app A. This template uses LINKED TEMPLATE features to call and execute the shared template.
  • Template 3 to create web app B. This template uses LINKED TEMPLATE features to call and execute the shared template.

RESULT

  • Both web apps are then published to the same server farm, sharing the instances.
  • The idempotent nature of the deployment is maintained.
  • Single point of truth for any app service plan deployment and any amendments.
  • Money saved on number of server farms required.

EXAMPLE

Shared Service Plan - ARM Template example of a shared service plan:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "planLabel": {
        "defaultValue": "shared-service-plan",
        "type": "string"
    }
},
"variables": {
    "servicePlanName": "[concat(parameters('planLabel'),'-Plan-', uniqueString(resourceGroup().id))]"
},
"resources": [
    {
        "comments": "Creates an App Service Plan on the Standard (S1) SKU.",
        "type": "Microsoft.Web/serverfarms",
        "sku": {
            "name": "S1",
            "tier": "Standard",
            "size": "S1",
            "family": "S",
            "capacity": 2
        },
        "kind": "app",
        "name": "[variables('servicePlanName')]",
        "apiVersion": "2016-09-01",
        "location": "[resourceGroup().location]",
        "properties": {
            "name": "[variables('servicePlanName')]"
        },
        "dependsOn": []
    }
],
"outputs": {
    "servicePlanResourceId": {
        "type": "string",
        "value": "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]"
    },
    "servicePlanName":{
        "type": "string",
        "value": "[variables('servicePlanName')]"
    },
    "resourceGroupName":{
        "type": "string",
        "value": "[resourceGroup().name]"
    }
}
}

Web App A - ARM Template Example containing LINKED TEMPLATE:

{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "servicePlanLabel": {
        "type": "string",
        "metadata": {
            "description": "The base name for the App Service Plan to be used in the linked template."
        },
        "defaultValue": "plan"
    },
    "appServicePlanResourceGroup": {
        "type": "string",
        "metadata": {
            "Description": "The name of the Resource Group the shared App Service Plan will be deployed to."
        },
        "defaultValue": "group"
    },
    "appServicePlanTemplateUri": {
        "type": "string",
        "metadata": {
            "description": "The URI to the App Service Plan linked template in BLOB"
        }
    }
},
"variables": {},
"resources": [
    {
        "apiVersion": "2017-05-10",
        "name": "appServicePlanTemplate",
        "type": "Microsoft.Resources/deployments",
        "resourceGroup": "[parameters('appServicePlanResourceGroup')]",
        "properties": {
            "mode": "Incremental",
            "templateLink": {
                "uri": "[parameters('appServicePlanTemplateUri')]",
                "contentVersion": "1.0.0.0"
            },
            "parameters": {
                "planLabel": {
                    "value": "[parameters('servicePlanLabel')]"
                }
            }
        }
    },
    {
        "apiVersion": "2015-08-01",
        "type": "Microsoft.Web/sites",
        "name": "[variables('webAppName')]",
        "location": "[resourceGroup().location]",
        "kind": "webapp",
        "tags": {
            "Environment": "production",
            "displayName": "App"
        },
        "dependsOn": [
            "[resourceId(parameters('appServicePlanResourceGroup'), 'Microsoft.Resources/deployments', 'appServicePlanTemplate')]"
        ],
        "properties": {}
    }
}
}

Hope this is useful to someone.

Thanks Scott

REF https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-resource-manager/resource-group-linked-templates.md

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!