This repository contains the Infrastructure as Code (IaC) and automation scripts for an advanced, risk-mitigated Canary Deployment pipeline. Built for high-frequency, zero-downtime production environments, this project demonstrates how to deploy code updates to a serverless Python API while mitigating deployment risk by mathematically shifting live user traffic.
- Infrastructure as Code (IaC): Terraform
- Compute Engine: AWS Lambda (Serverless)
- Release Automation: AWS CodeDeploy
- Language: Python 3.10
- CLI Engine: AWS CLI v2 (AppSpec Injections)
- Immutable Serverless Versioning: The Terraform configuration enforces strict Lambda compilation and publishing rules (
publish = true). This prevents version drifting by generating permanent, unmodifiable software versions (e.g.,V1,V2) rather than mutating code in place. - Decoupled Alias Routing: Traffic hits an abstract AWS Lambda Alias pointer (
live). Terraform is configured vialifecyclehooks to ignore active routing changes, handing absolute control over the production traffic split to AWS CodeDeploy. - Linear/Canary Shifting Configuration: The architecture implements the
CodeDeployDefault.LambdaCanary10Percent5Minutesengine. When an upgrade occurs, exactly 10% of global API requests are routed to the new code while 90% remain on the legacy system, establishing a 5-minute telemetry observation window before promoting the version to 100%.
/src: Contains the Python application code for the Market Pricing API (Simulating V1 baseline and V2 feature enhancement)./infrastructure: Contains the Terraform configuration mapping out IAM execution roles, the CodeDeploy deployment group, and the Lambda alias controls.
1. Deploy the Baseline Infrastructure (V1.0):
cd infrastructure
terraform init
terraform apply2. Push the V2.0 App Bundle to the Cloud Data Center:
Update the src/api.py payload with your new feature metrics and run an upgrade compilation:
terraform apply3. Execute the Live Canary Traffic Shift: Inject an AppSpec configuration directly into CodeDeploy via the AWS CLI to trigger the 10/90 mathematical split:
aws deploy create-deployment \
--application-name market-pricing-api-deployment \
--deployment-group-name market-pricing-api-dg \
--description "Canary shift from V1.0 to V2.0" \
--revision '{"revisionType": "AppSpecContent", "appSpecContent": {"content": "{\"version\": 0.0, \"Resources\": [{\"myLambdaFunction\": {\"Type\": \"AWS::Lambda::Function\", \"Properties\": {\"Name\": \"market-pricing-api\", \"Alias\": \"live\", \"CurrentVersion\": \"1\", \"TargetVersion\": \"2\"}}}]}"}}'