Tried out Azure Resource Management templates this evening, to learn more about the “Infrastructure as Code” topic. A Resource Manager template is a JSON file that defines the resources to be deployed together through a declarative syntax. A templates can then be deployed via code. I started simple by creating a Resource Group in Azure first and then deployed a Storage Account with Azure PowerShell and an ARM template. This is what I did:
Used PowerShell for creating the resource group and to deploy the ARM template.
Started with logging into my Azure Portal with the following command:
# This opens a dialog box, where you type your usernameand password. Connect-AzureRMAccount # If you have multiple subscriptions, I recommend to setthe correct subscription context, so your are making changes to the correct subscription. # To get all subscriptions you have access to, run: Get-AzureRmContext # To set the subscription context, copy thesubscriptionID from the desired subscription # from the previous command and added it to the commandbelow Set-AzureRmContext -SubscriptionId "<Fill insubscriptionId>"
Then created the script for creating a resource group.
# Creating a resource group $RgName = "iac-basic-arm" $Location = "West Europe" # By using -Force you avoid a dialog box that asks you if you want to create the resource group New-AzureRmResourceGroup -Name $rgName -Location$location -Force # If you want to delete the resource group you cansimply run. Remove-AzureRmResourceGroup -Name "iac-basic-arm" -Force
When running the script, you will see something like this in the Azure Portal, under Resource Groups:
Also experimented with Tag creation when creating a Resource Group, by modifying the above script a bit. If you run the following script after you created the Resource Group above, it will only update the Resource Group with the added changes.
# Creating a resource group$RgName = "iac-basic-arm"$Location = "West Europe" # Creating a hashtable with Key=>Value pairs $Tag = @{Purpose = "Demo"; Status = "ToBeDeleted"} # Then adding a little modification here, by adding the Tag parameter New-AzureRmResourceGroup -Name $rgName -Location $location -Tag $Tag -Force # Same if you want to delete it, you can run the following command Remove-AzureRmResourceGroup -Name "iac-basic-arm"
After running the script included with tag creation, you can click on the Resource Group name, iac-basic-arm
. To take a look on the Overview pane. Below is the result after the first creation
Then the result after running the script with Tag creation. Just remember to refresh the page. If you don’t see it right away. You probably also click on Resource Groups in the breadcrumbs path, then then click on the iac-basic-arm
group again, to see the updates. I worked for me.
Now, that the Resource Group is created, let’s look at the ARM template. To create this I used Visual Studio Code, I also installed the plugin Azure ARM Template Helper, to get help with writing the template.
What this does is to create an Azure Storage Account
{ "$schema": "http://schema.management.azure.comschemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "storageAccountType": { "type": "string", "defaultValue": "Standard_LRS", "allowedValues": [ "Standard_LRS", "Standard_GRS", "Standard_ZRS", "Premium_LRS" ], "metadata": { "description": "Storage Account Type" } }, "name": { "type": "string", "defaultValue": "iac", "metadata": { "description": "The name of the storageaccount" } } }, "variables": { "prefix": "[concat(parameters('name'),uniquestring(substring(resourceGroup().id, 0, 2))]", "storageType": "[split(parameter('storageAccountType'), '_')[0]]", "storageAccountName": "[toLower(concat(variable('prefix'), variables('storageType')))]" }, "resources": [{ "tags": { "Purpose": "Demo", "Status": "ToBeDeleted" }, "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')]" } } }
To deploy the template you can run the following command:
$RgName = "iac-basic-arm" New-AzureRmResourceGroupDeployment -Name 'IaCDemoDeployment' -ResourceGroupName $Rgname-TemplateFile '</your/path/to/the/template/file.json>' -Verbose # If you get an error during deployment, you can look upthe Error CorrelationID, to # get more detail of what caused the error. NOTE that ifyou run with -Verbose you might not get the CorrelationID Get-AzureRmLog -CorrelationId'428b783d-529c-4d89-8123-8e58974f1669' -DetailedOutput
When running the script, it may look something like this, if it runs successfully.
Now lets see what has changed in the Azure Portal. Heey! After refreshing the page you can see that the new Storage Account has been deployed successfully.
Cool to see what some cmdlets can do.