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


๐ 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 serverapp.get()
- Defines route handlers/health
- Required endpoint for Elastic Beanstalkapp.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:
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:
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
)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")
Restart Terminal
Close/reopen VS Code or PowerShell for changes to apply.Final Check
eb --version # Should now work
โก๏ธ Proceed to Initialization
Once verified, run:
eb init
Why this matters:
Ensures
eb
commands are available system-wideFixes common PATH issues on Windows
Takes <1 minute to implement
A1. First, install Python (required for EB CLI)
Follow prompts:
Select region (same as AWS CLI)
Create new application (e.g., "MyApp-Prod")
Choose platform (Node.js)
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:
Packages your application
Creates EC2 instance
Configures load balancer
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:
Swaps environment URLs
Route53 updates DNS (usually <60s)
Green now receives 100% traffic
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
Look for the
CNAME
field (e.g.,green-env.eba-xyz123.us-east-1.elasticbeanstalk.com
)
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:
Add automated tests to your pipeline
Configure CloudWatch alarms for monitoring
Implement canary deployments for gradual rollouts
Subscribe to my newsletter
Read articles from Joel Thompson directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
