7 – πŸš€ Azure App Service with Terraform β€” Blue-Green Deployment Step-by-Step

Blue-green deployment is a release strategy that lets you ship new versions of your app with near-zero downtime and low risk. Instead of updating your live app directly, you run two environments side-by-side and switch traffic between them.

In this guide, I’ll walk you through how I implemented blue-green deployment on Azure using Terraform and simple HTML apps. This is written for beginners and focuses on understanding why we do each step β€” not just what to type.

Table of Contents


🧠 What Is Blue-Green Deployment (Simple Explanation)

Imagine:

  • Blue = current live version
  • Green = new version

Users only see one version at a time.

You:

  1. Deploy the new version to Green
  2. Test it safely
  3. Swap Green β†’ Production
  4. Instantly roll back if needed

No downtime. No risky in-place updates.

Azure App Service deployment slots make this easy.


🎯 What We Will Build

We will:

βœ… Create Azure infrastructure with Terraform
βœ… Create a staging slot
βœ… Deploy two app versions (Blue & Green)
βœ… Swap them using Terraform
βœ… Understand how real companies do this


πŸ“Œ Prerequisites

You should have:

  • Azure subscription
  • Terraform (by HashiCorp) installed
  • Azure CLI installed
  • Logged in using az login
  • Basic Terraform knowledge

πŸ—οΈ Step 1 β€” Create Resource Group, App Service Plan & App Service

Why these resources?

Resource Group
Container that holds everything.

App Service Plan
Defines pricing tier, performance, and features.
Deployment slots require Standard tier or higher.

App Service
Your actual web app.


rg.tf

resource "azurerm_resource_group" "rg" {
  name = "rgminipro87897"
  location = "Central US"
}

asplan.tf

resource "azurerm_app_service_plan" "asp" {
  name = "aspminipro8972"
  location = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  sku {
    tier = "Standard"
    size = "S1"
  }
}

πŸ‘‰ Why S1?
Slots are unavailable in Free/Basic tiers.


appservice.tf

resource "azurerm_app_service" "as" {
  name = "appserviceminipro87897987233"
  location = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.asp.id
}

β–Ά Run Terraform

terraform init
terraform apply

βœ… Verify

Open the app URL in a browser.
You’ll see a default Azure page β€” that means infrastructure works.


πŸ” Step 2 β€” Create a Staging Slot

A deployment slot is a second live version of your app with its own URL.

Think of it as a testing environment running inside the same App Service.


slot.tf

resource "azurerm_app_service_slot" "slot" {
  name = "slotstagingminipro78623"
  location = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.asp.id
  app_service_name = azurerm_app_service.as.name
}

β–Ά Apply

terraform apply

βœ… Verify in Azure

You will see:

  • Production slot
  • Staging slot
  • Traffic: 100% production, 0% staging

πŸ‘‰ This is normal β€” staging is for testing.


🌈 Step 3 β€” Deploy Blue & Green Apps

Terraform builds infrastructure.
We use Azure CLI to deploy app code.

(That’s also how real companies separate infra and app deployments.)


Blue Version (Production)

Create:

<h1 style="background:blue;color:white;">BLUE VERSION</h1>

Zip with index.html at root β†’ blueapp.zip


Green Version (Staging)

<h1 style="background:green;color:white;">GREEN VERSION</h1>

Zip β†’ greenapp.zip


Deploy Using Microsoft Azure CLI

Blue β†’ Production

az webapp deploy \
  --resource-group rgminipro87897 \
  --name appserviceminipro87897987233 \
  --src-path blueapp.zip \
  --type zip

Green β†’ Staging

az webapp deploy \
  --resource-group rgminipro87897 \
  --name appserviceminipro87897987233 \
  --slot slotstagingminipro78623 \
  --src-path greenapp.zip \
  --type zip

βœ… Verify

Production URL β†’ Blue
Staging URL β†’ Green

Perfect setup!


πŸ”„ Step 4 β€” Slot Swapping (The Core of Blue-Green)

Now we swap environments.


swap.tf

resource "azurerm_web_app_active_slot" "swap" {
  slot_id = azurerm_app_service_slot.slot.id
}

β–Ά Apply

terraform apply

πŸŽ‰ Result

Now:

Production β†’ Green
Staging β†’ Blue

You just performed a blue-green deployment!


πŸ”™ How to Swap Back

Terraform won’t auto-reverse swaps.

Use Azure CLI:

az webapp deployment slot swap \
  --resource-group rgminipro87897 \
  --name appserviceminipro87897987233 \
  --slot slotstagingminipro78623 \
  --target-slot production

🏒 How Companies Do This in Real Life

In real projects:

Terraform
β†’ Creates infrastructure

CI/CD pipelines
β†’ Deploy apps & swap slots

Why?

Because swapping affects real users and needs:

  • Testing
  • Approval
  • Monitoring
  • Rollback strategy

Common tools:

  • GitHub Actions
  • Azure DevOps
  • Jenkins

πŸ“Œ Key Lessons

You learned:

βœ” App Service basics
βœ” Deployment slots
βœ” Blue-green strategy
βœ” Terraform infrastructure setup
βœ” CLI deployment
βœ” Slot swapping logic
βœ” Real-world DevOps workflow


🧹 Cleanup

Avoid charges:

terraform destroy

πŸš€ Final Thoughts

Blue-green deployment is a core DevOps skill.
Mastering it early gives you a big advantage.

This small demo mirrors how production systems reduce risk during releases.

Comments

Leave a Reply

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

TechMilestoneHub

Build Skills, Unlock Milestones

This is a test – edited from front page