Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda-nodejs): AWS SDK v2 is not available for node18.x runtime #22989

Merged
merged 9 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Bundling implements cdk.BundlingOptions {
}

this.externals = [
...props.externalModules ?? ['aws-sdk'], // Mark aws-sdk as external by default (available in the runtime)
...props.externalModules ?? (isSdkV2Runtime(props.runtime) ? ['aws-sdk'] : ['@aws-sdk/*']), // Mark aws-sdk as external by default (available in the runtime)
...props.nodeModules ?? [], // Mark the modules that we are going to install as externals also
];

Expand Down Expand Up @@ -367,3 +367,17 @@ function toCliArgs(esbuildArgs: { [key: string]: string | boolean }): string {

return args.join(' ');
}

function isSdkV2Runtime(runtime: Runtime): boolean {
const sdkV2RuntimeList = [
Runtime.NODEJS,
Runtime.NODEJS_4_3,
Runtime.NODEJS_6_10,
Runtime.NODEJS_8_10,
Runtime.NODEJS_10_X,
Runtime.NODEJS_12_X,
Runtime.NODEJS_14_X,
Runtime.NODEJS_16_X,
];
return sdkV2RuntimeList.includes(runtime);
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export interface BundlingOptions {
* A list of modules that should be considered as externals (already available
* in the runtime).
*
* @default ['aws-sdk']
* @default - ['aws-sdk'] if the runtime is < Node.js 18.x, ['@aws-sdk/*'] otherwise.
*/
readonly externalModules?: string[];

Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,27 @@ test('esbuild bundling source map default', () => {
});
});

test('esbuild bundling without aws-sdk v3 when use greater than or equal Runtime.NODEJS_18_X', () => {
Bundling.bundle({
entry,
projectRoot,
depsLockFilePath,
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=node18 --platform=node --outfile="/asset-output/index.js" --external:@aws-sdk/*',
],
}),
});
});

test('esbuild bundling source map inline', () => {
Bundling.bundle({
entry,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable no-console */
// @ts-ignore
import { S3Client } from '@aws-sdk/client-s3'; // eslint-disable-line import/no-extraneous-dependencies, import/no-unresolved

const s3 = new S3Client();

export async function handler() {
console.log(s3);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading