1
votes

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"
    }
  }
}
1
Here is an example of what i have tried: pastebin.com/PnH6ehf6 - but the problem in this template is when i want a new service plan to be created - the app service istn awaiting for the app service plan to be created - but im not able to set an dependon. any suggestions?pajzo
I think that you can use templatelink to use two templates.One for creating App service Plan, the other one for using exist service Plan. In this method, you should already prepared tow templates with specific name. I'm not sure whether you can understand this. But this method should work. You can refer to this template to use templatelink :github.com/rjmax/ArmExamples/blob/master/…Wayne Yang
Hi,@pajzo, the templates in your solution didn't work for me . I just got deployment failed.Wayne Yang
@WayneYang-MSFT you should créate a question ;)4c74356b41
@Wayne Yang - MSFT i do several deployment tests in Azure to test the finale ARM template. Both with or without existing service plan. Remember to edit the prefix variable, to make it unique ;-)pajzo

1 Answers

3
votes

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.