aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(aws-ecs / aws-elasticloadbalancingv2): CDK resets ALB listener rule actions during stack updates, breaking blue/green deployments

Open adriantaut opened this issue 10 months ago • 7 comments

Describe the bug

When using ECS blue/green deployments (either with AlternateTarget for native blue/green or manual weighted target groups), CDK resets the listener rule action to the default state defined in code whenever ANY stack update occurs. This happens even when traffic has been shifted externally via AWS CLI, CodeDeploy, ECS native deployment, or the AWS console.

This makes it very risky to:

  1. Perform blue/green traffic shifts outside of CDK
  2. Continue to update infrastructure with CDK without disrupting live traffic routing

Reproduction Steps

Scenario 1: ECS Native Blue/Green with AlternateTarget

Based on the test file packages/aws-cdk-lib/aws-ecs/test/alternate-target-configuration.test.ts:

const prodRule = new elbv2.ApplicationListenerRule(stack, 'ProdRule', {
  listener,
  priority: 1,
  conditions: [elbv2.ListenerCondition.pathPatterns(['/prod'])],
  action: elbv2.ListenerAction.forward([blueTargetGroup]), // Hardcoded default
});

const alternateTarget = new ecs.AlternateTarget('GreenTG', {
  alternateTargetGroup: greenTargetGroup,
  productionListener: ecs.ListenerRuleConfiguration.applicationListenerRule(prodRule),
});

const service = new ecs.FargateService(stack, 'FargateService', {
  cluster,
  taskDefinition,
});

const target = service.loadBalancerTarget({
  containerName: 'web',
  containerPort: 80,
  alternateTarget,
});
target.attachToApplicationTargetGroup(blueTargetGroup);

Steps to reproduce:

  1. Deploy the stack - traffic goes to blue
  2. ECS native blue/green shifts traffic to green (modifies prodRule action)
  3. Update conditions or any other resource in Listener
  4. Run cdk deploy
  5. Result: The prodRule action is reset to forward([blueTargetGroup]), breaking live traffic

Scenario 2: Weighted Target Groups

const listenerRule = new elbv2.ApplicationListenerRule(this, 'LiveTrafficRule', {
  listener,
  priority: 1,
  conditions: [elbv2.ListenerCondition.pathPatterns(['/*'])],
  action: elbv2.ListenerAction.weightedForward([
    { targetGroup: blue, weight: 100 }, 
    { targetGroup: green, weight: 0 }
  ]),
});

Steps to reproduce:

  1. Deploy with traffic at blue: 100, green: 0
  2. Use AWS CLI to shift traffic: aws elbv2 modify-rule --rule-arn <arn> --actions ... (blue: 0, green: 100)
  3. Update ANY other resource in the stack
  4. Run cdk deploy
  5. Result: Traffic weights are reset to blue: 100, green: 0 (destroys live traffic routing)

Expected Behavior

CDK should provide a mechanism to exclude listener rule actions from management, similar to:

  • Terraform's lifecycle { ignore_changes = [...] }
  • CloudFormation's UpdateReplacePolicy for specific properties

This would allow:

  • CDK to manage the listener rule infrastructure (conditions, priority, target group references)
  • External tools (CodeDeploy, ECS, AWS CLI) to manage traffic routing (action/weights)
  • Continued CDK updates without resetting traffic state

Current Behavior

CloudFormation always enforces the declarative state defined in CDK code, resetting any external changes to listener rule actions on every stack update.

Possible Solution

Provide one or more of the following:

Option 1: Property-level deletion override

const cfnRule = listenerRule.node.defaultChild as elbv2.CfnListenerRule;
cfnRule.addPropertyDeletionOverride('Actions.0.ForwardConfig.TargetGroups.0.Weight');
cfnRule.addPropertyDeletionOverride('Actions.0.ForwardConfig.TargetGroups.1.Weight');
// or for the entire action
cfnRule.addPropertyDeletionOverride('Actions.0');

Option 2: Dedicated property to exclude action from management

const listenerRule = new elbv2.ApplicationListenerRule(this, 'Rule', {
  listener,
  conditions: [...],
  action: elbv2.ListenerAction.forward([blueTargetGroup]),
  manageAction: false, // Don't update the action after initial creation
});

Option 3: Import pattern for external management

const listenerRule = elbv2.ApplicationListenerRule.fromListenerRuleArn(
  this, 
  'ExistingRule', 
  ruleArn,
  { manageAction: false }
);

Environment

  • CDK CLI Version: 2.x
  • Framework Version: aws-cdk-lib 2.x
  • Node.js Version: 18.x+
  • OS: All
  • Language: TypeScript/JavaScript

Regression Issue

  • [ ] Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

Current Behavior

Reproduction Steps

Possible Solution

No response

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.233.0

AWS CDK CLI version

2.1033.0

Node.js Version

v24.5.0

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

adriantaut avatar Jan 10 '26 10:01 adriantaut

Hi @adriantaut,

Thanks for this detailed bug report! This is a good catch - you've identified a real pain point when using CDK with ECS Native blue/green deployments.

Root Cause Analysis:

The issue stems from a design gap between ECS Native B/G and CloudFormation's declarative model:

  1. When you configure ECS Native B/G with AlternateTarget, CDK passes the ProductionListenerRule ARN to ECS via AdvancedConfiguration — this tells ECS which listener rule to modify during traffic shifts.

  2. However, the listener rule itself (with its Actions property including target group weights) is still fully managed by CloudFormation.

  3. When ECS shifts traffic, it modifies the listener rule's Actions directly via the ELBv2 API. CloudFormation is unaware of this change.

  4. On the next stack update, CloudFormation sees "drift" and resets the Actions to match the template — breaking your live traffic routing.

This appears to be a gap in the ECS Native B/G + CloudFormation integration. The AWS documentation shows how to set up ECS Native B/G with CloudFormation, but doesn't address ongoing stack updates after ECS has shifted traffic.

I have navigated all public docs but didn't found anything about this.

Actions:

This issue highlights a gap in the ECS Native B/G + IaC story. We'll:

  • Reach out to the ECS/CloudFormation teams to understand if there's a recommended pattern for this scenario, or if this is a known limitation.

  • Update documentation to clarify the interaction between ECS Native B/G and CloudFormation stack updates.

Related Issues:

  • #36012 - ECS CODE_DEPLOY migration issues (related but different root cause)

ASCII notes

┌─────────────────────────────────────────────────────────────────────────────┐
│                    TWO DIFFERENT B/G APPROACHES                             │
└─────────────────────────────────────────────────────────────────────────────┘

APPROACH 1: CloudFormation + CodeDeploy B/G (Transform-based)
─────────────────────────────────────────────────────────────
    ┌──────────────┐     ┌──────────────┐     ┌──────────────┐
    │ CloudFormation│────▶│  CodeDeploy  │────▶│ Traffic Shift│
    │ (orchestrates)│     │  (executes)  │     │ (managed)    │
    └──────────────┘     └──────────────┘     └──────────────┘
           │
           └──► CFN KNOWS about traffic state ✓
           └──► No drift issue ✓


APPROACH 2: ECS Native B/G (AlternateTarget) ◄── Issue #36649
─────────────────────────────────────────────
    ┌──────────────┐     ┌──────────────┐     ┌──────────────┐
    │ CloudFormation│     │     ECS      │────▶│ Traffic Shift│
    │ (infra only) │     │  (deploys)   │     │ (via AWS API)│
    └──────────────┘     └──────────────┘     └──────────────┘
           │                    │
           │                    └──► Modifies listener rules directly
           │
           └──► CFN DOESN'T KNOW about traffic state ✗
           └──► Drift/reset issue on next deploy ✗

pahud avatar Jan 12 '26 18:01 pahud

related PRs #35061

pahud avatar Jan 12 '26 18:01 pahud

Any strategies to mitigate the drift ? Would it be possible to add an action like "bluegreen" and just act as a placeholder that is ignored at synth time ?

unicorne-klarocque avatar Feb 10 '26 15:02 unicorne-klarocque

Any news on this ? The issue has been opened since January. It's almost May

unicorne-klarocque avatar Apr 29 '26 12:04 unicorne-klarocque

just had another Production incident because of this...are there any news? Thank you and would highly appreciate a fix!

adriantaut avatar Jun 29 '26 12:06 adriantaut

We also got bitten by this bug when spiking native blue/green Fargate deployments. We ended up working around this by managing listener rules outside of IaC, but native CDK support would be ideal 🙏

n-tucker avatar Oct 13 '26 02:10 n-tucker

@pahud Is there any update on this matter? A workaround would also be much appreciated.

pitayapj avatar Nov 12 '26 06:11 pitayapj