aws / aws/aws-cdk

NodejsFunction: bundler fails with unclear error message when using node 12 runtime

Open
#25,710 5 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-lambda-nodejs bug p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

I'm in the process of upgrading from an old version of CDK `1.x`, to the latest CDK `2.x` (`2.80.0`).

I had some old `NodejsFunction` code that was using `runtime: Runtime.NODEJS_12_X`

**Old code that caused error:**

```ts
this.fnHandler = new NodejsFunction(this, 'Handler', {
description: handlerDescription,
entry: path.join(__dirname, '..', 'functions', handlerPath),
handler: handlerExport,
runtime: Runtime.NODEJS_12_X,
memorySize: 128,
timeout: Duration.seconds(60),
})
```

### Expected Behavior

The framework would give me a more meaningful error message telling me what/where the issue was.

### Current Behavior

```shell
⇒ yarn cdk-dev diff '*'
yarn run v1.22.15
$ yarn -s awsvault-dev:nosession cdk diff '*'
$ /Users/devalias/dev/REDACTED-infrastructure/node_modules/.bin/ts-node bin/REDACTED.ts
[+] Building 19.8s (11/13)
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.30kB 0.0s
=> [internal] load metadata for public.ecr.aws/sam/build-nodejs12.x:latest 2.1s
=> [ 1/10] FROM public.ecr.aws/sam/build-nodejs12.x@sha256:4748f3e85ef16460a48f502433eec0b8282f374bd646553392e7dc27a1c20579 0.0s
=> CACHED [ 2/10] RUN npm install --global yarn@1.22.5 0.0s
=> [ 3/10] RUN npm install --global pnpm@7.30.5 4.2s
=> [ 4/10] RUN npm install --global typescript 3.7s
=> [ 5/10] RUN npm install --global --unsafe-perm=true esbuild@0 8.3s
=> [ 6/10] RUN mkdir /tmp/npm-cache && chmod -R 777 /tmp/npm-cache && npm config --global set cache /tmp/npm-cache 0.5s
=> [ 7/10] RUN mkdir /tmp/yarn-cache && chmod -R 777 /tmp/yarn-cache && yarn config set cache-folder /tmp/yarn-cache 0.7s
=> ERROR [ 8/10] RUN mkdir /tmp/pnpm-cache && chmod -R 777 /tmp/pnpm-cache && pnpm config --global set store-dir /tmp/pnpm-cache 0.3s
------
> [ 8/10] RUN mkdir /tmp/pnpm-cache && chmod -R 777 /tmp/pnpm-cache && pnpm config --global set store-dir /tmp/pnpm-cache:
#0 0.282 ERROR: This version of pnpm requires at least Node.js v14.6
#0 0.282 The current version of Node.js is v12.22.12
#0 0.282 Visit https://r.pnpm.io/comp to see the list of past pnpm versions with respective Node.js version support.
------
Dockerfile:31
--------------------
30 | # Ensure all users can write to pnpm cache
31 | >>> RUN mkdir /tmp/pnpm-cache && \
32 | >>> chmod -R 777 /tmp/pnpm-cache && \
33 | >>> pnpm config --global set store-dir /tmp/pnpm-cache
34 |
--------------------
ERROR: failed to solve: process "/bin/sh -c mkdir /tmp/pnpm-cache && chmod -R 777 /tmp/pnpm-cache && pnpm config --global set store-dir /tmp/pnpm-cache" did not complete successfully: exit code: 1

/Users/devalias/dev/REDACTED-infrastructure/node_modules/aws-cdk-lib/core/lib/private/asset-staging.js:2
`).map((line,idx)=>`${idx===0?firstLine:padding}${line}`)};const reason=proc.signal!=null?`signal ${proc.signal}`:`status ${proc.status}`,command=[prog,...args.map(arg=>/[^a-z0-9_-]/i.test(arg)?JSON.stringify(arg):arg)].join(" ");throw new Error([`${prog} exited with ${reason}`,...prependLines("--> STDOUT: ",proc.stdout)??[],...prependLines("--> STDERR: ",proc.stderr)??[],`--> Command: ${command}`].join(`
^
Error: docker exited with status 1
--> Command: docker build -t cdk-56873b35ad5918ca4be680982cd873a337a6409face1e9609c1043d328c6fd66 --platform "linux/amd64" --build-arg "IMAGE=public.ecr.aws/sam/build-nodejs12.x" --build-arg "ESBUILD_VERSION=0" "/Users/devalias/dev/REDACTED-infrastructure/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib"
at dockerExec (/Users/devalias/dev/REDACTED-infrastructure/node_modules/aws-cdk-lib/core/lib/private/asset-staging.js:2:237)
at Function.fromBuild (/Users/devalias/dev/REDACTED-infrastructure/node_modules/aws-cdk-lib/core/lib/bundling.js:1:4085)
at new Bundling (/Users/devalias/dev/REDACTED-infrastructure/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.js:1:2315)
at Function.bundle (/Users/devalias/dev/REDACTED-infrastructure/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.js:1:599)
at new NodejsFunction (/Users/devalias/dev/REDACTED-infrastructure/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.js:1:1310)
at new CustomNodeLambdaEventHandler (/Users/devalias/dev/REDACTED-infrastructure/lib/constructs/CustomNodeLambdaEventHandler.ts:44:22)
at new REDACTEDEventStack (/Users/devalias/dev/REDACTED-infrastructure/lib/REDACTED-event-stack.ts:100:43)
at Object. (/Users/devalias/dev/REDACTED-infrastructure/bin/REDACTED.ts:46:30)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module.m._compile (/Users/devalias/dev/REDACTED-infrastructure/node_modules/ts-node/src/index.ts:837:23)
error Command failed with exit code 1.
```

### Reproduction Steps

See above

### Possible Solution

Throw a more useful error/warning at the CDK framework level that describes the issue, rather than it being surfaced through arbitrary warnings within a related docker build container.

**Workaround**

Manually figure out that the error relates to using `runtime: Runtime.NODEJS_12_X`, find it in your code, and update it to something supported such as `runtime: Runtime.NODEJS_14_X`

### Additional Information/Context

N/A

### CDK CLI Version

2.80.0 (build bbdb16a)

### Framework Version

2.80.0

### Node.js Version

v16.15.1

### OS

macOS Ventura 13.3.1

### Language

Typescript

### Language Version

_No response_

### Other information

N/A

Contributor guide

Open the contributing guide

Research direction

Start with the NodejsFunction call and the aws-lambda-nodejs/lib/bundling.js path shown in the stack trace, then reproduce the Docker build using Runtime.NODEJS_12_X. Compare the bundler failure with the requested framework-level behavior. Done means the unsupported runtime or its cause is reported clearly rather than only through the Docker build output.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, docker, node.js, typescript
Domain
build-system, developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.