Skip to content

Commit

Permalink
fix(lambda-nodejs): non-deterministic runtime version (#14538)
Browse files Browse the repository at this point in the history
Use `NODEJS_14_X` as default runtime for `NodejsFunction`.

Closes #13893

BREAKING CHANGE: the default runtime version for `NodejsFunction` is now always `NODEJS_14_X` (previously the version was derived from the local NodeJS runtime and could be either 12.x or 14.x).

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored May 5, 2021
1 parent 1a97b66 commit 527f662
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 20 deletions.
10 changes: 3 additions & 7 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import { Bundling } from './bundling';
import { BundlingOptions } from './types';
import { callsites, findUp, LockFile, nodeMajorVersion } from './util';
import { callsites, findUp, LockFile } from './util';

// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line no-duplicate-imports, import/order
Expand Down Expand Up @@ -34,8 +34,7 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions {
* The runtime environment. Only runtimes of the Node.js family are
* supported.
*
* @default - `NODEJS_14_X` if `process.versions.node` >= '14.0.0',
* `NODEJS_12_X` otherwise.
* @default Runtime.NODEJS_14_X
*/
readonly runtime?: lambda.Runtime;

Expand Down Expand Up @@ -105,10 +104,7 @@ export class NodejsFunction extends lambda.Function {
// Entry and defaults
const entry = path.resolve(findEntry(id, props.entry));
const handler = props.handler ?? 'handler';
const defaultRunTime = nodeMajorVersion() >= 14
? lambda.Runtime.NODEJS_14_X
: lambda.Runtime.NODEJS_12_X;
const runtime = props.runtime ?? defaultRunTime;
const runtime = props.runtime ?? lambda.Runtime.NODEJS_14_X;

super(scope, id, {
...props,
Expand Down
7 changes: 0 additions & 7 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ export function callsites(): CallSite[] {
return stack as unknown as CallSite[];
}

/**
* Returns the major version of node installation
*/
export function nodeMajorVersion(): number {
return parseInt(process.versions.node.split('.')[0], 10);
}

/**
* Find a file by walking up parent directories
*/
Expand Down
7 changes: 1 addition & 6 deletions packages/@aws-cdk/aws-lambda-nodejs/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { CodeConfig, Runtime } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { NodejsFunction } from '../lib';
import { Bundling } from '../lib/bundling';
import { nodeMajorVersion } from '../lib/util';

jest.mock('../lib/bundling', () => {
return {
Expand Down Expand Up @@ -41,13 +40,9 @@ test('NodejsFunction with .ts handler', () => {
entry: expect.stringContaining('function.test.handler1.ts'), // Automatically finds .ts handler file
}));

const runtime = nodeMajorVersion() >= 14
? Runtime.NODEJS_14_X
: Runtime.NODEJS_12_X;

expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'index.handler',
Runtime: runtime.name,
Runtime: 'nodejs14.x',
});
});

Expand Down

0 comments on commit 527f662

Please sign in to comment.