Document

SUBSCRIBE TO GET FULL ACCESS TO THE E-BOOKS FOR FREE 🎁SUBSCRIBE NOW

Professional Dropdown with Icon

SUBSCRIBE NOW TO GET FREE ACCESS TO EBOOKS

Azure - ARM Templates

Azure – ARM Templates

 To implement infrastructure as code for your Azure solutions, use Azure Resource Manager templates (ARM templates). The template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it. In the template, you specify the resources to deploy and the properties for those resources.

ARM Template For Storage Account

Step 1: Search for the service Template Custom Deployment.

Step 2: select 


Step 3: It will open the Edit Template and Click on 

Step 4: Select a resource –> storage account and provide some unique storage account name and click on Save.

Step 5: Select the subscription and resource group and click on Review + Create. And a storage account will be created.


ARM Template for creating Virtual Machine using Powershell.

Step 1: Search for the service Template Custom Deployment.

Step 2: Select Create Windows VM 

Step 3: Enter the required values like subscription, Resource Group, username, password etc.

Step 4: Download the template.json file and Parametersfile.json(remove the code which requires some dynamic properties) by clicking on Edit Template and Edit ParameterFile button.

Step 5: Start the cloud shell and upload the above JSON files to the cloud shell.

Step 6: Run the below PowerShell command to create VM

az deployment group create –resource-group MyRG –template-file template.json –parameters parametersFile.json

Step 7: Verify the VM is created.

Nested ARM Templates

The main benefit of the Nested template is that in a JSON file you can create resources in multiple Resource groups.

In the below example, 2 storage accounts are created in different resource groups.

Step 1: Search for the service Template Custom Deployment.

Step 2: select 


Step 3: It will open the Edit Template and replace with the below code

{
  “$schema”“https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#”,
  “contentVersion”“1.0.0.0”,
  “parameters”: {
    “innerResourceGroup”: {
      “type”“string”
    },
    “innerSubscriptionID”: {
      “type”“string”,
      “defaultValue”“”
    }
  },
  “resources”: [
    {
      “type”“Microsoft.Storage/storageAccounts”,
      “apiVersion”“2019-06-01”,
      “name”“demostoreprimary1000”,
      “location”“East US”,
      “sku”:{
        “name”“Standard_LRS”
      },
      “kind”“Storage”,
      “properties”: {
      }
    },
    {
      “type”“Microsoft.Resources/deployments”,
      “apiVersion”“2019-10-01”,
      “name”“nestedTemplate”,
      “resourceGroup”“[parameters(‘innerResourceGroup’)]”,
      “subscriptionId”“[parameters(‘innerSubscriptionID’)]”,
      “properties”: {
      “mode”“Incremental”,
      “template”: {
          “$schema”“https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#”,
          “contentVersion”“1.0.0.0”,
          “parameters”: {},
          “variables”: {},
          “resources”: [
          {
            “type”“Microsoft.Storage/storageAccounts”,
            “apiVersion”“2019-06-01”,
            “name”“demostoresecondary1000”,
            “location”“Central US”,
            “sku”:{
              “name”“Standard_LRS”
            },
            “kind”“Storage”,
            “properties”: {
            }
          }
          ]
      },
      “parameters”: {}
      }
    }
  ]
}

Step 4: Click on Save and then provide the subscription and resource group for the main storage account. Also, provide an inner storage account subscription id and resource group.

Step 5: Click on Review + Create. And storage accounts will be created.


ARM Template with Key Vault.

Let’s store the password in a key vault’s secret and refer to the ARM Template.

1. Create a key vault in the Azure portal

2. Add a secret (Generate/Import option) with the password value.

3. Goto key Vault —>Properties and select the resource id value which is something like below.

/subscriptions/ec3fb11f-9a3e-4457-a370-5105e306c58a/resourceGroups/MYRG/providers/Microsoft.KeyVault/vaults/keyvault1239

4. Select Key Vault –> Access Policies –> check Azure Resource Manager for template deployment

5. Use the default template.json file and change the Parameter file as below

{
  “$schema”: “https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#”,
  “contentVersion”: “1.0.0.0”,
  “parameters”: {
    “adminUsername”: {
      “value”: “raman”
    },
    “adminPassword”: {
      “reference”: {
          “keyVault”: {
          “id”: “/subscriptions/ec3fb11f-9a3e-4457-a370-5105e306c58a/resourceGroups/MYRG/providers/Microsoft.KeyVault/vaults/keyvault1239”
          },
          “secretName”: “pass2”
        }
    },
   
    “publicIpName”: {
      “value”: “myPublicIP”
    },
    “publicIPAllocationMethod”: {
      “value”: “Dynamic”
    },
    “publicIpSku”: {
      “value”: “Basic”
    },
    “OSVersion”: {
      “value”: “2019-datacenter-gensecond”
    },
    “vmSize”: {
      “value”: “Standard_D2s_v3”
    },
    
    “vmName”: {
      “value”: “simple-vm”
    }
  }
}

6. Upload it to the cloud shell and Run the command

az deployment group create –resource-group MyRG –template-file template.json –parameters parametersFile.json

7. The virtual machine should be created with the defined secret password.

 

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *