Skip to main content

BlueGreenLambda

sdlc-cdk-lib v1.0.0


sdlc-cdk-lib / modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda

modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda

Classes​

BlueGreenLambda​

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:158

Construct for deploying Lambda functions with Blue/Green deployment strategy.

Remarks​

This construct implements zero-downtime Lambda deployments using AWS CodeDeploy. It creates a Lambda function with automatic versioning, a production alias for traffic shifting, and CloudWatch alarms for automatic rollback.

Deployment Strategies by Environment:

  • dev/test: ALL_AT_ONCE - Immediate deployment
  • staging/qa: CANARY_10PERCENT_5MINUTES - 10% traffic for 5 minutes
  • preprod/prod: LINEAR_10PERCENT_EVERY_10MINUTES - 10% every 10 minutes

Features:

  • Versioned deployments with Lambda aliases
  • CodeDeploy integration for gradual traffic shifting
  • CloudWatch alarms for automatic rollback on errors
  • Environment-specific deployment configuration
  • Automatic log group creation with retention policies
  • SDLC tag injection for resource tracking

Examples​

import { BlueGreenLambda } from './lib/lambda/BlueGreenLambda';

const lambda = new BlueGreenLambda(stack, 'MyApi', {
contextStrategy: stack.contextStrategy,
functionName: 'my-api',
entry: './src/lambda/myApi/index.ts',
});

// Use the production alias (not the function directly)
new LambdaRestApi(stack, 'Api', {
handler: lambda.getFunction(), // Returns production alias
});
const lambda = new BlueGreenLambda(stack, 'MyApi', {
contextStrategy: stack.contextStrategy,
functionName: 'my-api',
entry: './src/lambda/myApi/index.ts',
environment: {
TABLE_NAME: table.tableName,
API_KEY: secret.secretValue.unsafeUnwrap(),
},
timeout: Duration.minutes(5),
memorySize: 512,
});

// Grant permissions
table.grantReadWriteData(lambda.lambda);
const lambda = new BlueGreenLambda(stack, 'MyApi', {
contextStrategy: stack.contextStrategy,
functionName: 'my-api',
entry: './src/lambda/myApi/index.ts',
});

// Access underlying resources
console.log(lambda.lambda.functionName); // The Lambda function
console.log(lambda.productionAlias.aliasName); // 'production'
console.log(lambda.deploymentGroup.deploymentGroupName);
console.log(lambda.errorAlarm.alarmName);

Extends​

  • Construct

Constructors​

Constructor​

> new BlueGreenLambda(scope, id, props): BlueGreenLambda

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:235

Creates a new BlueGreenLambda construct.

Parameters​
scope​

Construct

Parent construct (typically a Stack)

id​

string

Construct identifier

props​

BlueGreenLambdaProps

Configuration properties

Returns​

BlueGreenLambda

Remarks​

This constructor:

  1. Creates a CloudWatch Log Group with SDLC-specific retention
  2. Creates a Lambda function with versioning enabled
  3. Creates a production alias pointing to the current version
  4. Creates CloudWatch alarms for error monitoring
  5. Creates a CodeDeploy deployment group for traffic shifting

The deployment strategy is determined by the SDLC environment:

  • Development: Deploys immediately
  • Staging: Canary deployment (10% for 5 minutes)
  • Production: Linear deployment (10% every 10 minutes)
Example​
const stack = new Stack(app, 'MyStack');
const lambda = new BlueGreenLambda(stack, 'API', {
contextStrategy: stack.contextStrategy,
functionName: 'my-api',
entry: './src/lambda/api/index.ts',
});
Overrides​

Construct.constructor

Properties​

currentVersion​

> readonly currentVersion: IVersion

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:194

Current Lambda version (immutable snapshot).

Remarks​

Each deployment creates a new version. CodeDeploy shifts traffic from the previous version to this version through the production alias.

deploymentGroup​

> readonly deploymentGroup: LambdaDeploymentGroup

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:185

CodeDeploy deployment group managing Blue/Green deployments.

Remarks​

Handles gradual traffic shifting and automatic rollback. Deployment configuration varies by SDLC environment.

errorAlarm​

> readonly errorAlarm: Alarm

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:203

CloudWatch alarm monitoring Lambda errors.

Remarks​

Triggers automatic rollback if error rate exceeds threshold during deployment. Thresholds are stricter for production environments.

lambda​

> readonly lambda: NodejsFunction

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:167

The underlying Lambda NodeJS function.

Remarks​

Use this to grant IAM permissions or access function properties. For API Gateway or ALB integration, use getFunction instead to ensure traffic shifting works correctly.

productionAlias​

> readonly productionAlias: Alias

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:176

Production alias for traffic shifting.

Remarks​

This alias is the target for API Gateway, ALB, or other Lambda triggers. CodeDeploy shifts traffic between Lambda versions through this alias.

Methods​

getFunction()​

> getFunction(): IFunction

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:349

Returns the production alias for integration with other services.

Returns​

IFunction

The Lambda production alias (not the function directly)

Remarks​

Always use this method when integrating with:

  • API Gateway (RestApi, HttpApi)
  • Application Load Balancer
  • EventBridge rules
  • SNS topics
  • SQS queues

Using the alias ensures CodeDeploy can perform gradual traffic shifting. Direct function references bypass Blue/Green deployment.

Examples​
const lambda = new BlueGreenLambda(stack, 'API', {
contextStrategy: stack.contextStrategy,
functionName: 'my-api',
entry: './src/lambda/api/index.ts',
});

new LambdaRestApi(stack, 'ApiGateway', {
handler: lambda.getFunction(), // ✅ Correct - uses alias
// handler: lambda.lambda, // ❌ Wrong - bypasses Blue/Green
});
const target = new LambdaTarget(lambda.getFunction());
targetGroup.addTarget(target);

Interfaces​

BlueGreenLambdaProps​

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:23

Configuration properties for BlueGreenLambda construct.

Properties​

contextStrategy​

> readonly contextStrategy: ContextStrategy

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:30

Context strategy for SDLC environment configuration.

Remarks​

Determines deployment strategy, alarm thresholds, and resource naming.

entry​

> readonly entry: string

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:52

Path to the Lambda function entry file.

Remarks​

Should be a TypeScript file with a default export handler.

Example​
entry: './src/lambda/myApi/index.ts'
environment?​

> readonly optional environment: Record<string, string>

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:62

Environment variables to pass to the Lambda function.

Remarks​

SDLC and SDLC_CORE are automatically added based on context strategy.

Default Value​
{}
functionName​

> readonly functionName: string

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:39

Base name for the Lambda function (without SDLC suffix).

Remarks​

Final function name will be: ${functionName}-${sdlcStage} Example: 'my-api' becomes 'my-api-dev' or 'my-api-staging-feature-123'

memorySize?​

> readonly optional memorySize: number

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:83

Lambda function memory size in MB.

Remarks​

Higher memory also increases CPU allocation. If not specified, uses SDLC-specific default from configuration.

Default Value​
SDLC config default (e.g., 256 for dev)
timeout?​

> readonly optional timeout: Duration

Defined in: modules/sdlc-aws-cdk-lib/lib/lambda/BlueGreenLambda.ts:72

Lambda function timeout.

Remarks​

If not specified, uses SDLC-specific default from configuration.

Default Value​
SDLC config default (e.g., Duration.seconds(30) for dev)