Zero-Downtime Deployments: AWS Blue/Green Strategy with Elastic Beanstalk

Joel ThompsonJoel Thompson
5 min read

๐ŸŒŸ Introduction to Blue/Green Deployments

In today's always-on digital landscape, application downtime during deployments translates directly to lost revenue and damaged user trust. Traditional deployment methods force teams to choose between stability and innovation. AWS Elastic Beanstalk's Blue/Green deployment strategy eliminates this compromise by maintaining two identical production environments:

๐Ÿ”ต Blue Environment (Current Production)

  • Handles 100% of live traffic

  • Stable, known-good version

๐ŸŸข Green Environment (New Version)

  • Exact replica with your updates

  • Tested before receiving traffic

Key Benefits:

  • ๐Ÿ•’ Zero downtime during deployments

  • ๐Ÿ”„ Instant rollback capability

  • ๐Ÿงช Real-world testing before cutover

  • ๐Ÿ“Š Performance comparison between versions

๐Ÿ›  Phase 1: Project Setup (VS Code)

Step 1.1 - Create Project Structure

# Create project folder
mkdir MyApp
cd MyApp

# Initialize Node.js project
npm init -y       # Creates package.json with default values
npm install express  # Adds Express web framework

# Create organized source structure
mkdir src         # Best practice for code organization
New-Item -Path src/index.js -Type File  # Creates empty JavaScript file

Why This Structure?

  • Keeps source code separate from configuration

  • Follows Node.js best practices

  • Makes it easy to add controllers, routes later

  • Clear separation for deployment

Step 1.2 - Build Basic Application

Edit src/index.js:

const express = require('express');
const app = express();

// Main endpoint - returns simple message
app.get('/', (req, res) => {
  res.send('MyApp Blue/Green Deployment');
});

// Health check endpoint - crucial for AWS
app.get('/health', (req, res) => {
  res.send('OK');
});

// Start server on port 3000
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Key Components:

  • express() - Creates web server

  • app.get() - Defines route handlers

  • /health - Required endpoint for Elastic Beanstalk

  • app.listen() - Starts the server

Step 1.3 - Test Locally

# First navigate to your project root:
cd ..

# Then run with the full path:
node src/index.js

Verification:

  1. Open browser to http://localhost:3000

    • Should show "MyApp Blue/Green Deployment"

Press Ctrl+C to stop server

๐Ÿ”ต Phase 2: Deploy Blue Environment

Step 2.1 - AWS CLI Setup

aws configure

Enter your:

  • AWS Access Key ID

  • Secret Access Key

  • Default region (e.g., us-east-1)

  • Output format (json)

Why? Authenticates your CLI with AWS services.

Step 2.2 - Initialize EB CLI

eb init

๐Ÿ”ง Troubleshooting EB CLI Installation

If you get 'eb' is not recognized error:

  1. Find EB CLI Location

     dir "$env:USERPROFILE\AppData\Roaming\Python\Python*\Scripts\eb.exe" -Recurse
    

    Look for the path containing eb.exe (e.g., Python313\Scripts)

  2. Add to PATH

    ```powershell

    Replace Python313 with your version

    $ebPath = "$env:USERPROFILE\AppData\Roaming\Python\Python313\Scripts"


3. **Verify Installation**

    ```powershell
    & "$env:USERPROFILE\AppData\Roaming\Python\Python313\Scripts\eb" --version
    # Should return version (e.g., "EB CLI 3.25")
  1. Restart Terminal
    Close/reopen VS Code or PowerShell for changes to apply.

  2. Final Check

     eb --version  # Should now work
    

โžก๏ธ Proceed to Initialization

Once verified, run:

eb init

Why this matters:

  • Ensures eb commands are available system-wide

  • Fixes common PATH issues on Windows

  • Takes <1 minute to implement

A1. First, install Python (required for EB CLI)

Follow prompts:

  1. Select region (same as AWS CLI)

  2. Create new application (e.g., "MyApp-Prod")

  3. Choose platform (Node.js)

  4. Set up SSH (recommended for debugging)

Creates: .elasticbeanstalk/config.yml with your settings

Step 2.3 - Create Blue Environment

eb create blue-env `
  --instance_type t2.micro `  # Low-cost instance
  --scale 1 `                # Single instance
  --envvars NODE_ENV=production

Deployment Process:

  1. Packages your application

  2. Creates EC2 instance

  3. Configures load balancer

  4. Sets up monitoring

Verify Deployment:

eb status

Wait for "Health: Green" (5-10 minutes)

Get Your Environment URL

Run this command to confirm the exact URL:

Look for the CNAME field (it will look like):
blue-env.eba-pp2cpu8x.us-east-1.elasticbeanstalk.com

๐ŸŸข Phase 3: Prepare Green Environment

Step 3.1 - Clone Blue Environment

eb clone blue-env -n green-env

What Happens:

  • Creates identical environment

  • Copies all configuration

  • Maintains separate resources

Step 3.2 - Deploy Updates to Green

eb use green-env  # Switch context to green
eb deploy         # Deploy latest code

Best Practice: Test thoroughly at this stage before traffic switch.

๐Ÿ”„ Phase 4: The Traffic Switch

Step 4.1 - Execute Blue/Green Swap

eb swap blue-env --destination_name green-env

What Happens:

  1. Swaps environment URLs

  2. Route53 updates DNS (usually <60s)

  3. Green now receives 100% traffic

  4. Blue remains running but idle

    1. Get the Green Environment URL

    Run this command to find your Green environment's URL:

    powershell

     eb status green-env
    

Step 4.2 - Monitor Traffic Shift

# Get load balancer ARN
$lbArn = aws elasticbeanstalk describe-environment-resources `
  --environment-name blue-env `
  --query "EnvironmentResources.LoadBalancers[0].Name" `
  --output text

# Monitor request metrics (last 10 minutes)
aws cloudwatch get-metric-statistics `
  --namespace "AWS/ApplicationELB" `
  --metric-name "RequestCount" `
  --dimensions Name=LoadBalancer,Value=$lbArn `
  --start-time (Get-Date).AddMinutes(-10).ToString("yyyy-MM-ddTHH:mm:ss") `
  --end-time (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss") `
  --period 60 `
  --statistics "Sum" `
  --output json | ConvertFrom-Json

๐ŸŽฏ Final Architecture

MyApp/
โ”œโ”€โ”€ src/                      # All application code
โ”‚   โ””โ”€โ”€ index.js              # Main entry point
โ”œโ”€โ”€ .ebextensions/            # Elastic Beanstalk configs
โ”‚   โ””โ”€โ”€ nodecommand.config    # Ensures npm start is used
โ””โ”€โ”€ package.json              # Node.js project metadata

๐Ÿ Conclusion

You've Successfully Implemented: โœ” Zero-downtime deployment strategy
โœ” Isolated testing environment
โœ” Automated CI/CD pipeline
โœ” Professional-grade AWS infrastructure

Next Steps to Enhance:

  1. Add automated tests to your pipeline

  2. Configure CloudWatch alarms for monitoring

  3. Implement canary deployments for gradual rollouts

0
Subscribe to my newsletter

Read articles from Joel Thompson directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Joel Thompson
Joel Thompson