简体   繁体   中英

Azure ARM deploy App Service Plan based on input parameters

I'm trying to create an ARM template that contains a App Service Plan and an App Service, but where the App Service Plan only will be created if an optional parameter with reference to an existing App Service Plan is not set.

So the ARM should either create an App Service Plan for the App Service, of just use the existing App Service Plan that is given as an parameter.

How is that possible to do?

SOLUTION

Here is the finale ARM template that works

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "currentServiceplanId": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "Optional. Use an existing serviceplan for deployment"
      }
    },
    "serviceplanSkuName": {
      "type": "string",
      "defaultValue": "B1",
      "allowedValues": [
        "B1",
        "B2",
        "B3",
        "S1",
        "S2",
        "S3",
        "P1",
        "P2",
        "P3",
        "P4"
      ],
      "metadata": {
        "description": "Describes plan's pricing tier and capacity. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
      }
    }
  },
  "variables": {
    "prefix": "setup05",
    "serviceplanName": "[concat(variables('prefix'), 'serviceplan')]",
    "serviceplanId": "[variables('serviceplanIdSelector')[string(equals(length(parameters('currentServiceplanId')), 0))]]",
    "serviceplanIdSelector": {
      "true": "[resourceId('Microsoft.Web/serverfarms', variables('serviceplanName'))]",
      "false": "[parameters('currentServiceplanId')]"
    },
    "api-appName": "[concat(variables('prefix'), 'api-app')]"
  },
  "resources": [
    {
      "name": "[variables('serviceplanName')]",
      "condition": "[equals(length(parameters('currentServiceplanId')), 0)]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-08-01",
      "sku": {
        "name": "[parameters('serviceplanSkuName')]"
      },
      "properties": {
        "name": "[variables('serviceplanName')]",
        "numberOfWorkers": 1
      }
    },
    {
      "name": "[variables('api-appName')]",
      "type": "Microsoft.Web/sites",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-08-01",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('serviceplanName'))]"
      ],
      "properties": {
        "name": "[variables('api-appName')]",
        "serverFarmId": "[variables('serviceplanId')]"
      }
    }
  ],
  "outputs": {
    "ApiDefaultHostname": {
      "value": "[reference(variables('api-appName')).defaultHostName]",
      "type": "string"
    },
    "ApiAppName": {
      "value": "[variables('api-appName')]",
      "type": "string"
    }
  }
}

In your case you should add a dependsOn property to the webApp:

dependsOn: [
    "[variables('serviceplanName')]"
]

This way it will wait for it to finish if it needs to be created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM