diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts index 30f0539ce2e6e..c5bd7c4243e47 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts @@ -137,7 +137,7 @@ export class Bundling implements cdk.BundlingOptions { // their environment. if (isV2Runtime && externals.some((pkgName) => pkgName.startsWith('@aws-sdk/'))) { cdk.Annotations.of(scope).addWarningV2('@aws-cdk/aws-lambda-nodejs:sdkV3NotInRuntime', 'If you are relying on AWS SDK v3 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 18 or higher.'); - } else if (externals.includes('aws-sdk')) { + } else if (!isV2Runtime && externals.includes('aws-sdk')) { cdk.Annotations.of(scope).addWarningV2('@aws-cdk/aws-lambda-nodejs:sdkV2NotInRuntime', 'If you are relying on AWS SDK v2 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 16 or lower.'); } diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts index 4d86d89e157d0..af162ed7b5fa0 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts @@ -882,6 +882,24 @@ test('bundling with <= Node16 warns when sdk v3 is external', () => { ); }); +test('bundling with <= Node16 does not warn with default externalModules', () => { + const myStack = new Stack(app, 'MyTestStack2'); + Bundling.bundle(myStack, { + entry, + projectRoot, + depsLockFilePath, + runtime: Runtime.NODEJS_16_X, + architecture: Architecture.X86_64, + }); + + Annotations.fromStack(myStack).hasNoWarning('*', + 'If you are relying on AWS SDK v3 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 18 or higher. [ack: @aws-cdk/aws-lambda-nodejs:sdkV3NotInRuntime]', + ); + Annotations.fromStack(myStack).hasNoWarning('*', + 'If you are relying on AWS SDK v2 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 16 or lower. [ack: @aws-cdk/aws-lambda-nodejs:sdkV2NotInRuntime]', + ); +}); + test('bundling with >= Node18 warns when sdk v3 is external', () => { Bundling.bundle(stack, { entry, @@ -897,6 +915,24 @@ test('bundling with >= Node18 warns when sdk v3 is external', () => { ); }); +test('bundling with >= Node18 does not warn with default externalModules', () => { + const myStack = new Stack(app, 'MyTestStack3'); + Bundling.bundle(myStack, { + entry, + projectRoot, + depsLockFilePath, + runtime: Runtime.NODEJS_18_X, + architecture: Architecture.X86_64, + }); + + Annotations.fromStack(myStack).hasNoWarning('*', + 'If you are relying on AWS SDK v3 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 18 or higher. [ack: @aws-cdk/aws-lambda-nodejs:sdkV3NotInRuntime]', + ); + Annotations.fromStack(myStack).hasNoWarning('*', + 'If you are relying on AWS SDK v2 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 16 or lower. [ack: @aws-cdk/aws-lambda-nodejs:sdkV2NotInRuntime]', + ); +}); + test('bundling with NODEJS_LATEST warns when any dependencies are external', () => { Bundling.bundle(stack, { entry,