I am here again with a new topic on Azure to share with you. Today I have come with Azure ARM template. In my first article, I will share the fundamental details of ARM template. Possibly in the next few weeks, I will come up with an advanced ARM template topic. If you have any specific query regarding ARM template please post your comment here.
Azure Resource Manager
When you create an application or use any Azure service it is typically made up of a lot of other components just like a virtual machine, virtual network, Storage, Cache service, database service or else any other service. These are not separate entities. You can see that these are all interdependent on each other. Actually, Azure Resource Manager enables you to maintain and deploy all these resources in the group. This grouping helps you to maintain all related resources like tagging and deployment as per interdependency. In ARM you use Template to deploy all related resources in order and in a single click.
Overview of ARM Template
ARM template is simple JSON format template which defines the infrastructure and configuration of the Azure solution. You may be aware that before ARM template there was ‘Classic Mode’ of Deployment. We used to call API to create different resources. It used to call ‘ASM (Azure Service Management)’.
Structure of ARM Template
There are mainly 4 important sections in the Template as seen by the below parameters, variables, resources, and output.
- {
- "$schema": "",
- "contentVersion": "",
- "parameters": { },
- "variables": { },
- "resources": [ ],
- "outputs": { }
- }
- Parameters
This section is where you define all your input parameters. Any value which you don’t want to be static you pass to template on call.
- Variables
This is a section where you will define all static value variables which you can use overall anywhere in the template.
- Resources
Collection of resources to be deployed.
- Outputs
If you want to return any particular value or list of variables then this section can be used as output.
$schema is actually a location of schema file which describes the version of the template language. This URL will be different as per different versions.
contentVersion = its version of the template, you can pass any value here by inversion. But while deploying this template it makes sure the correct template is going to be used.
Sample ARM template
This is one basic ARM template to create Azure Storage. It contains one input parameter named ‘storageAccountType’ (string) and the default value is ‘Standard_LRS’ (if this parameter is blank then it will use the default value). It has one variable ‘storageAccountName’. Instead of simple string, it contains a concert and unique string function to generate storage account name. In resource section one storage account is going to be created.
- {
- "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
- "contentVersion": "1.0.0.0",
- "parameters": {
- "storageAccountType": {
- "type": "string",
- "defaultValue": "Standard_LRS",
- "metadata": {
- "description": "Storage Account type"
- }
- }
- },
- "variables": {
- "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
- },
- "resources": [
- {
- "type": "Microsoft.Storage/storageAccounts",
- "name": "[variables('storageAccountName')]",
- "apiVersion": "2016-01-01",
- "location": "[resourceGroup().location]",
- "sku": {
- "name": "[parameters('storageAccountType')]"
- },
- "kind": "Storage",
- "properties": {
- }
- }
- ],
- "outputs": {
- "storageAccountName": {
- "type": "string",
- "value": "[variables('storageAccountName')]"
- }
- }
- }
Parameters
While defining any parameters you can add conditions on this parameter value and other properties. See the below example:
- "storagename": {
- "Type": "string",
- "allowedValues": [
- "storage1",
- "storage2",
- "storage3"
- ],
- "defaultValue": "storage1",
- "minLength": "2",
- "minLength": "50",
- },
- Default Value
Default value is to be used if one is not provided.
- minLength and maxLength
The minimum length for the string or array type parameter.
- Metadata
Metadata for the parameter can be any valid JSON object
- Allowed values
Value can only be one of these values.
- minValue and maxValue
Minimum and Maximum value for the int type parameter.
There are a total of seven types of parameters you can pass to ARM template:
- "parameterValueTypes": {
- "type": [
- "string",
- "boolean",
- "integer",
- "number",
- "object",
- "array",
- "null"
- ]},
Type = “SecureString” / “SecureObject”
If you have any secured data like password or secret key then use parameter type ‘SecureString’. If you pass any sensitive data in JSON format use parameter type ‘SecureObject’. The main reason for using type secure string is Template parameter with ‘SecureString’ or ‘SecureObject’ types cannot be read after resource deployment.
Deploy ARM Template using PowerShell
While deploying any Azure resources using ARM template, ARM template should be on your local machine drive or somewhere on external storage but should be accessible via URL.
Using PowerShell to Deploy ARM template you need to follow below steps,
Here in the below example, we will deploy Azure storage account using ARM template. This template has only one Parameter ‘StorageAccountType’.
- Log in to your Azure Account.
- Select subscription in which resource group you want to deploy resources. (If you have multiple subscriptions)
- Select-AzureRmSubscription -SubscriptionName <yourSubscriptionName>
- You can create new Resource Group or can use existing resource group.
- There are two ways to pass parameters to ARM template, first is you can directly pass an object of all parameters (as below) or create parameter file and pass parameter file path.
- $tempparam = @{
- "storageAccountType" = "Standard_LRS"
- }
New-AzureRmResourceGroupDeployment -Name "gla-dev1-d-rsg-demoarm" `
- ResourceGroupName "rsgmidserver" `
- TemplateUri "C:\AK\create_storage.json" `
- TemplateParameterObject $tempparam
The command ‘New-AzureRmResourceGroupDeployment’ is used to deploy Azure resources. There are a few parameters which are needed for this command to pass:
Name |
Deployment Name, this can be used to check different deployment details in azure portal or using PowerShell command. |
ResourceGroupName |
Name of the existing Resource group where you want to deploy resources. |
TemplateUri |
URI of your Azure ARM Template |
TemplateParameterObject |
An Object which contains all parameters of ARM template. |
TemplateParameterFile <optional> |
You can create parameter file and pass file path. |
Once you execute the above command it will show status ‘Deploying’ in ‘Deployment’ section of your resource group.
If deployment is successful it will return a complete message with Status and all input and output parameters like below.
Overview of Deployment
Now, if you check your resource group then it will have a new resource, ‘StorageAccount,’ in the resources list.
So this was a basic overview of ARM template. In the next article, we will see some advanced level features of ARM templates. Until then if you have any queries please add a comment here. Thank you all.
| | | | | | | | | |
Text-to-speech function is limited to 200 characters