Ever feel like you’re repeating yourself in code? Like copy-pasting the same complicated string over and over?

Well, ARM templates let you stop doing that — using variables!

This time, I explored how to simplify my deployment template by storing expressions in variables. Think of it like giving a nickname to a long, awkward phrase, so you don’t have to say the whole thing every time. Let’s get into it 👇


🧰 Before You Start

If you’ve been following my earlier posts, you’re probably set up. But just to be sure, here’s what you’ll need:

✅ Azure CLI installed and ready

✅ Logged in with az login

✅ Subscription selected with:

az account set --subscription "Your Subscription Name"

✅ A resource group to deploy into:

az group create --name RG1 --location "East US"

🛠️ Task 1 – Add a Variable to Your Template

We’re going to rewrite our template to make it smarter and less repetitive.

✍️ Here’s the setup:

Open your azuredeploy.json in VS Code (or create a new one), and paste this:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    }
  },
  "functions": [],
  "variables": {
    "uniqueStorageName": "[toLower(concat(parameters('storagePrefix'), uniqueString(resourceGroup().id)))]"
  },
  "resources": [
    {
      "name": "[variables('uniqueStorageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "tags": {
        "displayName": "[variables('uniqueStorageName')]"
      },
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "[parameters('storageSKU')]"
      }
    }
  ],
  "outputs": {}
}

💡 Why Use a Variable?

Imagine if every time you said "supercalifragilisticexpialidocious" you had to say the whole word — not fun, right?

Now imagine you could just say "S-word" and everyone knew what you meant. That’s what ARM variables do!

Instead of repeating that long storage name expression over and over (toLower(concat(...))), we define it once as a variable and reuse it throughout the template. Cleaner. Smarter. Easier to update.


🚀 Task 2 – Deploy It!

Head to your terminal and run:

templateFile="azuredeploy.json"
today=$(date +'%d-%b-%Y')
DeploymentName="addVariable-$today"

az deployment group create \
  --resource-group RG1 \
  --name $DeploymentName \
  --template-file $templateFile \
  --parameters storagePrefix=mybob

🔑 You can change mybob to any 3–11 character prefix. The template will automatically add a hash after it to make sure it’s globally unique — no naming collisions here!


🔍 Check It Out in Azure

  1. Head over to Azure Portal
  2. Navigate to Resource groups > RG1
  3. Open the latest Deployment (something like addVariable-08-Apr-2025)
  4. You’ll see a Storage Account named like: mybobabc123

Boom. It worked! 🎉


🧠 What I Learned

Concept Real-World Meaning
Parameters Like asking the user to "fill in the blanks" when running the template
Variables Like using a nickname for a long phrase — define once, reuse many times
Expressions Like LEGO blocks — you snap together pieces like concat() and toLower() to build logic
Azure CLI The power tool to run your template from the terminal and deploy resources

✅ In Short

ARM variables = less copy-pasting and cleaner templates.

The more you use variables, the easier it is to maintain and scale your templates over time.


🚀 What’s Next?

Wanna follow my Azure learning journey? Stick around — I’m sharing it all, wins and stumbles included 😄

You can find me on LinkedIn — drop me a message and just say hi 👋.

Would love to hear what you’re working on or learning too!