adaptive.run TECH BLOG

Cloud can be tricky sometimes. Find out what scenarios we've ran into that are worth being mentioned and explained.

Deploying Azure ARM Templates Using PowerShell: A Step-by-Step Guide

Level: 200
Publishing date: 12-Mar-2024
Author: Catalin Popa

Introduction

Azure Resource Manager (ARM) templates are instrumental in automating the deployment of resources within the Azure cloud environment. Leveraging PowerShell commands for deploying ARM templates provides a streamlined and efficient way to manage infrastructure. In this article, we will walk through the process of deploying an ARM template using PowerShell, using an example template that creates a Virtual Machine (VM) and a Virtual Network (VNet). We will then showcase the PowerShell commands needed to deploy this template into a newly created resource group.


Step 1: Create an ARM Template for VM and VNet Deployment
Here is a simplified example ARM template that deploys a VM and a VNet. Save this template in a file named azuredeploy.json:

JSON:


{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-06-01",
"name": "MyVnet",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": ["10.0.0.0/16"]
}
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2020-06-01",
"name": "MyVM",
"location": "[resourceGroup().location]",
"dependsOn": ["MyVnet"],
"properties": {
"hardwareProfile": {
"vmSize": "Standard_DS1_v2"
},
"osProfile": {
"computerName": "MyVM",
"adminUsername": "adminUser",
"adminPassword": "yourAdminPassword"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2019-Datacenter",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', 'MyVMNic')]"
}
]
}
}
}
]

Ensure you replace "yourAdminPassword" with a secure password.

Step 2: Deploy the ARM Template Using PowerShell

Now, let's deploy the ARM template using PowerShell commands. Open PowerShell and run the following commands:

PowerShell:

# Log in to Azure (if not already logged in)
Connect-AzAccount

# Create a resource group (if it doesn't exist)
New-AzResourceGroup -Name „YourResourceGroupName” -Location „YourRegion”

# Deploy the ARM template
New-AzResourceGroupDeployment -ResourceGroupName „YourResourceGroupName” -Mode incremental -TemplateFile azuredeploy.json  

adaptive.run

Transform your business.
Run adaptive.

Contact

Phone: +40 73 523 0005
Email: hello@adaptive.run

© Copyright  2019-2024 adaptive.run- All Rights Reserved