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

Multiple warnings with aws-cdk-lib 2.224.0

Open aramir opened this issue 1 year ago • 20 comments

Describe the bug

After updating aws-cdk-lib from 2.222.0 to 2.224.0 started getting multiple warnings during trmplate build (e.g.: running cdk diff or cdk deploy for all l,abda functions registered via NodejsFunction with warning message "ValidationError: The result of fromAwsManagedPolicyName can not be used in this API":

[Warning at /xxx/yyy/PreSignUpFn/ServiceRole] Failed to add construct metadata for node [ServiceRole]. Reason: ValidationError: The result of fromAwsManagedPolicyName can not be used in this API [ack: @aws-cdk/core:addConstructMetadataFailed]
[Warning at /xxx/yyy/PostConfirmationFn/ServiceRole] Failed to add construct metadata for node [ServiceRole]. Reason: ValidationError: The result of fromAwsManagedPolicyName can not be used in this API [ack: @aws-cdk/core:addConstructMetadataFailed]     
...

the function registration itself is very basic/standard:

const preSignUpFn = new NodejsFunction(this, "PreSignUpFn", {
	description: "Cognito preSignUp Trigger Function",
	entry: path.join(__dirname, "../src/functions/cognito/preSignUp.ts"),
	runtime: Runtime.NODEJS_22_X,
	handler: "handler",
	bundling: {
		bundleAwsSDK: true
	}
});

Regression Issue

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

Last Known Working CDK Library Version

aws-cdk-lib 2.222.0

Expected Behavior

No warnings shown

Current Behavior

Warnings "ValidationError: The result of fromAwsManagedPolicyName can not be used in this API" shown for each lambda function

Reproduction Steps

Create lambda function using NodejsFunction:

const preSignUpFn = new NodejsFunction(this, "PreSignUpFn", {
	description: "Cognito preSignUp Trigger Function",
	entry: path.join(__dirname, "../src/functions/cognito/preSignUp.ts"),
	runtime: Runtime.NODEJS_22_X,
	handler: "handler",
	bundling: {
		bundleAwsSDK: true
	}
});

source code doesn't matter, empty handler function could be used

Possible Solution

No response

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.224.0

AWS CDK CLI version

2.1031.2

Node.js Version

22.17.0

OS

Windows 10

Language

TypeScript

Language Version

5.6.3

Other information

  • No response

aramir avatar Nov 16 '25 06:11 aramir

I'm seeing the same.

cloudopolis avatar Nov 16 '25 16:11 cloudopolis

Same here. The minimum reproducible code seems to be:

const role = new iam.Role(this, 'Role', {
  assumedBy: new iam.AccountRootPrincipal(),
  managedPolicies: [iam.ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess')],
});

[Warning at /TestStack/Role] Failed to add construct metadata for node [Role]. Reason: ValidationError: The result of fromAwsManagedPolicyName can not be used in this API [ack: @aws-cdk/core:addConstructMetadataFailed]

Tietew avatar Nov 17 '25 04:11 Tietew

Looking into this issue now. If you have the same issue, please click 👍 to let us know.

pahud avatar Nov 17 '25 15:11 pahud

Hi @aramir cc @cloudopolis @Tietew

Thank you for reporting this issue. To help us investigate further, could you please check if you have the @aws-cdk/core:enableAdditionalMetadataCollection feature flag explicitly enabled in your cdk.json file?

We've investigated the upgrade path from v2.178.0 to v2.224.0 and confirmed that this feature flag is not automatically enabled during upgrades. The flag only appears in projects that were:

  1. Created with cdk init after v2.178.0 (where it's included in the template), or
  2. Had the flag manually added to cdk.json

Our testing shows: • ✅ Upgrading from v2.178.0 → v2.224.0 without the flag: No warnings • ⚠️ With the flag explicitly enabled: The warning appears

The warning itself appears to be a bug in the metadata collection code when dealing with imported resources like ManagedPolicy.fromAwsManagedPolicyName(). However, understanding whether you have this flag enabled will help us determine if this is affecting existing projects or only new ones.

pahud avatar Nov 17 '25 16:11 pahud

From my investigation, the bug seems to be from @aws-cdk/core:enableAdditionalMetadataCollection and the team is aware of this.

Let's discuss the following 3 scenarios:

Scenario 1: Upgrading from pre-2.178.0 → 2.224.0

Will see warnings? ❌ NO

Why? The feature flag @aws-cdk/core:enableAdditionalMetadataCollection defaults to OFF for existing projects. The flag is only ON for new projects created with cdk init after v2.178.0.

Existing project (created before 2.178.0)
  ↓
Upgrade to 2.224.0
  ↓
Feature flag = OFF (default for existing projects)
  ↓
No metadata collection = No warnings

Scenario 2: Explicitly turned ON the feature flag

Will see warnings? ✅ YES (if using Lambda functions or other constructs with imported managed policies)

How to suppress?

Option 1: Disable the feature flag

{
  "context": {
    "@aws-cdk/core:enableAdditionalMetadataCollection": false
  }
}

Option 2: Acknowledge the warning in your Stack

export class MyStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Suppress the metadata collection warning for this stack
    cdk.Annotations.of(this).acknowledgeWarning(
      '@aws-cdk/core:addConstructMetadataFailed',
      'Known issue with imported managed policies'
    );

    // ... rest of your stack
  }
}

Note: The context flag "@aws-cdk/core:addConstructMetadataFailed": true does NOT suppress the warning. It only provides an acknowledgment ID in the warning message.

Scenario 3: Fresh cdk init after v2.178.0

Will see warnings? ✅ YES (if using Lambda functions or other constructs with imported managed policies) Why? New projects have the feature flag ON by default. Use the same options as Scenario 2 above to surpass.

pahud avatar Nov 17 '25 17:11 pahud

@pahud perhaps I misunderstood the explanation above, but please note the following:

  • My project has @aws-cdk/core:enableAdditionalMetadataCollection set
  • My project does not use any imported resources.

Here is a minimal repro project: cdk-test.zip, all it has is a single NodejsFunction. Lib aws-cdk-lib is set to ^2.225.0, flag enableAdditionalMetadataCollection is set.

Steps to reproduce:

npm install
cdk synth test-backend

warning output:

[Warning at /test-backend/Test/PreSignUpFn/ServiceRole] Failed to add construct metadata for node [ServiceRole]. Reason: ValidationError: The result of fromAwsManagedPolicyName can not be used in this API [ack: @aws-cdk/core:addConstructMetadataFailed]  

aramir avatar Nov 17 '25 17:11 aramir

(edited reply above, sorry was a mistake with the flag enableAdditionalMetadataCollection)

aramir avatar Nov 17 '25 18:11 aramir

This is because the lambda.Function creation behind the NodejsFunction:

https://github.com/aws/aws-cdk/blob/fa332bc9a7a8afa6ed8da2e533e0d9d5b24fc088/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L962

This confirms that every Lambda function automatically gets the AWSLambdaBasicExecutionRole managed policy attached via ManagedPolicy.fromAwsManagedPolicyName(), even when the user doesn't explicitly use imported resources in their code.

To surpass the warnings:

export class Auth extends Construct {

	constructor(scope: Construct, id: string, props: AuthProps) {
		super(scope, id);

		// Acknowledge the metadata collection warning for imported managed policies
		cdk.Annotations.of(this).acknowledgeWarning('@aws-cdk/core:addConstructMetadataFailed', 'Known issue with imported managed policies in Lambda roles');

		// your NodejsFunction here

	}
}

The key change is on line 21:

cdk.Annotations.of(this).acknowledgeWarning('@aws-cdk/core:addConstructMetadataFailed', 'Known issue with imported managed policies in Lambda roles');

You can have that in your Stack definition for stack-wide suppression as well.

This single line suppresses the warning for this construct or stack and all its children (including the Lambda function's service role).

pahud avatar Nov 17 '25 18:11 pahud

@pahud understood, thank you very much for clarifying! so - at the moment the recommended resolution is to disable the (harmless) warning with acknowledgeWarning until there is a proper fix.

aramir avatar Nov 17 '25 20:11 aramir

bundling: { //including AWS SDK into a function deployment bundle for prod (provides faster cold start) bundleAwsSDK: props.production }

How true is this?

				//including AWS SDK into a function deployment bundle for prod (provides faster cold start)
				bundleAwsSDK: props.production
			}```

VesperQuartz avatar Nov 26 '25 19:11 VesperQuartz

@VesperQuartz

That code was from code-test.zip from @aramir https://github.com/aws/aws-cdk/issues/36078#issuecomment-3543197225

To avoid confusion, I'll remove that code snippets then.

pahud avatar Nov 26 '25 19:11 pahud

@VesperQuartz yep, that comment sneaked when I was trimming down part of an active project for a repro sample. It is true, however the benefit is not that huge to be fair.

aramir avatar Nov 26 '25 23:11 aramir

I have an additional datapoint. We saw massive performance regressions with these warnings and switching from Node 22 to Node 24.

We have a monorepo, and we currently have around ~100 of these warnings. We are on version 2.224.0. The Node upgrade caused massive slowdowns in cdk synth and cdk deploy. On Node 24.1.1: With "@aws-cdk/core:enableAdditionalMetadataCollection": false cdk synth -v -v --strict 43.73s user 13.47s system 65% cpu 1:27.11 total With "@aws-cdk/core:enableAdditionalMetadataCollection": true, cdk synth -v -v --strict 122.27s user 13.58s system 83% cpu 2:43.33 total

We also saw slowdowns on cases without synthesizing (cdk deploy --app cdk.out), going from 4s to 30s. I don't know certain where is this coming from (IO?), the debug logs are not very useful here.

As a workaround I will keep "@aws-cdk/core:enableAdditionalMetadataCollection" set to false.

henrys-funke avatar Dec 03 '25 16:12 henrys-funke

@VesperQuartz yep, that comment sneaked when I was trimming down part of an active project for a repro sample. It is true, however the benefit is not that huge to be fair.

Ok

VesperQuartz avatar Dec 05 '25 18:12 VesperQuartz

I'm using this version 2.224.0 and it failed at cdk ls with following error

/home/jack/workspace/codecommit/cdk-typescript/aws-eks-blueprints-cdk/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/core/lib/private/detached-construct.js:1
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DetachedConstruct=void 0;var constructs_1=()=>{var tmp=require("constructs");return constructs_1=()=>tmp,tmp},errors_1=()=>{var tmp=require("../errors");return errors_1=()=>tmp,tmp};const CONSTRUCT_SYM=Symbol.for("constructs.Construct");class DetachedConstruct extends constructs_1().Construct{errorMessage;constructor(errorMessage){super(null,void 0),this.errorMessage=errorMessage,Object.defineProperty(this,"node",{get(){throw new(errors_1()).UnscopedValidationError(errorMessage)}}),Object.defineProperty(this,CONSTRUCT_SYM,{value:!1,enumerable:!1,writable:!1})}get env(){throw new(errors_1()).UnscopedValidationError(this.errorMessage)}}exports.DetachedConstruct=DetachedConstruct;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          ^
UnscopedValidationError: The result of fromAwsManagedPolicyName can not be used in this API
    at path [undefined]

    at AwsManagedPolicy.get [as node] (/home/jack/workspace/codecommit/cdk-typescript/aws-eks-blueprints-cdk/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/core/lib/private/detached-construct.js:1:507)

I see related commit https://github.com/aws/aws-cdk/pull/36015 cc: @mrgrain

jackmdao avatar Dec 29 '25 08:12 jackmdao

@jackmdao the warnings reported in this issue are unintentional and need fixing.

The exception however should be pre-existing behaviour. Can you share the code that works in 2.223.0 and fails in 2.224.0?

mrgrain avatar Dec 29 '25 09:12 mrgrain

Hi @mrgrain Here is the public project https://github.com/awslabs/cdk-eks-blueprints/tree/blueprints-1.17.2, steps to reproduce

  1. Clone the repo
  2. Go to the repo
cd cdk-eks-blueprints
sed -i 's/"aws-cdk-lib":.*/"aws-cdk-lib": "2.233.0"/g' package.json
npm i
cdk ls

/home/jack/workspace/github/cdk-eks-blueprints/node_modules/aws-cdk-lib/core/lib/private/detached-construct.js:1
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DetachedConstruct=void 0;var constructs_1=()=>{var tmp=require("constructs");return constructs_1=()=>tmp,tmp},errors_1=()=>{var tmp=require("../errors");return errors_1=()=>tmp,tmp};const CONSTRUCT_SYM=Symbol.for("constructs.Construct");class DetachedConstruct extends constructs_1().Construct{errorMessage;constructor(errorMessage){super(null,void 0),this.errorMessage=errorMessage,Object.defineProperty(this,"node",{get(){throw new(errors_1()).UnscopedValidationError(errorMessage)}}),Object.defineProperty(this,CONSTRUCT_SYM,{value:!1,enumerable:!1,writable:!1})}get env(){throw new(errors_1()).UnscopedValidationError(this.errorMessage)}}exports.DetachedConstruct=DetachedConstruct;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          ^
UnscopedValidationError: The result of fromAwsManagedPolicyName can not be used in this API
    at path [undefined]

    at AwsManagedPolicy.get [as node] (/home/jack/workspace/github/cdk-eks-blueprints/node_modules/aws-cdk-lib/core/lib/private/detached-construct.js:1:507)
    at /home/jack/workspace/github/cdk-eks-blueprints/node_modules/lodash/lodash.js:2730:27
    at arrayEach (/home/jack/workspace/github/cdk-eks-blueprints/node_modules/lodash/lodash.js:530:11)
    at baseClone (/home/jack/workspace/github/cdk-eks-blueprints/node_modules/lodash/lodash.js:2727:7)
    at /home/jack/workspace/github/cdk-eks-blueprints/node_modules/lodash/lodash.js:2733:34
    ... collapsed 3 duplicate lines matching above lines ...
    at arrayEach (/home/jack/workspace/github/cdk-eks-blueprints/node_modules/lodash/lodash.js:530:11)
    at baseClone (/home/jack/workspace/github/cdk-eks-blueprints/node_modules/lodash/lodash.js:2727:7)

vumdao avatar Jan 05 '26 11:01 vumdao

@henrys-funke

I have an additional datapoint. We saw massive performance regressions with these warnings and switching from Node 22 to Node 24.

Would appreciate if you open a separate issue for this. I'm guessing the warnings itself are not the cause of the performance regression, but the metadata collection.

mrgrain avatar Jan 12 '26 12:01 mrgrain

@henrys-funke

I have an additional datapoint. We saw massive performance regressions with these warnings and switching from Node 22 to Node 24.

Would appreciate if you open a separate issue for this. I'm guessing the warnings itself are not the cause of the performance regression, but the metadata collection.

Done, see https://github.com/aws/aws-cdk/issues/36669

henrys-funke avatar Jan 12 '26 13:01 henrys-funke

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

github-actions[bot] avatar Jan 13 '26 09:01 github-actions[bot]