DEV Community

Alvaro Salinas
Alvaro Salinas

Posted on

Deploying Azure Storage with ARM Templates.

Introduction

Azure Resource Manager (ARM) is a service that 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. This resource group can contain various Azure resources such as virtual machines, storage accounts, and network interfaces.

ARM Templates are blocks of code that define Infrastructure as Code (IaC) using JSON syntax. All resources and their properties are defined in this template.

I’ve been experimenting with different Azure services to practice and learn a bit more. I've done this small guide to show how to deploy an Azure resource using an ARM template, a PowerShell script, and an Azure DevOps pipeline.

For this example, I used a template to create a Storage Account.

Prerequisites

  • Azure subscription
  • Azure DevOps
  • Azure Service Connection

Repository structure

├── deploy.ps1              # PowerShell script to handle logic (Create RG + Deploy)
├── azure-pipelines.yml     # CI/CD Pipeline definition
└── storageaccount.json     # ARM Template definition
Enter fullscreen mode Exit fullscreen mode

Step 1: Copy this script on your repository.

    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! 🚀"

Enter fullscreen mode Exit fullscreen mode

Step 2: Copy the pipeline.yaml on the repository.

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' # Work Service Connection name in Azure DevOps
    ScriptType: pscore
    ScriptPath: '$(System.DefaultWorkingDirectory)/deploy.ps1'
    ScriptArguments: >-
      -ResourceGroupName "$(resourceGroupName)"
      -StorageAccountName "$(storageAccountName)"
      -Location "$(location)"
      -Sku "$(sku)"
Enter fullscreen mode Exit fullscreen mode

Step 3: Run the pipeline

Run the pipeline in Azure DevOps.

Step 4: Validate Deployment

Check out in Azure the new Storage Account created.

Top comments (0)