my blog

$ cat azure-arm-templates/part-01.md

My First Azure ARM Templates: Deploying Web Apps

Why I Started Learning ARM Templates

Hello everyone! I’m Reza, a junior DevOps engineer. Today I want to share my first experience with Azure ARM templates. When I started my home lab, I was creating Azure resources manually through the portal. It was slow and I made mistakes. Then I learned about Infrastructure as Code (IaC) and decided to try ARM templates.

What is ARM Template?

ARM means Azure Resource Manager. ARM templates are JSON files that define what resources you want to create in Azure. Instead of clicking in the portal, you write code and Azure creates everything for you.

Think about it like a recipe. You write down all ingredients and steps, then Azure follows your recipe and makes the infrastructure.

My First ARM Template – Creating a Web App

Let me show you my first ARM template. This is the actual code from my GitHub repository:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "functions": [],
  "variables": {},
  "resources": [
    {
        "type": "Microsoft.Web/serverfarms",
        "apiVersion": "2022-03-01",
        "name": "reza787878",
        "location": "[resourceGroup().location]",
        "sku": {
                "name":"F1",
                "capacity":1
            },
            "properties":{
                "name":"reza787878"
            }
    },
    {
            "type": "Microsoft.Web/sites",
            "apiVersion": "2022-03-01",
            "name": "rezawebapp994848",
            "location": "[resourceGroup().location]",
            "properties":{
                "name":"rezawebapp994848",
                "serverFarmId":"[resourceId('Microsoft.Web/serverfarms','reza787878')]"
            },
            "dependsOn":[
                "[resourceId('Microsoft.Web/serverfarms','reza787878')]"
            ]
        }
  ],
  "outputs": {}
}

Understanding the Structure

Let me explain each part:

1. Schema and Version

"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0"

This tells Azure what type of template this is. Every ARM template needs this at the top.

2. App Service Plan (Server Farm)

{
    "type": "Microsoft.Web/serverfarms",
    "apiVersion": "2022-03-01",
    "name": "reza787878",
    "location": "[resourceGroup().location]",
    "sku": {
        "name":"F1",
        "capacity":1
    }
}

This creates the App Service Plan. Think of it like a server that will host your web app.

Important details:

  • type: Microsoft.Web/serverfarms – this is the resource type for App Service Plan
  • apiVersion: 2022-03-01 – Azure API version (very important!)
  • name: reza787878 – my App Service Plan name
  • location: [resourceGroup().location] – this is ARM function! It takes location from your resource group automatically
  • sku: F1 – this is FREE tier! Perfect for learning and testing

3. Web App

{
    "type": "Microsoft.Web/sites",
    "apiVersion": "2022-03-01",
    "name": "rezawebapp994848",
    "location": "[resourceGroup().location]",
    "properties":{
        "name":"rezawebapp994848",
        "serverFarmId":"[resourceId('Microsoft.Web/serverfarms','reza787878')]"
    },
    "dependsOn":[
        "[resourceId('Microsoft.Web/serverfarms','reza787878')]"
    ]
}

This creates the actual Web App.

Key points:

  • type: Microsoft.Web/sites – this is Web App resource type
  • serverFarmId: This connects the Web App to the App Service Plan. I use resourceId() function to get the ID
  • dependsOn: This is VERY important! It tells Azure: “Create the App Service Plan FIRST, then create the Web App”

The Magic of dependsOn

When I first wrote this template, I forgot to add dependsOn. Azure tried to create the Web App before the App Service Plan existed. The deployment failed!

"dependsOn":[
    "[resourceId('Microsoft.Web/serverfarms','reza787878')]"
]

This line saved me. It creates the correct order:

  1. First: App Service Plan
  2. Second: Web App

How I Deployed This Template

I used Azure CLI in my terminal:

# Create resource group first
az group create --name template-grp --location "North Europe"

# Deploy the template
az deployment group create \
  --resource-group template-grp \
  --template-file 01-AzureWebApp.json

After 2-3 minutes, everything was ready! I checked the Azure portal and saw my Web App running.

What I Learned

  1. ARM templates save time: I can deploy the same infrastructure again in 2 minutes
  2. Location function is smart: Using [resourceGroup().location] means I don’t need to write location twice
  3. Dependencies matter: Always use dependsOn when one resource needs another
  4. F1 tier is free: Great for learning without spending money
  5. resourceId() function: This connects resources together

My Mistakes and How I Fixed Them

Mistake 1: I used wrong API version first (2020-12-01) Fix: I checked Azure documentation and updated to 2022-03-01

Mistake 2: Name conflicts – Azure said “name already exists” Fix: I added numbers to make unique names like rezawebapp994848

$ ls -t root-access/ | head -3

Hello world! uncategorized 1 min Building a Job Board API, Part 1: The Foundation job-board-api · 1/1 16 min Part 13 – Kubernetes Autoscaling in Action: A FastAPI Microservice That Scales Itself home-lab · 13/13 6 min

Leave a comment

Your email address will not be published. Required fields are marked *