AWS API Gateway Blue/Green Deployment
A production-grade, zero-downtime deployment pipeline using blue/green strategy on AWS. This solution leverages API Gateway dual stages, Lambda alias weighting, and is fully automated via GitHub Actions and Terraform.
Architecture Overview
The system implements a classic blue/green deployment model where:
- Blue = Live production environment (stable, serving 100% of traffic)
- Green = Candidate environment (new release, validated before promotion)
Traffic is shifted gradually using Lambda alias weighting, starting at 10% to green, monitoring CloudWatch metrics, and promoting to 100% only when health thresholds are met.
Key Features
- Blue/Green Deployments: Zero-downtime releases with instant rollback capability.
- Canary Traffic Shifting: Gradual 0% → 10% → 100% Lambda alias weighting.
- Automated Validation: CloudWatch alarms gate promotion — no human approval needed.
- Infrastructure as Code: Entire stack declared in Terraform; reproducible in any region.
- CI/CD Pipeline: GitHub Actions orchestrates build, deploy, and promote stages.
- Shared Data Layer: DynamoDB + S3 shared between both environments — no data drift.
Deployment Lifecycle
The deployment process involves the following steps:
- Developer pushes code to the main branch.
- GitHub Actions builds and tests the code.
- Terraform applies changes to provision the green environment's Lambda function.
- The new version is deployed to the green stage of the API Gateway.
- A canary shift is performed, moving 10% of traffic to the green environment via Lambda alias weight updates.
- CloudWatch metrics are validated over a ~5-minute window.
- If healthy, traffic is fully promoted to green (100%); otherwise, the deployment is rolled back to blue.
Getting Started
Prerequisites:
- AWS CLI configured with appropriate permissions.
- Terraform version 1.5 or higher.
- Node.js version 18 or higher (for Lambda source code).
- GitHub repository secrets configured (AWS credentials, region, Terraform state bucket).
Required AWS IAM Permissions:
The deploying IAM role/user needs permissions for: lambda:*, apigateway:*, cloudwatch:*, dynamodb:*, s3:*, route53:*, iam:PassRole.
Local Setup:
Clone the repository, navigate to the terraform directory, and run terraform init, terraform plan, and terraform apply.
Manual Traffic Shift Commands:
- Shift 10% traffic to green: ./scripts/shift-traffic.sh --blue 90 --green 10
- Validate metrics: ./scripts/validate-metrics.sh
- Full promotion: ./scripts/shift-traffic.sh --blue 0 --green 100
- Emergency rollback: ./scripts/shift-traffic.sh --blue 100 --green 0
Observability
CloudWatch dashboards track key metrics per environment, including Error Rate, Latency (p99), Lambda Throttles, Lambda Duration, and DynamoDB Read/Write Capacity. Alarms are configured to gate the promotion process.
Security Considerations
- Lambda functions use least-privilege IAM roles.
- API Gateway endpoints can be protected via IAM authorization or API keys.
- Terraform state is encrypted at rest in S3.
- Secrets are managed via AWS Secrets Manager.
- CloudTrail is enabled for auditing.
Design Decisions
This solution uses a single API Gateway with dual stages to avoid DNS propagation delays during cutover. Lambda alias weighting is preferred over Route 53 weighted routing for sub-second rollbacks. Sharing DynamoDB/S3 between blue and green environments simplifies data management but requires strict adherence to backwards-compatible schema migrations.
License
This project is licensed under the MIT License.