• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar
  • Home
  • Projects
  • Products
  • Themes
  • Tools
  • Request for Quote

Vengala Vinay

Having 12+ Years of Experience in Software Development

  • Home
  • WordPress
  • PHP
    • Codeigniter
  • Django
  • Magento
  • Selenium
  • Server
Home » Zero-Downtime Blue-Green Deployment Pipelines for Magento 2 Applications on AWS

Zero-Downtime Blue-Green Deployment Pipelines for Magento 2 Applications on AWS

Understanding Blue-Green Deployments for Magento 2

Zero-downtime deployments are critical for e-commerce platforms like Magento 2, where even brief outages can result in significant revenue loss. Blue-Green deployment is a strategy that minimizes risk and downtime by running two identical production environments, “Blue” and “Green.” At any given time, one environment (e.g., Blue) is live and serving production traffic, while the other (Green) is idle. The new release is deployed to the idle environment (Green). Once tested, traffic is switched from Blue to Green, making Green the new live environment. This allows for rapid rollback by simply switching traffic back to the original Blue environment if issues arise.

AWS Infrastructure for Blue-Green Magento 2

A robust AWS setup for this strategy typically involves several key components:

  • Elastic Load Balancing (ELB): An Application Load Balancer (ALB) is ideal for routing traffic. It can manage listener rules and target groups, which are central to switching traffic between Blue and Green environments.
  • Auto Scaling Groups (ASGs): Two distinct ASGs, one for the Blue environment and one for the Green environment, will manage the EC2 instances running your Magento 2 application.
  • EC2 Instances: These will host your Magento 2 application code, web server (Nginx/Apache), PHP-FPM, and any other necessary services.
  • Shared Database: A single, highly available RDS instance (e.g., Aurora MySQL or Multi-AZ RDS MySQL) is crucial. Magento 2’s architecture requires a consistent database state across both environments.
  • Shared Cache/Session Storage: ElastiCache for Redis or Memcached should be shared between both environments to maintain session consistency and caching performance.
  • Shared Media Storage: AWS S3 with CloudFront for CDN is the standard for Magento 2 media files, ensuring both environments access the same assets.
  • Deployment Pipeline: AWS CodePipeline, CodeBuild, and CodeDeploy (or a similar CI/CD tool like Jenkins, GitLab CI) orchestrates the deployment process.

Setting Up the Environments

We’ll assume a base Magento 2 installation is already running on AWS. The core task is to duplicate this setup for the second environment and configure the load balancer for traffic switching.

1. Duplicate the Application Stack

The most straightforward way to duplicate the application stack is by creating a new Auto Scaling Group and launching configuration (or launch template) that mirrors the existing “Blue” environment. This new group will represent the “Green” environment.

Key considerations during duplication:

  • AMI: Use the same Amazon Machine Image (AMI) for both ASGs. If you’re using Packer to build AMIs, ensure it’s updated with the latest stable Magento code.
  • User Data/Cloud-Init: Ensure user data scripts correctly configure the web server, PHP-FPM, and any other services, pointing to the shared database and cache.
  • Environment Variables: Use AWS Systems Manager Parameter Store or Secrets Manager to store sensitive configurations (database credentials, API keys) and retrieve them via user data or application startup scripts.
  • Magento Configuration: Ensure Magento’s app/etc/env.php is correctly configured to point to the shared database and cache. This is often managed via environment variables injected during instance launch.

2. Configure Application Load Balancer (ALB)

The ALB is the traffic director. We’ll use two target groups, one for each environment, and a listener rule that can be dynamically updated.

Steps:

  • Create two Target Groups: magento-blue-tg and magento-green-tg. Both should point to the respective Auto Scaling Groups and use the appropriate health check path (e.g., /healthcheck.php or a custom Magento health check endpoint).
  • Configure the ALB Listener (typically on port 443 for HTTPS):
    • The default rule should forward traffic to the magento-blue-tg.
    • Add a second rule with a higher priority (e.g., priority 1) that forwards traffic to the magento-green-tg. This rule will be activated during a deployment.

The health check is critical. A simple healthcheck.php file in the Magento root that checks database connectivity and cache connectivity is a good start. For more robust checks, consider a custom script that queries Magento’s internal status.

Automating the Deployment Pipeline

AWS CodePipeline is a natural fit for orchestrating this process. A typical pipeline would look like this:

1. Source Stage

Connect to your Git repository (e.g., GitHub, AWS CodeCommit). Any push to the main branch triggers the pipeline.

2. Build Stage (AWS CodeBuild)

This stage is responsible for preparing the deployment artifact. For Magento 2, this typically involves:

  • Cloning the repository.
  • Running Composer install/update.
  • Compiling Magento DI (bin/magento setup:di:compile).
  • Static content deployment (bin/magento setup:static-content:deploy -f en_US en_GB).
  • Database schema/data patches application (bin/magento setup:upgrade).
  • Clearing cache (bin/magento cache:clean, bin/magento cache:flush).
  • Packaging the output into a deployable artifact (e.g., a ZIP file).

The buildspec.yml for CodeBuild would look something like this:

version: 0.2

phases:
  install:
    runtime-versions:
      php: 8.1 # Or your Magento-compatible PHP version
    commands:
      - echo "Installing dependencies..."
      - composer install --no-dev --optimize-autoloader
  build:
    commands:
      - echo "Compiling DI..."
      - bin/magento setup:di:compile
      - echo "Deploying static content..."
      - bin/magento setup:static-content:deploy -f en_US en_GB # Add your locales
      - echo "Running Magento setup:upgrade..."
      - bin/magento setup:upgrade
      - echo "Clearing cache..."
      - bin/magento cache:clean
      - bin/magento cache:flush
  post_build:
    commands:
      - echo "Packaging artifact..."
      - zip -r ../deploy-artifact.zip .
artifacts:
  files:
    - ../deploy-artifact.zip
  discard-paths: yes

3. Deploy Stage (AWS CodeDeploy)

This is where the Blue-Green magic happens. AWS CodeDeploy can manage deployments to EC2 instances. We’ll configure it for a Blue-Green deployment strategy.

CodeDeploy Application Configuration:

  • Create a CodeDeploy Application (e.g., Magento2App).
  • Create a Deployment Group within this application. Crucially, select “Amazon EC2 Instances” as the compute platform.
  • Deployment Type: Choose “Blue/green deployment”.
  • Load Balancer: Select your ALB.
  • Target Groups: Specify both magento-blue-tg and magento-green-tg. CodeDeploy will manage which one is active.
  • Termination Wait Time: Set this to 0 for immediate traffic switching.
  • Deployment Configuration: Use a custom configuration that deploys to all instances in the target group (e.g., “AllAtOnce”).

appspec.yml for CodeDeploy:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/magento2 # Your Magento root directory
hooks:
  BeforeInstall:
    - location: scripts/before_install.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: scripts/after_install.sh
      timeout: 600
      runas: root
  ApplicationStart:
    - location: scripts/application_start.sh
      timeout: 300
      runas: root
  ValidateService:
    - location: scripts/validate_service.sh
      timeout: 600
      runas: root

Example Scripts:

scripts/before_install.sh:

#!/bin/bash
# Stop web server to prevent serving stale content during deployment
systemctl stop nginx # or apache2
# Optional: Clear Magento cache if not handled by CodeBuild artifact
# /usr/local/bin/php /var/www/html/magento2/bin/magento cache:clean
# /usr/local/bin/php /var/www/html/magento2/bin/magento cache:flush

scripts/after_install.sh:

#!/bin/bash
# Ensure correct file permissions
chown -R www-data:www-data /var/www/html/magento2
chmod -R 755 /var/www/html/magento2

