[EN] ARM templates - getting started guide

Posted by     "Maciej Gos" on Tuesday, August 11, 2020

Recently I started my #100DaysOfCloud journey so this guides me to write some thoughts about ARM templates. But why ARM templates as first blog post in this journey?

The answer for me is straight forward ARM allow me to quick and easily create resources for testing new concepts without wasting money. I could just simply drop whole resource group at the end of day or when I finish my tutorial. On next day I could just simply load everything from template.

ARM Templates

ARM (Azure Resource Manager) template is a kind of JSON file which allow us to define and manage resources using CLI, PowerShell or Portal. Its main usage scenario if in automation/scripting and CI/CD pipelines and of course for Infrastructure-As-A-Code approach in our projects.

Using templates we can simple store them in git repository and track all changes in our infrastructure.

The structure

Below is the basic/empty example of template file. I try to go thru each section and explain they purpose.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "functions": [],
    "variables": {},
    "resources": [],
    "outputs": {}
}

parameters

As name suggests this section contains parameters of our template such us resource name, location or event SKU tier which could be provided later during execution.

"parameters": {
    "databaseAccountName": {
    "type": "string",
    "minLength": 3,
    "metadata": {
        "description": "The Azure Cosmos DB database account name."
    }
    },
    "consistencyLevel": {
    "type": "string",
    "defaultValue": "Session",
    "allowedValues": [
        "Eventual",
        "Strong",
        "Session",
        "BoundedStaleness"
    ],
    "metadata": {
        "description": "The Azure Cosmos DB default consistency level for this account."
    }
    },
}

functions

In functions section we can create our user-defined functions with complicated expressions which are frequently used in our template. Of course they can also extend build in functions. The standard build-in functions you can find here.

If you want to learn more about user-defined function check here.

And of course an example

"functions": [
  {
    "namespace": "contoso",
    "members": {
      "uniqueName": {
        "parameters": [
          {
            "name": "namePrefix",
            "type": "string"
          }
        ],
        "output": {
          "type": "string",
          "value": "[concat(toLower(parameters('namePrefix')), uniqueString(resourceGroup().id))]"
        }
      }
    }
  }
],

variables

In case when we want to customize our parameter ex. add some unique string or make some concatenation we use variable which will be used in our template.

"variables": {
    "sqlServerName": "[concat(uniquestring(resourceGroup().id), 'sqlserver')]",
    "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]"
},

resources

And finally resources. This is the main place and most interesting where all magic happens. In this place we define and manage our resources.

"resources": [
    {
    "apiVersion": "2020-04-01",
    "type": "Microsoft.DocumentDB/databaseAccounts",
    "name": "[parameters('databaseAccountName')]",
    "location": "[parameters('location')]",
    "tags": {
        "displayName": "DocumentDB"
    },
    "properties": {
        "name": "[parameters('databaseAccountName')]",
        "databaseAccountOfferType": "[parameters('documentDBOfferType')]",
        "consistencyPolicy": {
        "defaultConsistencyLevel": "[parameters('consistencyLevel')]",
        "maxStalenessPrefix": "[parameters('maxStalenessPrefix')]",
        "maxIntervalInSeconds": "[parameters('maxIntervalInSeconds')]"
        },
        "locations": [
        {
            "locationName": "[parameters('location')]",
            "failoverPriority": 0
        }
        ]
    }
    }
]

outputs

In outputs we can define values returned by our template which can used further in scripts or pipelines.

The Tools

The main tool and most widely used is of course Visual Studio Code with ARM Tools.

Second option is Azure Portal However this option has one small problem😉…It is still in preview after few years of existence.

Deployment

To deploy our templates we use CLI, PowerShell or Portal but I stay for now in Windows Terminal territories.

PowerShell script option

$SourcePath = 'D:\Learn\Azure\ARM-templates\Onstage'

# Create Resource Group
$ResourceGroupName = 'Onstage-ARM-Test-RG'
New-AzResourceGroup -Name $ResourceGroupName -Location 'westeurope'

# Deploy
New-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName `
    -TemplateFile "$SourcePath\onstage.template.json" `
    -TemplateParameterFile "$SourcePath\onstage.template.parameters.json"

Azure CLI option

# Here we create our resource group....

az deployment group create --resource-group arm-webapp-rg --name Initial --template-file .\azuredeploy.json --parameters azuredeploy.parameters.json

Some helpful links when getting started

Summary

As you can see ARM is not such a complicated beast it can be but I think that after some time it is easy and straight forward. Me personally huge benefit is possibility to drop whole infrastructure at the end of day and not loosing money during night😊.

What is your reasons to learn ARM templates?

Photo by Markus Spiske on Unsplash

Maciej Gos

真诚赞赏,手留余香

使用微信扫描二维码完成支付


comments powered by Disqus