Claude Skills Guide

Claude Code for Blue-Green Switch Workflow Guide

The switch phase—routing traffic from the blue environment to the green environment—is the most critical moment in any blue-green deployment. A poorly executed switch can result in downtime, data loss, or customer impact. This guide demonstrates how Claude Code can help you implement reliable, automated switch workflows with built-in health verification and instant rollback capabilities.

Understanding the Switch Phase

In blue-green deployment, the “switch” refers to the moment you redirect production traffic from the current environment (blue) to the new environment (green). This switch can be implemented through various mechanisms depending on your infrastructure: DNS changes, load balancer configuration updates, service mesh routing rules, or application-level feature flags.

The switch phase presents unique challenges that Claude Code is well-suited to address. You need to verify the new environment is healthy before switching, implement a gradual or instant cutover based on your requirements, monitor for issues post-switch, and have the ability to rollback quickly if problems emerge. Claude Code can help you design, implement, and automate each of these aspects.

Setting Up Environment Verification

Before performing any switch, you must verify that the green environment is healthy and ready to receive traffic. Claude Code can help you create comprehensive health check scripts that validate multiple aspects of your deployment.

First, establish what “healthy” means for your application. This typically includes HTTP endpoint availability, database connectivity, external service integration, and critical business logic validation. Claude Code can generate health check scripts that cover all these aspects:

#!/bin/bash
# Health verification script for green environment

GREEN_ENDPOINT="https://green.example.com"
HEALTH_ENDPOINT="${GREEN_ENDPOINT}/health"
DB_HOST="green-db.example.com"
MAX_RESPONSE_TIME=3000

# Check HTTP health endpoint
response=$(curl -s -w "\n%{http_code}" "$HEALTH_ENDPOINT")
http_code=$(echo "$response" | tail -n1)

if [ "$http_code" != "200" ]; then
    echo "FAIL: Health endpoint returned $http_code"
    exit 1
fi

# Verify response time
response_time=$(curl -s -w "%{time_total}" "$HEALTH_ENDPOINT" -o /dev/null)
response_ms=$(echo "$response_time * 1000" | bc)

if (( $(echo "$response_ms > $MAX_RESPONSE_TIME" | bc -l) )); then
    echo "FAIL: Response time ${response_ms}ms exceeds threshold ${MAX_RESPONSE_TIME}ms"
    exit 1
fi

# Verify database connectivity
if ! nc -z "$DB_HOST" 5432; then
    echo "FAIL: Cannot connect to database at $DB_HOST"
    exit 1
fi

echo "PASS: All health checks passed"
exit 0

This script provides the foundation for an automated pre-switch verification process. Claude Code can help you extend this with additional checks specific to your application, such as verifying cache layer connectivity, checking message queue availability, and ensuring third-party API integrations are functional.

Implementing the Traffic Switch

Once you’ve verified the green environment is healthy, you need to execute the actual switch. Claude Code can help you implement switches across different infrastructure types, each with its own considerations.

DNS-Based Switch

For DNS-based switches, you update the DNS records to point to the new environment’s IP address or load balancer. Claude Code can generate scripts that handle this process safely:

#!/usr/bin/env python3
"""
DNS switch script for blue-green deployment
Updates Route53 records to point to green environment
"""

import boto3
import time

def switch_dns_record hosted_zone_id, record_name, green_elb_dns:
    route53 = boto3.client('route53')
    
    # Get current record to preserve any existing settings
    current = route53.get_resource_record_set(
        HostedZoneId=hosted_zone_id,
        StartRecordName=record_name,
        MaxItems='1'
    )
    
    # Update to point to green environment
    response = route53.change_resource_record_sets(
        HostedZoneId=hosted_zone_id,
        ChangeBatch={
            'Changes': [{
                'Action': 'UPSERT',
                'ResourceRecordSet': {
                    'Name': record_name,
                    'Type': 'A',
                    'AliasTarget': {
                        'HostedZoneId': get_hosted_zone_id(green_elb_dns),
                        'DNSName': green_elb_dns,
                        'EvaluateTargetHealth': True
                    }
                }
            }]
        }
    )
    
    return response['ChangeInfo']['Id']

# Usage
switch_dns_record(
    hosted_zone_id='ZONE_ID',
    record_name='api.example.com',
    green_elb_dns='green-api-1234567890.elb.us-east-1.amazonaws.com'
)

DNS switches require careful consideration of TTL values. Lower TTLs (300 seconds or less) allow faster failover but increase DNS query load. Claude Code can advise on appropriate TTL settings for your use case and help you plan DNS changes that minimize user impact.

Load Balancer Switch

For environments using load balancers, you can switch traffic by updating target group registrations or changing listener rules. This approach is faster than DNS-based switches and doesn’t require waiting for DNS propagation:

# AWS ALB target group switch using AWS CLI
# Switch from blue to green target group