# Re-apply Magento configuration if needed (e.g., if env.php is not part of artifact)
# This is usually handled by user data or instance configuration, but can be reinforced here.
# Example: Injecting DB credentials from SSM Parameter Store
# DB_HOST=$(aws ssm get-parameter --name /magento/db/host --query Parameter.Value --output text)
# DB_NAME=$(aws ssm get-parameter --name /magento/db/name --query Parameter.Value --output text)
# ... and update app/etc/env.php

# Ensure Magento's generated code directories are writable
mkdir -p /var/www/html/magento2/generated/code
mkdir -p /var/www/html/magento2/generated/metadata
mkdir -p /var/www/html/magento2/generated/session
chown -R www-data:www-data /var/www/html/magento2/generated

# Ensure var directory is writable
chown -R www-data:www-data /var/www/html/magento2/var

scripts/application_start.sh:

#!/bin/bash
# Start web server
systemctl start nginx # or apache2

scripts/validate_service.sh:

#!/bin/bash
# This script is crucial for Blue/Green.
# It should perform checks to ensure the new deployment is healthy.
# A simple approach is to curl the health check endpoint.
HEALTH_CHECK_URL="http://localhost/healthcheck.php" # Or your actual health check URL

# Wait for the web server to start and respond
sleep 30

# Perform health check
if curl -s --fail "$HEALTH_CHECK_URL"; then
  echo "Health check passed."
  exit 0
else
  echo "Health check failed."
  exit 1
fi

The Deployment Workflow

When a new commit is pushed to the main branch:

  1. CodePipeline detects the change in the source repository.
  2. CodeBuild runs the buildspec.yml, compiling Magento, deploying static content, and packaging the artifact.
  3. CodeDeploy is triggered to deploy the artifact.
  4. CodeDeploy identifies the “Green” environment (the one not currently serving traffic).
  5. CodeDeploy copies the artifact to the EC2 instances in the Green ASG.
  6. The appspec.yml hooks execute:
    • BeforeInstall: Stops the web server on Green instances.
    • AfterInstall: Sets permissions, potentially re-applies configurations.
    • ApplicationStart: Starts the web server on Green instances.
    • ValidateService: Executes the health check script. If it fails, CodeDeploy will roll back by not switching traffic.
  7. If ValidateService passes, CodeDeploy instructs the ALB to shift traffic from the Blue target group to the Green target group. This is an instantaneous DNS record update within the ALB.
  8. The old “Blue” environment is now idle.

Rollback Strategy

If a deployment fails validation or if issues are discovered post-deployment:

  • Automated Rollback: If the ValidateService hook fails, CodeDeploy automatically stops the deployment and does not switch traffic. The old “Blue” environment remains live.
  • Manual Rollback: In the AWS CodeDeploy console, you can initiate a rollback. This will instruct the ALB to switch traffic back to the original “Blue” environment (which is still running and serving traffic). The “Green” environment, now with the faulty deployment, can be terminated or redeployed later.

Important Considerations & Best Practices

  • Database Migrations: This is the trickiest part of Blue-Green for stateful applications. Magento’s database schema changes must be backward compatible. Deployments should ideally only contain code that works with the *current* database schema. If a new schema is required, it needs a multi-step deployment:
    1. Deploy code that *adds* new schema elements (e.g., new columns, new tables) but doesn’t rely on them.
    2. Switch traffic.
    3. Deploy code that *uses* the new schema elements.
    4. Switch traffic.
    5. Deploy code that *removes* old schema elements (e.g., old columns).
    This often requires careful coordination and potentially manual intervention or more sophisticated CI/CD tooling. For simpler Magento 2 setups, ensure your setup:upgrade commands are idempotent and don’t break existing functionality.
  • Magento Cache: Ensure cache clearing commands are robust. If using Redis, ensure both environments connect to the same Redis instance.
  • Session Management: Using Redis or Memcached for sessions is mandatory for seamless traffic switching.
  • Media Files: S3/CloudFront is essential. Ensure both environments have read access.
  • Health Checks: Invest time in a comprehensive health check script that verifies not just connectivity but also application-level functionality.
  • Testing: Implement automated integration and end-to-end tests that run against the “Green” environment *before* traffic is switched. This can be integrated into the ValidateService hook or run as a separate stage in CodePipeline.
  • Instance Warm-up: For Magento, after starting PHP-FPM and the web server, there might be a brief period where performance is suboptimal due to JIT compilation or initial cache loading. The ValidateService script should account for this, perhaps by retrying the health check or introducing a small delay.
  • Configuration Management: Use tools like AWS Systems Manager Parameter Store or Secrets Manager for all sensitive configurations and environment-specific settings. Avoid hardcoding.
  • Downtime during DB Upgrade: If a database schema change is *not* backward compatible, a brief maintenance window might still be unavoidable. The Blue-Green strategy minimizes this by allowing you to prepare the new environment and switch quickly, but the database itself might require a brief read-only period or a short downtime if schema changes are destructive.

