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)
- π― What We Will Build
- π Prerequisites
- ποΈ Step 1 β Create Resource Group, App Service Plan & App Service
- π Step 2 β Create a Staging Slot
- π Step 3 β Deploy Blue & Green Apps
- π Step 4 β Slot Swapping (The Core of Blue-Green)
- π How to Swap Back
- π’ How Companies Do This in Real Life
- π Key Lessons
- π§Ή Cleanup
- π Final Thoughts
π§ What Is Blue-Green Deployment (Simple Explanation)
Imagine:
- Blue = current live version
- Green = new version
Users only see one version at a time.
You:
- Deploy the new version to Green
- Test it safely
- Swap Green β Production
- 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.

Leave a Reply