Skip to content

Commit

Permalink
fix(lambda-nodejs): aws-sdk version detection broken for self-defined…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
LarsFronius authored Jan 20, 2023
1 parent ff0070d commit 8a7dffd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,5 @@ function isSdkV2Runtime(runtime: Runtime): boolean {
Runtime.NODEJS_14_X,
Runtime.NODEJS_16_X,
];
return sdkV2RuntimeList.includes(runtime);
return sdkV2RuntimeList.some((r) => {return r.family === runtime.family && r.name === runtime.name;});
}
26 changes: 25 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as child_process from 'child_process';
import * as os from 'os';
import * as path from 'path';
import { Architecture, Code, Runtime } from '@aws-cdk/aws-lambda';
import { Architecture, Code, Runtime, RuntimeFamily } from '@aws-cdk/aws-lambda';
import { AssetHashType, DockerImage } from '@aws-cdk/core';
import { version as delayVersion } from 'delay/package.json';
import { Bundling } from '../lib/bundling';
Expand Down Expand Up @@ -329,6 +329,30 @@ test('esbuild bundling source map inline', () => {
});
});

test('esbuild bundling is correctly done with custom runtime matching predefined runtime', () => {
Bundling.bundle({
entry,
projectRoot,
depsLockFilePath,
runtime: new Runtime('nodejs14.x', RuntimeFamily.NODEJS, { supportsInlineCode: true }),
architecture: Architecture.X86_64,
sourceMapMode: SourceMapMode.INLINE,
});

expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), {
assetHashType: AssetHashType.OUTPUT,
bundling: expect.objectContaining({
command: [
'bash', '-c',
[
'esbuild --bundle "/asset-input/lib/handler.ts" --target=node14 --platform=node --outfile="/asset-output/index.js"',
'--sourcemap=inline --external:aws-sdk',
].join(' '),
],
}),
});
});

test('esbuild bundling source map enabled when only source map mode exists', () => {
Bundling.bundle({
entry,
Expand Down

0 comments on commit 8a7dffd

Please sign in to comment.