Primary Sidebar

A little about the Author

Having 12+ Years of Experience in Software Development, Vinay is a principal software architect, senior systems engineer, and elite technical consultant. He specializes in bespoke PHP/WordPress development, high-performance Magento 2 & Shopify architectures, custom plugin/theme development from scratch, and legacy code modernization (including VB6, VB.NET, PyQt, and Crystal Reports). Known for solving complex database bottlenecks, speed optimization (Core Web Vitals), and advanced security code auditing, Vinay engineers production-ready systems designed to scale under heavy concurrent load conditions.



Chat on WhatsApp

Recent Posts

  • Reducing database query bloat in Sage Roots modern environments layouts using custom lazy loaders
  • Performance Optimization: Tuning PHP-FPM and opcache pools for high-concurrency Firebase Realtime DB handlers
  • Reducing Largest Contentful Paint (LCP) by optimizing custom script enqueuing structures in legacy plugins
  • How to implement native Redis caching layers for high-volume custom taxonomy queries in Carbon Fields custom wrappers
  • Building secure B2B pricing grids with custom REST API Controllers endpoints and role overrides

Categories

  • apache (1)
  • Business & Monetization (390)
  • Centos (4)
  • Comparisons & Decision Making (55)
  • Debian (2)
  • Debugging & Troubleshooting (658)
  • Desktop Applications (14)
  • DevOps (7)
  • DevOps & Cloud Scaling (962)
  • Django (1)
  • Laravel (4)
  • Migration & Architecture (192)
  • Mobile Applications (24)
  • MySQL (1)
  • Performance & Optimization (872)
  • PHP (5)
  • PHP Development (48)
  • Plugins & Themes (244)
  • Programming Languages (9)
  • Python (20)
  • Ruby on Rails (1)
  • Security & Compliance (639)
  • SEO & Growth (492)
  • Server (23)
  • Ubuntu (9)
  • VB6 & VB.NET (8)
  • Web Applications & Frontend (19)
  • Web Assembly (Wasm) (2)
  • WordPress (22)
  • WordPress Plugin Development (182)
  • WordPress Plugin Development (197)
  • WordPress Plugin Development (330)
  • WordPress Theme Development (357)

Recent Posts

  • Reducing database query bloat in Sage Roots modern environments layouts using custom lazy loaders
  • Performance Optimization: Tuning PHP-FPM and opcache pools for high-concurrency Firebase Realtime DB handlers
  • Reducing Largest Contentful Paint (LCP) by optimizing custom script enqueuing structures in legacy plugins

Top Categories

  • DevOps & Cloud Scaling (962)
  • Performance & Optimization (872)
  • Debugging & Troubleshooting (658)
  • Security & Compliance (639)
  • SEO & Growth (492)
  • Business & Monetization (390)

Our Products

  • ERP & LMS Systems (4)
  • Directories & Marketplaces (4)
  • Healthcare Portals (3)
  • Point of Sale (POS) (2)
  • E-Commerce Engines (2)

Our Services

  • E-Commerce Development (10)
  • WordPress Development (8)
  • Python & Desktop GUI (7)
  • General Consulting (7)
  • Legacy Modernization (5)
  • Mobile App Development (4)

Copyright © 2026 · Vinay Vengala