Sunday, 1 January 2023

ARM Template

 

What are ARM Template?

ARM Template

ARM (Azure Resource Manager) template is a block of code that defines the infrastructure and configuration for your project. These templates use a declarative syntax to let you define your deployment in the form of JSON (JavaScript Object Notation) files. All the resources and their properties are defined in this template. This helps in automating the deployment process in a constant flow.

Benefits of Using ARM Templates

ARM Templates provides multiple advantages over the deployment process.

  • Using ARM Templates, we can declare network infrastructure, storage and any other resources.
  • Over the development lifecycle, ARM Templates allows the deployment of resources repeatedly in a consistent manner.
  • User can deploy templates parallelly, and only one command is sufficient to deploy all your resource settings.
  • Templates can be divided into different modules. In other words, templates can be broken into multiple templates so that a parent template can consist of small templates.
  • The PowerShell or Bash Scripts can be added to the templates using deployment scripts.
  • The working of ARM Templates can be tested using the ARM template toolkit.
  • A user can see the preview of the template. All the resources that are being created or deleted in this template will be shown in the preview.
  • A user can integrate templates with Continuous Integration (CI) and Continuous Deployment (CD) tools to automate the release.

Azure Quickstart Template

Azure Quickstart Templates are ready to deploy sample templates. The Microsoft community creates these templates. You will find various Azure Quickstart templates on Microsoft’s official site like creating a VM (Virtual Machine), deploying the VM, etc. A beginner can use these quickstart templates to get familiar with the concept.


After visiting the Azure QuickStart Template portal, you will see some template samples. Click on any sample and then choose ‘Browse on Github‘. Next, you can click on the ‘Visualize‘ button to see the preview of templates. Finally, you can choose the ‘Deploy‘ option to deploy the template to your Azure Portal.

Create and Deploy ARM Template

After learning the format of the ARM Templates, we are ready to learn the process of creating and deploying ARM templates. For writing a template, any JSON editor will work. Also, Visual Studio Code is a tool provided by Microsoft for writing codes in various formats, including JSON. You can get Visual Studio Code from the official Microsoft website or store. No tool is required by the users with access to Azure Portal as the platform already contains built-in editors. So, we will use Azure Portal in this blog and assume that you have Azure access. Now, visit and log in to your Azure Portal. Then, follow the steps with us to create and deploy ARM Templates.

Create Resource

Cross-check the highlighted URL in the image below. Now, the first step is to create resources in the Azure Portal.

Azure Portal

Check the above image. You will find the options to create a resource in Azure Portal. Click on the ‘Create a Resource‘ button.

Create Resource

In the next window, you will see various options listed to create a resource. Some of these options are to create Windows, Ubuntu, Web App, SQL Database etc. We will use a template for creating resources, so click on the search bar. Search template and click on ‘Template Deployment (deploy using custom template)’ as highlighted in the above image.

Deploy Template

Template Deployment (deploy using custom template) window will open up. Now, click on the ‘Create‘ button as shown in the above image.

Build Your Own Template

Azure Portal will show the ‘Custom Deployment‘ window after clicking the ‘Create‘ button in the previous steps.

Build Template

To create a template from scratch, click on the ‘Build your own template in the editor‘ option as highlighted in the above image. Also, if you want to import your own create a template, you can pick the same option.

Create ARM Template in Template Editor

Now, Azure Portal will open the ‘Template Editor‘ window. Here a simple format of the ARM Template will be available in the editor. Using the format, you can write and add your own code in the editor. The highlighted menu at the top in the above image are options that you can pick to add a resource, use a quickstart template, load your template file and download the created template.

Use QuickStart Template

Creating a template from scratch might be complicated for you, especially when you are a beginner. In Azure Portal, you can use quickstart templates instead of creating a template from the start.

Quickstart Templates

For using quickstart templates, instead of opting for the ‘Build your own template in the editor‘, we will follow the below steps.

Step 1) Click on the ‘Quickstart template‘ radio button.

Step 2) Click on the drop-down button to see all the quickstart templates you can use. Next, search for the template that you want to use. Here, we choose a basic template to create a storage account and enable security on it.

Step 3) Click on the ‘Edit template‘ to modify and make changes in your quickstart template before deploying.

Edit Template

In the template editor window, you can make the required changes. On the left side of the editor window are the parameters, variables and resources that this code will create. You can also use the above menu for downloading this template. When you are ready with the template code, click on the ‘Save‘ button as highlighted in the above image. After saving the template, another window will open to confirm all the resources that this template will create. After completing all the verification, you are ready to deploy your template and purchase the resources.

How to Deploy ARM Templates using PowerShell

While there are several ways to deploy Azure resources using ARM templates, this tutorial focuses on deploying a storage account using PowerShell. You will need the following items to follow along with this tutorial:

  • PowerShell 5.1 or later is required. This tutorial makes use of version 7.1.3.
  • PowerShell module for Azure (Az). This tutorial makes use of version 6.0.0.
  • An Azure administrator account with resource creation permissions, such as Owner or Contributor.

i) Connect to Azure

To use the Az PowerShell module commands, you must first connect to Azure with an authenticated account. When you run the Connect-AzAccount command, PowerShell will launch an Internet browser to complete the authentication process.

ii) Create a Resource Group

Create a resource group to house the ARM template resources. Give the resource group a name and an Azure region by using the New-AzResourceGroup command. Save the command output to a variable called $rg so you can refer to it later. The resource group’s name is “armdemo-rg,” and the location is “WestUs2.”

$rg = New-AzResourceGroup -Name armdemo-rg -Location WestUs2

iii) Create an ARM Template

To create the resource, you will need an ARM template with a storage account defined. Here is a simple ARM template that uses a single parameter called StorageAccountName to set the storage account name. This should be saved to a file called storageAccount.json.

No comments:

Post a Comment