Skip to content

AWS Lambda canary deployment implementation pattern using aliases and weighted traffic shifting to gradually roll out new versions with automated rollback capabilities.

Notifications You must be signed in to change notification settings

mate329/lambda-alias-canary-deployment

Repository files navigation

Lambda Alias Canary Deployment

AWS Lambda canary deployment implementation using aliases and weighted traffic shifting to gradually roll out new versions with automated rollback capabilities.

Overview

This project demonstrates a production-ready serverless real estate property management API built with AWS CDK, implementing canary deployments for Lambda functions using Lambda aliases and AWS CodeDeploy. The architecture showcases best practices for gradual rollouts with automated monitoring and rollback mechanisms.

Key Features

  • πŸš€ Canary Deployments: Automated gradual rollouts using AWS CodeDeploy (10% traffic for 5 minutes)
  • πŸ”„ Automatic Rollback: CloudWatch Alarms trigger rollbacks on deployment failures
  • πŸ“¦ Lambda Versioning: Code hash-based versioning for deterministic deployments
  • 🎯 Lambda Aliases: Production aliases with traffic shifting capabilities
  • πŸ“Š Monitoring: CloudWatch error alarms for each Lambda function
  • πŸ—οΈ Infrastructure as Code: Complete AWS CDK implementation
  • πŸ—„οΈ Single-Table Design: Optimized DynamoDB schema with PynamoDB ORM
  • πŸ–ΌοΈ Image Management: S3 + CloudFront for property images with presigned URLs

Architecture

Deployment Flow

Code Change β†’ Hash Generation β†’ Lambda Version β†’ Alias Update β†’ Canary Deployment
                                                                        ↓
                                               10% traffic β†’ Monitor errors for 5 min
                                                                        ↓
                                                     Success β†’ 100% traffic
                                                     Failure β†’ Rollback

Lambda Functions

Each CRUD operation has its own Lambda function with canary deployment:

  • CreatePropertyLambda: Create new property listings
  • GetPropertyLambda: Retrieve single or multiple properties with filters
  • UpdatePropertyLambda: Update existing property data
  • DeletePropertyLambda: Delete properties and associated images

AWS Resources

  • API Gateway: RESTful API endpoints
  • Lambda Functions: Serverless compute with production aliases
  • DynamoDB: Two tables for properties and image metadata
  • S3: Property image storage with versioning
  • CloudFront: CDN for image delivery with Origin Access Control
  • CloudWatch: Error monitoring and alarming
  • CodeDeploy: Canary deployment orchestration
  • Lambda Layers: Shared dependencies (PynamoDB, AWS Lambda Powertools)

Canary Deployment Configuration

Deployment Strategy

  • Type: CANARY_10_PERCENT_5_MINUTES
  • Initial Traffic: 10% to new version
  • Evaluation Period: 5 minutes
  • Monitoring: CloudWatch error alarms (2 datapoints in 30-second periods)
  • Auto-Rollback: Enabled for failed and stopped deployments

Code Versioning

Lambda versions are automatically created based on code hash:

def _get_code_hash(self, directory: str) -> str:
    """Generate a hash of all files in the Lambda code directory."""
    # Hashes all .py, .json, .txt, .yaml, .yml files
    # Returns first 8 characters of MD5 hash

The hash is stored in the Lambda function description as v-{hash}, enabling deterministic version tracking.

API Endpoints

Base URL

https://{api-id}.execute-api.{region}.amazonaws.com/prod/properties

Endpoints

Method Endpoint Description
POST /properties Create new property
GET /properties Get all properties (with filters)
GET /properties/{id} Get single property
PUT /properties/{id} Update property
DELETE /properties/{id} Delete property

Query Parameters (GET /properties)

  • city, state, type, status
  • featured=true, isNew=true
  • minPrice, maxPrice
  • limit, offset (pagination)

Project Structure

.
β”œβ”€β”€ app.py                          # CDK stack definition
β”œβ”€β”€ cdk.json                        # CDK configuration
β”œβ”€β”€ requirements.txt                # CDK dependencies
β”œβ”€β”€ models.py                       # PynamoDB models
β”œβ”€β”€ install_deps.sh                 # Layer dependencies installer
β”œβ”€β”€ CreatePropertyHandler/
β”‚   └── handler.py                  # POST handler
β”œβ”€β”€ GetPropertyHandler/
β”‚   └── handler.py                  # GET handler
β”œβ”€β”€ UpdatePropertyHandler/
β”‚   └── handler.py                  # PUT handler
β”œβ”€β”€ DeletePropertyHandler/
β”‚   └── handler.py                  # DELETE handler
└── layers/
    └── common_layer/
        β”œβ”€β”€ requirements.txt        # Layer dependencies
        └── python/
            β”œβ”€β”€ models.py           # Shared models
            └── aws_lambda_powertools/

