Azure Resource Manager - Part 8 - Export Template for Resources in a Resource Group with the REST API

This article is part of a series. Here's a list of all available parts.

Background

This blog post will be about how to in the easiest way possible (well, as a developer anyway) export a JSON template from the Azure Resource Manager.

If you're using the ARM (Azure Resource Manager) just like me and you aare automating a lot of tasks and deployments - one thing that sometimes happen is that a resource group deviates from the original template after being modified or additional resources were added manually, or any other valid approach that happens in the real world (even if they're not always ideal).

Falling in line with this blog series, I'll show you how easy it is to get the full JSON template of your deployed resources by using the Azure Resource Manager REST API.

Use the Azure Resource Manager REST Api to Export Template for your resources

The following steps will assume that you're already familiar with running REST queries with the ARM.

If you're looking for the basics of getting started, please check out the other posts in this article series. Start here.

So pose that I have a Resource Group called Bots (because I do, and they're awesome). This Resource Group contains only three simple resources for this demo showcase, which are:

  • zimmer-slack-bot-demo: Application Insights
  • SkynetServicePlan: The App Service Plan
  • zimmer-slack-bot-demo: Web App

Check out how we can generate a template for re-use.

The POST Request

The POST request is simple, as always. You only need to target your Resource Group, and then append /exportTemplate in order to send the request for getting the json formatted template.

POST https://management.azure.com/subscriptions/b67713f0-97f6-4565-90a0-2dcae01a59ae/resourceGroups/MyResourceGroup/exportTemplate?api-version=2014-04-01

The JSON Response

Once the POST request has been sent, and you get the generated results back (the request could take a few microsoft moments if you have a lot of resources), you'll receive a JSON template back fully automated and ready to be re-used and re-deployed wherever you want.

{
  "template": {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "components_zimmer_slack_bot_demo_name": {
        "defaultValue": "zimmer-slack-bot-demo",
        "type": "String"
      },
      "serverfarms_SkynetServicePlan_name": {
        "defaultValue": "SkynetServicePlan",
        "type": "String"
      },
      "sites_zimmer_slack_bot_demo_name": {
        "defaultValue": "zimmer-slack-bot-demo",
        "type": "String"
      }
    },
    "variables": {},
    "resources": [
      {
        "comments": "Generalized from resource: '/subscriptions/b67713f0-97f6-4565-90a0-2dcae01a59ae/resourceGroups/Bots/providers/microsoft.insights/components/zimmer-slack-bot-demo'.",
        "type": "microsoft.insights/components",
        "kind": "web",
        "name": "[parameters('components_zimmer_slack_bot_demo_name')]",
        "apiVersion": "2014-04-01",
        "location": "Central US",
        "tags": {
          "hidden-link:/subscriptions/b67713f0-97f6-4565-90a0-2dcae01a59ae/resourceGroups/Bots/providers/Microsoft.Web/sites/zimmer-slack-bot-demo": "Resource"
        },
        "properties": {
          "ApplicationId": "[parameters('components_zimmer_slack_bot_demo_name')]"
        },
        "dependsOn": []
      },
      {
        "comments": "Generalized from resource: '/subscriptions/b67713f0-97f6-4565-90a0-2dcae01a59ae/resourceGroups/Bots/providers/Microsoft.Web/serverfarms/SkynetServicePlan'.",
        "type": "Microsoft.Web/serverfarms",
        "sku": {
          "name": "S1",
          "tier": "Standard",
          "size": "S1",
          "family": "S",
          "capacity": 1
        },
        "name": "[parameters('serverfarms_SkynetServicePlan_name')]",
        "apiVersion": "2015-08-01",
        "location": "North Europe",
        "properties": {
          "name": "[parameters('serverfarms_SkynetServicePlan_name')]",
          "numberOfWorkers": 1
        },
        "dependsOn": []
      },
      {
        "comments": "Generalized from resource: '/subscriptions/b67713f0-97f6-4565-90a0-2dcae01a59ae/resourceGroups/Bots/providers/Microsoft.Web/sites/zimmer-slack-bot-demo'.",
        "type": "Microsoft.Web/sites",
        "name": "[parameters('sites_zimmer_slack_bot_demo_name')]",
        "apiVersion": "2015-08-01",
        "location": "North Europe",
        "tags": {
          "hidden-related:/subscriptions/b67713f0-97f6-4565-90a0-2dcae01a59ae/resourcegroups/Bots/providers/Microsoft.Web/serverfarms/SkynetServicePlan": "empty"
        },
        "properties": {
          "name": "[parameters('sites_zimmer_slack_bot_demo_name')]",
          "hostNames": [
            "zimmer-slack-bot-demo.azurewebsites.net"
          ],
          "enabledHostNames": [
            "zimmer-slack-bot-demo.azurewebsites.net",
            "zimmer-slack-bot-demo.scm.azurewebsites.net"
          ],
          "hostNameSslStates": [
            {
              "name": "[concat(parameters('sites_zimmer_slack_bot_demo_name'),'.azurewebsites.net')]",
              "sslState": 0,
              "thumbprint": null,
              "ipBasedSslState": 0
            },
            {
              "name": "[concat(parameters('sites_zimmer_slack_bot_demo_name'),'.scm.azurewebsites.net')]",
              "sslState": 0,
              "thumbprint": null,
              "ipBasedSslState": 0
            }
          ],
          "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_SkynetServicePlan_name'))]"
        },
        "dependsOn": [
          "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_SkynetServicePlan_name'))]"
        ]
      }
    ]
  }
}

Summary

With little effort we have now exported our JSON template for this entire resource group. This can be a key to success when talking about automation and deployments. I am using this regularly to see how my deployed resources differ from the templates I have in my repositories. Or even better, if you want to design a new template by creating resources in Azure - you can configure them through the portal easily until you're happy, then just run the exportTemplate POST request and you're good to go with a fresh template which is automatically generated based off of your resources.