Skip to content

ConstructsFeatureFlagsReport fails with CDK Pipelines: App.of() returns Stage instead of App #1440

@agogna

Description

@agogna

Description

In version 2.96.0, the ConstructsFeatureFlagsReport.ensure() method uses App.of(scope) to find the root App. However, when Solutions Constructs are used within CDK Pipelines (specifically when constructs are instantiated in a Stage within a Pipeline), App.of() returns the Stage object instead of the root App, causing a validation error.

Code Reference

The issue originates from this line in constructs-feature-flags.ts:

public static ensure(scope: Construct) {
  const app = App.of(scope);  // Returns Stage in CDK Pipelines context
  if (!app) {
    throw new Error('Invalid scope provided to ConstructsFeatureFlagsReport');
  }
  const id = 'AwsSolutionsFeatureFlagsReport';
  if (!app.node.tryFindChild(id)) {
    new ConstructsFeatureFlagsReport(app, id);
  }
}

Error Message

ValidationError: ConstructsFeatureFlagsReport at 'HubPipeline/HubTest-EuWest1/AwsSolutionsFeatureFlagsReport' 
should be created in the scope of a Stack, but no Stack found
    at path [HubPipeline/HubTest-EuWest1] in aws-cdk-lib.Stage

Architecture

App (root)
└── CdkPipeline (Stack)
    └── TestStage-EuWest1 (Stage)  ← App.of() returns this Stage
        └── AppStack (Stack)
            └── AppConstruct (Construct)
                └── SnsToSqs (Solutions Construct)

When SnsToSqs calls ConstructsFeatureFlagsReport.ensure(this), the App.of(scope) traverses up the tree and stops at the first App instance it finds. However, in CDK Pipelines, the Stage class extends App (via composition), so App.of() returns the Stage instead of the root App.

Reproduction Steps

  1. Create a CDK Pipeline with a Stage
  2. Within the Stage, create a Stack
  3. Within the Stack, use any Solutions Construct that instantiates SnsToSqs inside a Construct (not directly in a Stack)
  4. Run cdk synth or cdk list

Expected Behavior

ConstructsFeatureFlagsReport should be created at the root App level, regardless of whether the construct is used within a CDK Pipeline Stage.

Actual Behavior

ConstructsFeatureFlagsReport is created at the Stage level, which causes a validation error because it's not in a Stack scope.

Environment

  • AWS CDK: 2.231.0
  • AWS Solutions Constructs: 2.96.0
  • Node.js: 22.13.1
  • TypeScript: 5.7.3

Workaround

Downgrade to AWS Solutions Constructs v2.92.0 or earlier, which doesn't include the ConstructsFeatureFlagsReport feature.

Additional Context

This issue was introduced in v2.93.0 when feature flag infrastructure was added (#1407). The feature appears to have been tested only with simple App → Stack hierarchies, not with CDK Pipelines that use Stages.

CDK Pipelines are a common pattern for multi-environment deployments, so this issue affects many production use cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions