Create Azure Web App in existing App Hosting Plan using Azure ARM template

When working with Azure services, you will combine services together. Many times, you would need to add an Azure Web App to an existing App Hosting Plan rather than creating a new app hosting plan every time you want to create an azure app service. This is a useful strategy to save cost if the load on the web site is not high. In this blog post we are going to discuss how we can leverage Azure ARM to deploy an app service to an existing app hosting plan.

In one of the previous posts, we discussed how to create an app hosting plan and an azure app service in one go using Azure ARM. The way we linked an hosting plan with app service is by mentioning app hosting plan id inside the property of the web app:

creating azure app service with new hosting plan

and we made sure that app hosting plan gets created first by mentioning it inside the dependsOn property of the web app.

Now, since our app service plan already exists, we first need to remove it from inside the dependsOn properties of the web app. If we don’t do that we would keep getting an error like this when trying to deploy template:

New-AzureRmResourceGroupDeployment : 1:31:56 AM – Error: Code=InvalidTemplate;
01:31:56 – Message=Deployment template validation failed: ‘The resource
01:31:56 – ‘Microsoft.Web/serverfarms/xxxx-yyyy’ is not defined in the template. Please
01:31:56 – see https://aka.ms/arm-template for usage details.

This is because of the reason that dependsOn property always maps to resources in the existing template. So Azure will throw invalid template error as you have not defined ARM code for creating app hosting plan.

Now, to link the web app to the existing plan, we could use something like below code:

“serverFarmId”: “[resourceId(‘Microsoft.Web/serverfarms’, parameters(‘appHostingPlanName’))]”

Here, assuming that appHostingPlanName is supplied as parameter. However this will not work for some reason. The way it works is also to use resource group name and then fetch the app hosting plan resource id. So to make it work, we need to fetch app hosting plan id like below:

“serverFarmId” : “[resourceId(resourceGroup().name,’Microsoft.Web/serverfarms’, parameters(‘appHostingPlanName’))]”

So, combining two above changes would make our web app linked to the resource id of the existing app service plan. In above line of code, resourceGroup().Name will provide the name of current azure resource group used for deployment.

This is how the ARM code would look like:

creating azure app service with existing app hosting plan
creating azure app service with existing app hosting plan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s