This post will show you how to use the Azure portal to save JSON templates from an existing resource group.
Saving a Resource Group as a Template
It is possible to manually deploy a service into a resource group and then create a JSON template from the contents of the resource group; you can use this template as the basis for a new service, or you can create more instances of this service; for example, development, testing, production, and diagnostics.
The process for getting the JSON is very simple. Browse to the resource group in the Azure Portal and select Automation Script.
Note: The Azure Portal is constantly changing, so the names of entries in the GUI might (I should say “will”) change after we publish this article.
The JSON that will duplicate the contents of the resource group is displayed here. You can also see the Parameters JSON file, as well as the CLI, PowerShell, .NET and Ruby options for deploying the JSON template.There are some interesting actions that you can perform from here:
- Download: Get a zip file your JSON deployment. We’ll return to this later in the post.
- Add to Library: We’ll return to this option where you can save the JSON file in Azure.
- Deploy: You can create another copy of your service into another resource group. This option can work, but it’s messy if you do not customize the JSON template first.
Let’s save the JSON template in Azure so that we can re-use it. Click Add To Library. You are asked to name the template and add a description.Tip: You will edit your templates over time. It’s a good idea to version your templates so that you can keep older versions and rollback if necessary.
Viewing Your Template in the Azure Portal
Any templates that you save in the Azure Portal are associated with your identity and, by default, can only be seen and used by you. Browse to Templates in the Azure Portal; here is where you will see any JSON templates that you have saved or uploaded.
Click the JSON that was just saved. The template is displayed. There are a few actions that you can do:
- Deploy: Deploy the JSON template to a new or existing resource group.
- Edit: Edit the template.
- Delete: Delete the template.
- Share: Share the template with other users/groups (ideally the latter) in your Azure AD domain.
Downloading Your Template
If you want a quick and dirty re-deployment, then the previous steps can be used to save a template and then you can deploy it without editing it. You’ll find that the template will require a lot of data to be entered as confusingly named parameters, and some items, such as load balancers (NAT and load balancing rules), could be quite complicated to deal with. The editor in the Azure Portal is OK for small tweaks, but it isn’t a great way to make larger changes. The best approach to creating a template that you want to keep is to download the original template and edit it using the free VS Code or Visual Studio.
Browse to the resource group and click the Download action. A zip file will be downloaded by your browser. You will find the following if you extract the contents of the zip file:
- PS1: A comprehensive PowerShell script for deploying the template.
- SH: A command-line interface script that will deploy the template.
- rb: A Ruby script that will deploy the template.
- CS: A C# file for deploying the template.
- JSON: A file that you can edit to pre-program the data that will be supplied as parameters for the deployment.
- JSON: The JSON file that describes the deployment.
- Reduce the number of parameters and convert as much as possible into variables.
- I rename parameters (easier in VS Code with the JSON extensions installed) to make it easier to understand their functions.
- Enter new default options in parameters and sometimes restrict choices using drop-down list box.
- Replace string values from the Resources section with reusable variables.
- Tweak/add/remove resources as necessary.
Make sure that your Parameters.JSON is updated to match the Parameters section of the new version of Template.JSON. Then you can deploy the new version of your JSON using:
- One of the provided scripts
- The Azure Portal