Skip to content

Commit 8a7dffd

Browse files
authored
fix(lambda-nodejs): aws-sdk version detection broken for self-defined runtimes (#23416)
We have single-sourced our node versions in our repositories into a single `.nvmrc` at the root of the repo. From there, we create the Runtimes, like so: ```typescript const nvmrc = readFileSync(path.join(__dirname, '..', '.nvmrc')).toString().trim() new Runtime(`nodejs${nvmrc}.x'`, RuntimeFamily.NODEJS, { supportsInlineCode: true }) ``` As of #22989 the detection for the appropriate command line flags that dictate which aws-sdk is considered as an external module in esbuild broke for our self-defined Lambda runtimes < node18, as the [codepath](https://github.com/aws/aws-cdk/pull/22989/files#diff-cd86fbd4f2bbefcbcffc2143adccabafa1debe5981edbcdfcc766b5a705fe770R371-R383) detecting runtime versions that ship with the aws-sdk v2 never returned a truthy result. This left us with Lambdas running node 16, that did not bundle the aws-sdk v3, as it was incorrectly set as external in the esbuild command, which should only be the case from node 18 onwards. I am thus suggesting explicitly comparing the runtimes family and name to detect whether the runtime is one shipping the aws-sdk v2, as none of the other features of the Runtime class actually have an effect on the Lambda runtime environment. Related issue: #22976
1 parent ff0070d commit 8a7dffd

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,5 +410,5 @@ function isSdkV2Runtime(runtime: Runtime): boolean {
410410
Runtime.NODEJS_14_X,
411411
Runtime.NODEJS_16_X,
412412
];
413-
return sdkV2RuntimeList.includes(runtime);
413+
return sdkV2RuntimeList.some((r) => {return r.family === runtime.family && r.name === runtime.name;});
414414
}

packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as child_process from 'child_process';
22
import * as os from 'os';
33
import * as path from 'path';
4-
import { Architecture, Code, Runtime } from '@aws-cdk/aws-lambda';
4+
import { Architecture, Code, Runtime, RuntimeFamily } from '@aws-cdk/aws-lambda';
55
import { AssetHashType, DockerImage } from '@aws-cdk/core';
66
import { version as delayVersion } from 'delay/package.json';
77
import { Bundling } from '../lib/bundling';
@@ -329,6 +329,30 @@ test('esbuild bundling source map inline', () => {
329329
});
330330
});
331331

332+
test('esbuild bundling is correctly done with custom runtime matching predefined runtime', () => {
333+
Bundling.bundle({
334+
entry,
335+
projectRoot,
336+
depsLockFilePath,
337+
runtime: new Runtime('nodejs14.x', RuntimeFamily.NODEJS, { supportsInlineCode: true }),
338+
architecture: Architecture.X86_64,
339+
sourceMapMode: SourceMapMode.INLINE,
340+
});
341+
342+
expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), {
343+
assetHashType: AssetHashType.OUTPUT,
344+
bundling: expect.objectContaining({
345+
command: [
346+
'bash', '-c',
347+
[
348+
'esbuild --bundle "/asset-input/lib/handler.ts" --target=node14 --platform=node --outfile="/asset-output/index.js"',
349+
'--sourcemap=inline --external:aws-sdk',
350+
].join(' '),
351+
],
352+
}),
353+
});
354+
});
355+
332356
test('esbuild bundling source map enabled when only source map mode exists', () => {
333357
Bundling.bundle({
334358
entry,

0 commit comments

Comments
 (0)