You are currently viewing Writing Your First Azure Resource Manager Template

Writing Your First Azure Resource Manager Template

In last article we saw some basic concepts about Azure resource manager and template files. Let’s try to write the first ARM template in this article.

This blog article is little longer as we will try to explain each and every step when we are writing the template. Are you ready for the journey ?

Before we begin

There may be a question on why we need to use ARM templates ? The quickest answer can be automation for creating and managing the resources.

But, we already know that there are number of ways you can create and manage Azure resources. We can use Azure Portal, we can use Azure CLI or PowerShell. We can write our own code using client SDKs or we can directly call REST APIs provided by Azure. So, there are a lot of ways of automating, you can write PowerShell script, or you can create CLI script to automate the deployment.

So, why ARM templates ?

Below are some of the reasons why we should choose ARM templates over other options.

Declarative Syntax

In template file, we just need to specify the resources we need, we do not need to specify how to create them. This declarative syntax makes it supereasy to write and manage the templates.

Better Orchestration

You do not need to worry about order of creation of resources. The Azure Resource Manager takes care of identifying interdependent resources and create them in the desired order.

CI/CD Integration

If you are using Azure DevOps (previously, VSTS), then you can use Azure resource manager task to deploy the templates.

IDE Support

Very important for developers, the intellisense support, syntax highlighting in Visual Studio and Visual Studio code.

Writing the first template

Even though we have option to get the quick start template, let’s try to create the template from beginning. The template is for creating a storage account.

Please note that you may want to create multiple resources required for your application e.g. app service, relational database, Redis cache, load balancer, and background web jobs, or functions, etc. In this blog, we will try to keep it simple to understand the building blocks of the template.

Step 1: Basic template

Create a new file, name it as firstTemplate.json.

Open the file and add below contents in it:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": []
}

$schema specifies the schema definition for this template. The contentVersion field defines the version of this document. You can use any value for this field.

The resources collection specifies the resources we want to deploy, let’s try to add one resource in next step.

Step 2: Add resource we want to create

Now, add a resource object under resources collection. The complete JSON file should look as below. Please update {proivde-unique-name} and replace it with unique name for your storage account.

There are some properties which are applicable to each resource.

  • type specifies the resource provider and resource to create
  • apiVersion specifies the Rest API version to use for creating resource
  • location specifies the physical location where resource will be located
  • sku is pricing tier for the resource

There can be some resource type specific properties, for example here we have kind property which specifies to create StorageV2 kind of storage account.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "{provide-unique-name}",
            "location": "westeurope",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "StorageV2",
            "properties": {
                "supportsHttpsTrafficOnly": true
            }
        }
    ]
}

Step 3: Parameters for naming the storage

In previous step, we hardcoded the name of storag eaccount. Below is the complete JSON document which shows how to add parameter storageName and how to use it.

The parameter storageName is of string type and it should have minimum three characters and maximum 24 characters.

Also note down the usage of parameter (i.e. [parameters(‘parametername’)])

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageName": {
            "type": "string",
            "minLength": 3,
            "maxLength": 24
        }
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[parameters('storageName')]",
            "location": "westeurope",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "StorageV2",
            "properties": {
                "supportsHttpsTrafficOnly": true
            }
        }
    ]
}

Step 4: Variables

The variable like in any other programming language is to define and hold the reusable values or objects. In below script, a variables section is added and a new variable “uniqueStorageName” has been added. The variable just concatenates the storage name with resource group id to form the unique name.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageName": {
            "type": "string",
            "minLength": 3,
            "maxLength": 24
        }
    },
    "variables": {
        "uniqueStorageName": "[concat(parameters('storageName'), uniqueString(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[variables('uniqueStorageName')]",
            "location": "westeurope",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "StorageV2",
            "properties": {
                "supportsHttpsTrafficOnly": true
            }
        }
    ]
}

Step 5: Output the result

Here we will add outputs in the JSON document. It will try to print the primary endpoint of the storage account we created.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageName": {
            "type": "string",
            "minLength": 3,
            "maxLength": 24
        }
    },
    "variables": {
        "uniqueStorageName": "[concat(parameters('storageName'), uniqueString(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[variables('uniqueStorageName')]",
            "location": "westeurope",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "StorageV2",
            "properties": {
                "supportsHttpsTrafficOnly": true
            }
        }
    ],
    "outputs": {
        "storageEndpoint": {
        "type": "object",
         "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
        }
    }
}

Deploy the template

You can deploy it using the below Azure CLI script. Do not forget to replace {your-unique-name} with storage account name you want to give.

Also, please make sure you provide appropriate json file path while declaring script variable $templateFile

# to create resource group
az group create \
  --name samplesRG \
  --location westeurope

# provide the template file path
$templateFile="{provide-the-path-to-the-template-file}"

# deploy the template file
az group deployment create      \
  --name addnameparameter       \
  --resource-group samplesRG    \
  --template-file $templateFile \
  --parameters storageName={your-unique-name}

You can review your work by logging in to Azure portal.

I hope this article has provided you enough details to get you started on ARM templates. Let me know your thoughts.

Leave a ReplyCancel reply