AWSSCRIPT=$(cat << 'SCRIPT'
#!/bin/bash
set -e

BLUE_TG_ARN="arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/blue-api/abc123"
GREEN_TG_ARN="arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/green-api/def456"
ALB_ARN="arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/prod-alb/xyz789"

# Register green targets, deregister blue targets
aws elbv2 register-targets --target-group-arn "$GREEN_TG_ARN" \
    --targets Id=green-api-1,Port=80 Id=green-api-2,Port=80

aws elbv2 deregister-targets --target-group-arn "$BLUE_TG_ARN" \
    --targets Id=blue-api-1,Port=80 Id=blue-api-2,Port=80

# Wait for targets to become healthy
aws elbv2 wait target-in-service \
    --target-group-arn "$GREEN_TG_ARN" \
    --targets Id=green-api-1,Port=80 Id=green-api-2,Port=80

echo "Switch completed successfully"
SCRIPT
)

Claude Code can help you customize these scripts for your specific load balancer setup, whether you’re using AWS ALB, Nginx, HAProxy, or cloud-agnostic solutions like Traefik.

Implementing Rollback Procedures

The ability to rollback quickly is one of the primary advantages of blue-green deployment. Claude Code can help you design robust rollback procedures that can be executed manually or automatically based on monitoring alerts.

A well-designed rollback script should reverse exactly what the switch did. If you registered green targets with the load balancer, rollback deregisters those targets and re-registers blue targets. If you updated DNS records, rollback updates them back to blue:

#!/bin/bash
# Automated rollback script
# Triggered by monitoring or manual intervention

set -e

ENVIRONMENT=${1:-green}  # Pass 'blue' to rollback to blue
NOTIFICATION_WEBHOOK="${WEBHOOK_URL}"

rollback_to_environment() {
    local target_env=$1
    echo "Rolling back to $target_env environment..."
    
    # Update load balancer targets
    case $target_env in
        blue)
            # Re-register blue targets
            aws elbv2 register-targets \
                --target-group-arn "$BLUE_TG_ARN" \
                --targets Id=blue-api-1,Port=80 Id=blue-api-2,Port=80
            
            # Deregister green targets
            aws elbv2 deregister-targets \
                --target-group-arn "$GREEN_TG_ARN" \
                --targets Id=green-api-1,Port=80 Id=green-api-2,Port=80
            ;;
        green)
            # Reverse: register green, deregister blue
            aws elbv2 register-targets \
                --target-group-arn "$GREEN_TG_ARN" \
                --targets Id=green-api-1,Port=80 Id=green-api-2,Port=80
            
            aws elbv2 deregister-targets \
                --target-group-arn "$BLUE_TG_ARN" \
                --targets Id=blue-api-1,Port=80 Id=blue-api-2,Port=80
            ;;
    esac
    
    # Send notification
    curl -X POST "$NOTIFICATION_WEBHOOK" \
        -H 'Content-Type: application/json' \
        -d "{\"text\": \"Rollback to $target_env completed at $(date)\"}"
}

rollback_to_environment "$ENVIRONMENT"

Integrating with Monitoring

A complete blue-green switch workflow includes post-switch monitoring to detect issues before users report them. Claude Code can help you set up monitoring checks that automatically trigger rollback if problems are detected:

# Post-switch monitoring with automatic rollback trigger

def monitor_post_switch(environment, duration_seconds=300, error_threshold=5):
    """
    Monitor the environment after switch and trigger rollback if needed
    """
    start_time = time.time()
    error_count = 0
    
    while time.time() - start_time < duration_seconds:
        # Check error rate from logs/metrics
        error_count = get_error_count(environment)
        p99_latency = get_p99_latency(environment)
        
        if error_count > error_threshold:
            print(f"ERROR: {error_count} errors detected, triggering rollback")
            trigger_rollback(environment)
            return False
        
        if p99_latency > 5000:  # 5 second threshold
            print(f"WARN: High latency detected: {p99_latency}ms")
        
        time.sleep(10)  # Check every 10 seconds
    
    print(f"Monitoring passed for {environment}")
    return True

Best Practices for Switch Workflows

When implementing blue-green switch workflows with Claude Code, follow these proven practices. First, always verify the green environment is fully healthy before switching. Don’t rely on a single health check—implement multiple verification layers. Second, implement a gradual switch strategy for critical applications. Start with a small percentage of traffic and increase gradually while monitoring for issues.

Third, ensure your rollback procedure is tested regularly. A rollback that hasn’t been tested is not reliable when you need it. Fourth, maintain detailed logs of all switch operations for post-incident analysis. Finally, communicate with stakeholders before and after switch operations, especially for customer-facing applications.

Conclusion

The switch phase is where blue-green deployment delivers its value—enabling zero-downtime releases with instant rollback capability. Claude Code can help you implement robust switch workflows that verify environment health, execute safe traffic routing, and automatically rollback when issues are detected. By automating these processes and testing them regularly, you can achieve confident, low-risk production deployments.

Built by theluckystrike — More at zovo.one