From 19ee46d7653894f0669aff3872c6c5314be0666c Mon Sep 17 00:00:00 2001 From: GZ Date: Thu, 3 Oct 2024 15:53:06 -0700 Subject: [PATCH] fix(lambda-nodejs): remove smithy models from bundling for AWS SDK v3 runtimes (under feature flag) (#31639) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Issue # (if applicable) Closes https://github.com/aws/aws-cdk/issues/31610 ### Reason for this change for Node 18+ runtimes, since AWS Lambda includes AWS SDK v3 by default, and CDK excludes all the `@aws-sdk/*` packages because they’re expected to already be present. However, the CDK currently removes only the `@aws-sdk/*` packages when bundling for Node 18+ runtimes, but it does not remove the `@smithy/*` packages. This can cause a mismatch in versions between the `@smithy/*` packages and the AWS SDK packages that AWS Lambda provides. The mismatch can happen in the following scenarios. This is a pretty edge case but customers did encounter this issue. ``` /user-app/node_modules/ - /@smithy/* (v123) <-- this gets used because it wasn't deleted - /@aws-sdk/* (v123) <-- CDK removes `@aws-sdk/*` currently /lambda-hidden-folder/node_modules - /@smithy/* (v456) - /@aws-sdk/* (v456) <-- this gets used as fallback since the module is removed from node_modules by CDK ``` ### Description of changes Add a feature flag. When feature flag is set to true, we will also remove smithy models. ### Description of how you validated changes Added unit tests and integration tests. ### Checklist - [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- ...efaultTestDeployAssert8BE0D2D1.assets.json | 19 ++ ...aultTestDeployAssert8BE0D2D1.template.json | 36 +++ .../index.js | 109 +++++++++ ...ambda-nodejs-18-exclude-smithy.assets.json | 32 +++ ...bda-nodejs-18-exclude-smithy.template.json | 91 ++++++++ .../cdk.out | 1 + .../integ.json | 13 ++ .../manifest.json | 121 ++++++++++ .../tree.json | 221 ++++++++++++++++++ .../integ.function-exclude-smithy-models.ts | 32 +++ .../aws-lambda-nodejs/lib/bundling.ts | 12 +- .../aws-lambda-nodejs/test/bundling.test.ts | 112 +++++++++ packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md | 21 +- packages/aws-cdk-lib/cx-api/README.md | 18 ++ packages/aws-cdk-lib/cx-api/lib/features.ts | 16 ++ 15 files changed, 852 insertions(+), 2 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/asset.5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270/index.js create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets.json new file mode 100644 index 0000000000000..901b7c868eaaf --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets.json @@ -0,0 +1,19 @@ +{ + "version": "38.0.1", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/asset.5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270/index.js b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/asset.5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270/index.js new file mode 100644 index 0000000000000..68bb5aa4d3629 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/asset.5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270/index.js @@ -0,0 +1,109 @@ +"use strict"; +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// node_modules/delay/index.js +var require_delay = __commonJS({ + "node_modules/delay/index.js"(exports2, module2) { + "use strict"; + var randomInteger = (minimum, maximum) => Math.floor(Math.random() * (maximum - minimum + 1) + minimum); + var createAbortError = () => { + const error = new Error("Delay aborted"); + error.name = "AbortError"; + return error; + }; + var createDelay = ({ clearTimeout: defaultClear, setTimeout: set, willResolve }) => (ms, { value, signal } = {}) => { + if (signal && signal.aborted) { + return Promise.reject(createAbortError()); + } + let timeoutId; + let settle; + let rejectFn; + const clear = defaultClear || clearTimeout; + const signalListener = () => { + clear(timeoutId); + rejectFn(createAbortError()); + }; + const cleanup = () => { + if (signal) { + signal.removeEventListener("abort", signalListener); + } + }; + const delayPromise = new Promise((resolve, reject) => { + settle = () => { + cleanup(); + if (willResolve) { + resolve(value); + } else { + reject(value); + } + }; + rejectFn = reject; + timeoutId = (set || setTimeout)(settle, ms); + }); + if (signal) { + signal.addEventListener("abort", signalListener, { once: true }); + } + delayPromise.clear = () => { + clear(timeoutId); + timeoutId = null; + settle(); + }; + return delayPromise; + }; + var createWithTimers = (clearAndSet) => { + const delay3 = createDelay({ ...clearAndSet, willResolve: true }); + delay3.reject = createDelay({ ...clearAndSet, willResolve: false }); + delay3.range = (minimum, maximum, options) => delay3(randomInteger(minimum, maximum), options); + return delay3; + }; + var delay2 = createWithTimers(); + delay2.createWithTimers = createWithTimers; + module2.exports = delay2; + module2.exports.default = delay2; + } +}); + +// packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ-handlers/dependencies.ts +var dependencies_exports = {}; +__export(dependencies_exports, { + handler: () => handler +}); +module.exports = __toCommonJS(dependencies_exports); +var import_delay = __toESM(require_delay()); +async function handler() { + await (0, import_delay.default)(5); + console.log("log after delay"); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + handler +}); diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.assets.json new file mode 100644 index 0000000000000..6f6ef06d1d893 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.assets.json @@ -0,0 +1,32 @@ +{ + "version": "38.0.1", + "files": { + "5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270": { + "source": { + "path": "asset.5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270", + "packaging": "zip" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270.zip", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + }, + "54091fa5a5542ba4403c588e9d11556a4018017763ece0000b6b125ed2217a83": { + "source": { + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "54091fa5a5542ba4403c588e9d11556a4018017763ece0000b6b125ed2217a83.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.template.json new file mode 100644 index 0000000000000..16a7561d85c86 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk-integ-lambda-nodejs-18-exclude-smithy.template.json @@ -0,0 +1,91 @@ +{ + "Resources": { + "NodeJsFunctionServiceRole9AE046B6": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "NodeJsFunction6DD2A8DD": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": { + "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" + }, + "S3Key": "5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270.zip" + }, + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "NodeJsFunctionServiceRole9AE046B6", + "Arn" + ] + }, + "Runtime": "nodejs18.x" + }, + "DependsOn": [ + "NodeJsFunctionServiceRole9AE046B6" + ] + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk.out new file mode 100644 index 0000000000000..c6e612584e352 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"38.0.1"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/integ.json new file mode 100644 index 0000000000000..6d5e8dc74075f --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/integ.json @@ -0,0 +1,13 @@ +{ + "version": "38.0.1", + "testCases": { + "NodeJsFunctionExcludeSmithyInteg/DefaultTest": { + "stacks": [ + "cdk-integ-lambda-nodejs-18-exclude-smithy" + ], + "diffAssets": true, + "assertionStack": "NodeJsFunctionExcludeSmithyInteg/DefaultTest/DeployAssert", + "assertionStackName": "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/manifest.json new file mode 100644 index 0000000000000..3ee12d0c064ef --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/manifest.json @@ -0,0 +1,121 @@ +{ + "version": "38.0.1", + "artifacts": { + "cdk-integ-lambda-nodejs-18-exclude-smithy.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "cdk-integ-lambda-nodejs-18-exclude-smithy.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "cdk-integ-lambda-nodejs-18-exclude-smithy": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "cdk-integ-lambda-nodejs-18-exclude-smithy.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "notificationArns": [], + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/54091fa5a5542ba4403c588e9d11556a4018017763ece0000b6b125ed2217a83.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "cdk-integ-lambda-nodejs-18-exclude-smithy.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "cdk-integ-lambda-nodejs-18-exclude-smithy.assets" + ], + "metadata": { + "/cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "NodeJsFunctionServiceRole9AE046B6" + } + ], + "/cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "NodeJsFunction6DD2A8DD" + } + ], + "/cdk-integ-lambda-nodejs-18-exclude-smithy/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/cdk-integ-lambda-nodejs-18-exclude-smithy/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "cdk-integ-lambda-nodejs-18-exclude-smithy" + }, + "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "notificationArns": [], + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "NodeJsFunctionExcludeSmithyIntegDefaultTestDeployAssert8BE0D2D1.assets" + ], + "metadata": { + "/NodeJsFunctionExcludeSmithyInteg/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/NodeJsFunctionExcludeSmithyInteg/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "NodeJsFunctionExcludeSmithyInteg/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/tree.json new file mode 100644 index 0000000000000..e8f6df5cde5c9 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.js.snapshot/tree.json @@ -0,0 +1,221 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "cdk-integ-lambda-nodejs-18-exclude-smithy": { + "id": "cdk-integ-lambda-nodejs-18-exclude-smithy", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy", + "children": { + "NodeJsFunction": { + "id": "NodeJsFunction", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Code": { + "id": "Code", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/Code", + "children": { + "Stage": { + "id": "Stage", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/Code/Stage", + "constructInfo": { + "fqn": "aws-cdk-lib.AssetStaging", + "version": "0.0.0" + } + }, + "AssetBucket": { + "id": "AssetBucket", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/Code/AssetBucket", + "constructInfo": { + "fqn": "aws-cdk-lib.aws_s3.BucketBase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_s3_assets.Asset", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/NodeJsFunction/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "s3Bucket": { + "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" + }, + "s3Key": "5f1efdee2910b237bc92c947c2621e653fadf9ce9ef02e797da9ec166a2d4270.zip" + }, + "handler": "index.handler", + "role": { + "Fn::GetAtt": [ + "NodeJsFunctionServiceRole9AE046B6", + "Arn" + ] + }, + "runtime": "nodejs18.x" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda_nodejs.NodejsFunction", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "cdk-integ-lambda-nodejs-18-exclude-smithy/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "NodeJsFunctionExcludeSmithyInteg": { + "id": "NodeJsFunctionExcludeSmithyInteg", + "path": "NodeJsFunctionExcludeSmithyInteg", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "NodeJsFunctionExcludeSmithyInteg/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "NodeJsFunctionExcludeSmithyInteg/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "NodeJsFunctionExcludeSmithyInteg/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "NodeJsFunctionExcludeSmithyInteg/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "NodeJsFunctionExcludeSmithyInteg/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.ts new file mode 100644 index 0000000000000..40431c1a62383 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.function-exclude-smithy-models.ts @@ -0,0 +1,32 @@ +import * as path from 'path'; +import { App, Stack, StackProps } from 'aws-cdk-lib'; +import { Construct } from 'constructs'; +import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import { IFunction, Runtime } from 'aws-cdk-lib/aws-lambda'; +import { LAMBDA_NODEJS_SDK_V3_EXCLUDE_SMITHY_PACKAGES } from 'aws-cdk-lib/cx-api'; + +class TestStack extends Stack { + public lambdaFunction: IFunction; + + constructor(scope: Construct, id: string, props?: StackProps) { + super(scope, id, props); + + this.lambdaFunction = new lambda.NodejsFunction(this, 'NodeJsFunction', { + entry: path.join(__dirname, 'integ-handlers/dependencies.ts'), + runtime: Runtime.NODEJS_18_X, + }); + } +} + +const app = new App({ + postCliContext: { + [LAMBDA_NODEJS_SDK_V3_EXCLUDE_SMITHY_PACKAGES]: true, + }, +}); +const stack = new TestStack(app, 'cdk-integ-lambda-nodejs-18-exclude-smithy'); +new IntegTest(app, 'NodeJsFunctionExcludeSmithyInteg', { + testCases: [stack], + diffAssets: true, +}); + 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 e91171b851874..e021f7e491360 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts @@ -7,6 +7,7 @@ import { BundlingOptions, OutputFormat, SourceMapMode } from './types'; import { exec, extractDependencies, findUp, getTsconfigCompilerOptions, isSdkV2Runtime } from './util'; import { Architecture, AssetCode, Code, Runtime } from '../../aws-lambda'; import * as cdk from '../../core'; +import { LAMBDA_NODEJS_SDK_V3_EXCLUDE_SMITHY_PACKAGES } from '../../cx-api'; const ESBUILD_MAJOR_VERSION = '0'; const ESBUILD_DEFAULT_VERSION = '0.21'; @@ -125,10 +126,19 @@ export class Bundling implements cdk.BundlingOptions { throw new Error(`ECMAScript module output format is not supported by the ${props.runtime.name} runtime`); } + /** + * For Lambda runtime that uses AWS SDK v3, we need to remove both `aws-sdk/*` modules + * and `smithy/*` modules to prevent version mismatches. Hide it behind feature flag + * to make sure no breaking change is introduced. + * + * Issue reference: https://github.com/aws/aws-cdk/issues/31610#issuecomment-2389983347 + */ + const sdkV3Externals = cdk.FeatureFlags.of(scope).isEnabled(LAMBDA_NODEJS_SDK_V3_EXCLUDE_SMITHY_PACKAGES) ? + ['@aws-sdk/*', '@smithy/*'] : ['@aws-sdk/*']; // Modules to externalize when using a constant known version of the runtime. // Mark aws-sdk as external by default (available in the runtime) const isV2Runtime = isSdkV2Runtime(props.runtime); - const versionedExternals = isV2Runtime ? ['aws-sdk'] : ['@aws-sdk/*']; + const versionedExternals = isV2Runtime ? ['aws-sdk'] : sdkV3Externals; // Don't automatically externalize any dependencies when using a `latest` runtime which may // update versions in the future. // Don't automatically externalize aws sdk if `bundleAwsSDK` is true so it can be 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 ae44f18650056..1abdb8d31e331 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 @@ -313,6 +313,118 @@ test('esbuild bundling source map default', () => { }); }); +test.each([ + [Runtime.NODEJS_18_X, 'node18'], + [Runtime.NODEJS_20_X, 'node20'], +]) ('esbuild bundling without aws-sdk v3 and smithy with feature flag enabled using Node 18+', (runtime, target) => { + const cdkApp = new App({ + context: { + '@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages': true, + }, + }); + const cdkStack = new Stack(cdkApp, 'MyTestStack'); + Bundling.bundle(cdkStack, { + entry, + projectRoot, + depsLockFilePath, + runtime: runtime, + architecture: Architecture.X86_64, + }); + + // Correctly bundles with esbuild + expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), { + assetHashType: AssetHashType.OUTPUT, + bundling: expect.objectContaining({ + command: [ + 'bash', '-c', + `esbuild --bundle "/asset-input/lib/handler.ts" --target=${target} --platform=node --outfile="/asset-output/index.js" --external:@aws-sdk/* --external:@smithy/*`, + ], + }), + }); +}); + +test('esbuild bundling with bundleAwsSdk true with feature flag enabled using Node 18+', () => { + const cdkApp = new App({ + context: { + '@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages': true, + }, + }); + const cdkStack = new Stack(cdkApp, 'MyTestStack'); + Bundling.bundle(cdkStack, { + entry, + projectRoot, + depsLockFilePath, + bundleAwsSDK: true, + runtime: Runtime.NODEJS_18_X, + architecture: Architecture.X86_64, + }); + + // Correctly bundles with esbuild + expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), { + assetHashType: AssetHashType.OUTPUT, + bundling: expect.objectContaining({ + command: [ + 'bash', '-c', + `esbuild --bundle "/asset-input/lib/handler.ts" --target=${STANDARD_TARGET} --platform=node --outfile="/asset-output/index.js"`, + ], + }), + }); +}); + +test('esbuild bundling with feature flag enabled using Node Latest', () => { + const cdkApp = new App({ + context: { + '@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages': true, + }, + }); + const cdkStack = new Stack(cdkApp, 'MyTestStack'); + Bundling.bundle(cdkStack, { + entry, + projectRoot, + depsLockFilePath, + runtime: Runtime.NODEJS_LATEST, + architecture: Architecture.X86_64, + }); + + // Correctly bundles with esbuild + expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), { + assetHashType: AssetHashType.OUTPUT, + bundling: expect.objectContaining({ + command: [ + 'bash', '-c', + `esbuild --bundle "/asset-input/lib/handler.ts" --target=${STANDARD_TARGET} --platform=node --outfile="/asset-output/index.js"`, + ], + }), + }); +}); + +test('esbuild bundling with feature flag enabled using Node 16', () => { + const cdkApp = new App({ + context: { + '@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages': true, + }, + }); + const cdkStack = new Stack(cdkApp, 'MyTestStack'); + Bundling.bundle(cdkStack, { + entry, + projectRoot, + depsLockFilePath, + runtime: Runtime.NODEJS_16_X, + architecture: Architecture.X86_64, + }); + + // Correctly bundles with esbuild + expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), { + assetHashType: AssetHashType.OUTPUT, + bundling: expect.objectContaining({ + command: [ + 'bash', '-c', + 'esbuild --bundle "/asset-input/lib/handler.ts" --target=node16 --platform=node --outfile="/asset-output/index.js" --external:aws-sdk', + ], + }), + }); +}); + test('esbuild bundling without aws-sdk v3 when use greater than or equal Runtime.NODEJS_18_X', () => { Bundling.bundle(stack, { entry, diff --git a/packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md b/packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md index c2813160d9f03..f6bfea7c4de12 100644 --- a/packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md +++ b/packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md @@ -76,6 +76,7 @@ Flags come in three types: | [@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions](#aws-cdkaws-ecsreduceec2fargatecloudwatchpermissions) | When enabled, we will only grant the necessary permissions when users specify cloudwatch log group through logConfiguration | 2.159.0 | (fix) | | [@aws-cdk/aws-ec2:ec2SumTImeoutEnabled](#aws-cdkaws-ec2ec2sumtimeoutenabled) | When enabled, initOptions.timeout and resourceSignalTimeout values will be summed together. | 2.160.0 | (fix) | | [@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission](#aws-cdkaws-appsyncappsyncgraphqlapiscopelambdapermission) | When enabled, a Lambda authorizer Permission created when using GraphqlApi will be properly scoped with a SourceArn. | V2NEXT | (fix) | +| [@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages](#aws-cdkaws-lambda-nodejssdkv3excludesmithypackages) | When enabled, both `@aws-sdk` and `@smithy` packages will be excluded from the Lambda Node.js 18.x runtime to prevent version mismatches in bundled applications. | V2NEXT | (fix) | | [@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId](#aws-cdkaws-rdssetcorrectvaluefordatabaseinstancereadreplicainstanceresourceid) | When enabled, the value of property `instanceResourceId` in construct `DatabaseInstanceReadReplica` will be set to the correct value which is `DbiResourceId` instead of currently `DbInstanceArn` | V2NEXT | (fix) | | [@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics](#aws-cdkcorecfnincluderejectcomplexresourceupdatecreatepolicyintrinsics) | When enabled, CFN templates added with `cfn-include` will error if the template contains Resource Update or Create policies with CFN Intrinsics that include non-primitive values. | V2NEXT | (fix) | @@ -144,7 +145,8 @@ The following json shows the current recommended set of flags, as `cdk init` wou "@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true, "@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true, "@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId": true, - "@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics": true + "@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics": true, + "@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true } } ``` @@ -1440,6 +1442,23 @@ specific AppSync GraphQL API. | V2NEXT | `false` | `true` | +### @aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages + +*When enabled, both `@aws-sdk` and `@smithy` packages will be excluded from the Lambda Node.js 18.x runtime to prevent version mismatches in bundled applications.* (fix) + +Currently, when bundling Lambda functions with the non-latest runtime that supports AWS SDK JavaScript (v3), only the '@aws-sdk/*' packages are excluded by default. +However, this can cause version mismatches between the '@aws-sdk/*' and '@smithy/*' packages, as they are tightly coupled dependencies in AWS SDK v3. + +When this feature flag is enabled, both '@aws-sdk/*' and '@smithy/*' packages will be excluded during the bundling process. This ensures that no mismatches +occur between these tightly coupled dependencies when using the AWS SDK v3 in Lambda functions. + + +| Since | Default | Recommended | +| ----- | ----- | ----- | +| (not in v1) | | | +| V2NEXT | `false` | `true` | + + ### @aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId *When enabled, the value of property `instanceResourceId` in construct `DatabaseInstanceReadReplica` will be set to the correct value which is `DbiResourceId` instead of currently `DbInstanceArn`* (fix) diff --git a/packages/aws-cdk-lib/cx-api/README.md b/packages/aws-cdk-lib/cx-api/README.md index 9c5d801284bf4..68a16be402040 100644 --- a/packages/aws-cdk-lib/cx-api/README.md +++ b/packages/aws-cdk-lib/cx-api/README.md @@ -457,3 +457,21 @@ _cdk.json_ } } ``` + +* `@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages` + +Currently, when bundling Lambda functions with the non-latest runtime that supports AWS SDK JavaScript (v3), only the `@aws-sdk/*` packages are excluded by default. +However, this can cause version mismatches between the `@aws-sdk/*` and `@smithy/*` packages, as they are tightly coupled dependencies in AWS SDK v3. + +When this feature flag is enabled, both `@aws-sdk/*` and `@smithy/*` packages will be excluded during the bundling process. This ensures that no mismatches +occur between these tightly coupled dependencies when using the AWS SDK v3 in Lambda functions. + +_cdk.json_ + +```json +{ + "context": { + "@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true + } +} +``` diff --git a/packages/aws-cdk-lib/cx-api/lib/features.ts b/packages/aws-cdk-lib/cx-api/lib/features.ts index f31d2d3b78687..4bf2ad083cc33 100644 --- a/packages/aws-cdk-lib/cx-api/lib/features.ts +++ b/packages/aws-cdk-lib/cx-api/lib/features.ts @@ -112,6 +112,7 @@ export const EC2_SUM_TIMEOUT_ENABLED = '@aws-cdk/aws-ec2:ec2SumTImeoutEnabled'; export const APPSYNC_GRAPHQLAPI_SCOPE_LAMBDA_FUNCTION_PERMISSION = '@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission'; export const USE_CORRECT_VALUE_FOR_INSTANCE_RESOURCE_ID_PROPERTY = '@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId'; export const CFN_INCLUDE_REJECT_COMPLEX_RESOURCE_UPDATE_CREATE_POLICY_INTRINSICS = '@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics'; +export const LAMBDA_NODEJS_SDK_V3_EXCLUDE_SMITHY_PACKAGES = '@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages'; export const FLAGS: Record = { ////////////////////////////////////////////////////////////////////// @@ -1203,6 +1204,21 @@ export const FLAGS: Record = { recommendedValue: true, introducedIn: { v2: 'V2NEXT' }, }, + + ////////////////////////////////////////////////////////////////////// + [LAMBDA_NODEJS_SDK_V3_EXCLUDE_SMITHY_PACKAGES]: { + type: FlagType.BugFix, + summary: 'When enabled, both `@aws-sdk` and `@smithy` packages will be excluded from the Lambda Node.js 18.x runtime to prevent version mismatches in bundled applications.', + detailsMd: ` + Currently, when bundling Lambda functions with the non-latest runtime that supports AWS SDK JavaScript (v3), only the '@aws-sdk/*' packages are excluded by default. + However, this can cause version mismatches between the '@aws-sdk/*' and '@smithy/*' packages, as they are tightly coupled dependencies in AWS SDK v3. + + When this feature flag is enabled, both '@aws-sdk/*' and '@smithy/*' packages will be excluded during the bundling process. This ensures that no mismatches + occur between these tightly coupled dependencies when using the AWS SDK v3 in Lambda functions. + `, + introducedIn: { v2: 'V2NEXT' }, + recommendedValue: true, + }, }; const CURRENT_MV = 'v2';