Setup & Deployment

Prerequisites

  • Python 3.12+
  • Node.js 18+ (for AWS CDK)
  • AWS CLI configured
  • AWS CDK CLI installed (npm install -g aws-cdk)

Installation

  1. Clone the repository

    git clone <repository-url>
    cd lambda-alias-canary-deployment
  2. Create virtual environment

    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Install Lambda layer dependencies

    chmod +x install_deps.sh
    ./install_deps.sh
  5. Bootstrap CDK (first time only)

    cdk bootstrap

Deploy

cdk deploy

The deployment will output:

  • API Gateway endpoint URL
  • Lambda alias ARNs for each function
  • CloudFront distribution domain

Making Updates

When you update Lambda handler code:

  1. Modify handler code in respective directories
  2. Deploy changes
    cdk deploy
  3. Monitor deployment
    • CodeDeploy will automatically:
      • Create a new Lambda version
      • Shift 10% traffic to new version
      • Monitor for errors for 5 minutes
      • Complete rollout or rollback based on alarms

Monitoring Deployments

AWS Console:

  • Navigate to CodeDeploy β†’ Deployments
  • View deployment status and traffic shifting progress

CLI:

aws deploy list-deployments --application-name <deployment-group-name>

Data Models

PropertyModel

Main property table with single-table design:

  • PK: PROPERTY#{id}
  • SK: METADATA

Attributes:

  • Basic: title, description, price, address, city, state, zip
  • Details: beds, baths, sqft, type
  • Media: images count, features list
  • Status: featured, isNew, status (active/sold/pending)
  • Analytics: viewCount, saveCount
  • Timestamps: createdAt, updatedAt

PropertyImageModel

Image metadata and analysis table:

  • PK: PROPERTY#{property_id}
  • SK: IMAGE#{image_id}

Attributes:

  • imageKey (S3 key)
  • imageUrl (CloudFront URL)
  • rekognitionLabels (AI-detected labels)
  • uploadedAt

Environment Variables

Lambda functions use the following environment variables:

  • PROPERTY_TABLE_NAME: DynamoDB property table
  • PROPERTY_IMAGES_TABLE: DynamoDB images table
  • PROPERTY_BUCKET: S3 bucket for images
  • CLOUDFRONT_DOMAIN: CloudFront distribution domain
  • ALLOWED_ORIGIN: CORS allowed origin
  • CODE_VERSION: Lambda version hash

Adding New Features

  1. Update handler code in respective directories
  2. Update models if schema changes
  3. Run install_deps.sh if new dependencies added
  4. Deploy with cdk deploy
  5. Monitor CodeDeploy for canary deployment progress

Cleanup

To remove all resources:

cdk destroy

This will delete:

  • Lambda functions and versions
  • API Gateway
  • DynamoDB tables
  • S3 bucket (with auto-delete enabled)
  • CloudFront distribution
  • CodeDeploy deployment groups
  • CloudWatch alarms

Key Learnings

Canary Deployments

  • Traffic Shifting: Gradual rollout reduces blast radius of bugs
  • Automated Monitoring: CloudWatch alarms enable automatic rollback
  • Lambda Versioning: Immutable versions enable reliable rollbacks
  • Aliases: Production alias provides stable ARN for API Gateway integration

Best Practices

  • βœ… Use Lambda layers for shared dependencies
  • βœ… Implement code hash versioning for deterministic deployments
  • βœ… Configure CloudWatch alarms for deployment monitoring
  • βœ… Enable auto-rollback for failed deployments
  • βœ… Use single-table design for DynamoDB optimization
  • βœ… Implement structured logging with AWS Lambda Powertools
  • βœ… Use presigned URLs for secure S3 access
  • βœ… Enable CloudFront OAC for S3 security

Technologies

  • AWS CDK (Python) - Infrastructure as Code
  • AWS Lambda - Serverless compute
  • AWS CodeDeploy - Deployment automation
  • Amazon DynamoDB - NoSQL database
  • Amazon S3 - Object storage
  • Amazon CloudFront - CDN
  • Amazon API Gateway - REST API
  • AWS Lambda Powertools - Structured logging and tracing
  • PynamoDB - Pythonic DynamoDB ORM

License

MIT


Note: This project is designed for educational purposes to demonstrate Lambda canary deployments with aliases. For production use, consider adding:

  • Authentication/Authorization (Cognito, Lambda authorizers)
  • Rate limiting and throttling
  • Input validation and sanitization
  • Comprehensive error handling
  • Unit and integration tests
  • CI/CD pipeline integration
  • Cost monitoring and optimization

About

AWS Lambda canary deployment implementation pattern using aliases and weighted traffic shifting to gradually roll out new versions with automated rollback capabilities.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published