Q: Define a plan for blue/green deployment with rollback on Azure using Terraform and pipelines.
Production blueprint for implementing Blue/Green zero-downtime deployments with instant traffic rollback on Azure using Terraform, App Service deployment slots, and Azure DevOps.
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: Blue/Green deployment eliminates downtime by maintaining two identical production environments: Blue (active live traffic) and Green (staging/idle). Using Terraform to declare infrastructure and Azure App Service Deployment Slots or Azure Front Door to swap traffic, we achieve instant cutovers and sub-second rollbacks without redeploying code.
Declare Blue and Green Slots with Sticky Settings in Terraform
Provision Azure App Service with a dedicated staging slot alongside production. Crucially, configure database connection strings and environment flags as sticky_settings so that when slots swap, configuration stays bound to the slot.
# Terraform App Service with Deployment Slot
resource "aws_app_service" "api" { ... }
resource "azurerm_app_service_slot" "green" {
name = "staging"
app_service_name = azurerm_linux_web_app.app.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
app_service_plan_id = azurerm_service_plan.plan.id
}
resource "azurerm_linux_web_app_slot" "sticky_config" {
# Mark slot-specific environment settings
}
CI/CD Pipeline Deploys to Staging (Green) & Runs Warmup Probes
The Azure DevOps pipeline packages the build artifact and deploys it exclusively to the Green (staging) slot. Execute automated smoke tests and verify warm-up health checks while Blue continues handling 100% of public traffic.
# Azure DevOps Pipeline Step
- task: AzureWebAppDeployment@4
inputs:
azureSubscription: 'Azure-OIDC'
appName: 'app-prod-banking'
deployToSlotOrASE: true
slotName: 'staging'
package: '$(Pipeline.Workspace)/**/*.zip'
Zero-Downtime Slot Swap (Virtual IP Cutover)
Trigger an Azure slot swap. Azure manages the cutover cleanly at the reverse-proxy routing layer by redirecting DNS/VIP bindings: Green becomes Production and Blue becomes Staging with zero dropped TCP connections.
- task: AzureAppServiceManage@0
inputs:
azureSubscription: 'Azure-OIDC'
Action: 'Swap Slots'
WebAppName: 'app-prod-banking'
ResourceGroupName: 'rg-banking'
SourceSlot: 'staging'
SwapWithProduction: true
Instant Sub-Second Rollback Protocol
If post-swap telemetry reveals error spikes, trigger an immediate second slot swap. Because the previous stable Blue code is still alive in the staging slot, swapping back restores stability instantly without building or deploying.
- Use Terraform to declare primary and staging slots with sticky configuration parameters.
- Deploy new releases into the staging slot and execute automated health checks.
- Execute an Azure slot swap to redirect production traffic with zero downtime.
- Execute an instant reverse slot swap if error rates spike post-cutover.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Executing Terraform applies without inspecting '# forces replacement' warnings in plan output when configuring Blue/Green Deployment Instant Attributes that force resource replacement destroy and recreate critical infrastructure (databases, load balancers), inducing unannounced production outages.
- Omitting remote state locking mechanisms (DynamoDB or backend native locks) for shared state files when configuring Blue/Green Deployment Instant Without state locking, concurrent CI/CD pipeline executions or team applies create race conditions that corrupt the state file.
- Structuring entire enterprise cloud footprints into a single monolithic Terraform root module when configuring Blue/Green Deployment Instant Monolithic state files slow plan execution dramatically, increase blast radius during failed applies, and create severe pipeline locking contention.
- Failing to pin required_providers versions, causing breaking provider upgrades in CI pipelines when configuring Blue/Green Deployment Instant Unpinned provider versions automatically download breaking major upgrades during CI runs, introducing unexpected schema validation failures.