Deploying Azure Storage with ARM Templates.
Source: Dev.to
Introduction
Azure Resource Manager (ARM) provides a way to organize resources in Azure. With ARM you can define and deploy Azure resources as a single logical unit called a resource group, which can contain virtual machines, storage accounts, network interfaces, and more.
ARM templates are JSON files that define Infrastructure as Code (IaC). This guide shows how to deploy an Azure Storage Account using:
- an ARM template
- a PowerShell deployment script
- an Azure DevOps pipeline
Prerequisites
- Azure subscription
- Azure DevOps project with a Service Connection to the subscription
Repository layout
├── deploy.ps1 # PowerShell script (creates RG + deploys template)
├── azure-pipelines.yml # CI/CD pipeline definition
└── storageaccount.json # ARM template definition
PowerShell deployment script (deploy.ps1)
param(
[string]$ResourceGroupName,
[string]$StorageAccountName,
[string]$Location,
[string]$Sku = "Standard_LRS"
)
$scriptDir = $PSScriptRoot
$template = Join-Path $scriptDir "storageaccount.json"
Write-Host "Checking if resource group '$ResourceGroupName' already exists..."
Start-Sleep -Seconds 2
$exists = (az group exists --name $ResourceGroupName) -eq 'true'
if (-not $exists) {
Write-Host "Resource group '$ResourceGroupName' does not exist. Creating..."
az group create --name $ResourceGroupName --location $Location | Out-Null
if ($LASTEXITCODE -ne 0) { throw "Failed to create resource group." }
} else {
Write-Host "Resource group '$ResourceGroupName' already exists."
}
Write-Host "Deploying Azure ARM template.."
az deployment group create `
--resource-group $ResourceGroupName `
--template-file $template `
--parameters "storageAccountName=$StorageAccountName" "sku=$Sku" "location=$Location"
if ($LASTEXITCODE -ne 0) {
Write-Error "ARM deployment failed."
return $false
}
Write-Host "Deployment successful by Alvaro! 🚀"
Azure Pipelines definition (azure-pipelines.yml)
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
resourceGroupName: 'Asgard'
storageAccountName: 'storageaccountasgard'
location: 'West Europe'
sku: 'Standard_LRS'
steps:
- task: AzureCLI@2
displayName: 'Deploy Storage Account ARM Template'
inputs:
azureSubscription: 'Marty-MacFly' # Service Connection name in Azure DevOps
ScriptType: pscore
ScriptPath: '$(System.DefaultWorkingDirectory)/deploy.ps1'
ScriptArguments: >-
-ResourceGroupName "$(resourceGroupName)"
-StorageAccountName "$(storageAccountName)"
-Location "$(location)"
-Sku "$(sku)"
Running the pipeline
- Commit the files to the
mainbranch of your Azure DevOps repository. - The pipeline triggers automatically (or run it manually).
- The PowerShell script creates the resource group if it does not exist and deploys the
storageaccount.jsonARM template.
Verification
After the pipeline completes, navigate to the Azure portal and verify that the new Storage Account (storageaccountasgard) has been created in the West Europe region.