aws / aws/aws-cdk

(lamda_nodejs): Node version 16 bundling is broken

Open
#21,841 4 comments 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-lambda-nodejs bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### Describe the bug

I am trying to update a lambda function from nodejs 12 to 16. After update the bundling of the lambda function fails but still works if I revert it to v12 or v14.

### Expected Behavior

Bundling in docker image works and allows installation of node modules and additional OS dependencies

### Current Behavior

When running cdk synth, the yum install would fail without piping it out with:

```sh
You need to be root to perform this command.
```

And installation of node_modules fails:

```sh
npm WARN logfile could not be created: Error: EACCES: permission denied, open '/tmp/npm-cache/_logs/2022-08-30T13_21_35_965Z-debug-0.log'
npm ERR! code E401
npm ERR! Incorrect or missing password.
npm ERR! If you were trying to login, change your password, create an
npm ERR! authentication token or enable two-factor authentication then
npm ERR! that means you likely typed your password in incorrectly.
npm ERR! Please try again, or recover your password at:
npm ERR! https://www.npmjs.com/forgot
npm ERR!
npm ERR! If you were doing some other operation then your saved credentials are
npm ERR! probably out of date. To correct this please try logging in again with:
npm ERR! npm login
```

### Reproduction Steps

Here is CDK code for bundling:

```js
const layer = new lambda.LayerVersion(this, `test-layer`, {
code: lambda.Code.fromDockerBuild(__dirname, {}),
compatibleRuntimes: [lambda.Runtime.NODEJS_16_X],
license: 'Apache-2.0',
description: 'A test layer',
});

const sendLambda = new lambda_nodejs.NodejsFunction(this, 'test-lambda', {
entry: 'lambda/test.ts',
runtime: lambda.Runtime.NODEJS_16_X,
timeout: cdk.Duration.minutes(1),
vpc,
bundling: {
nodeModules: ['got'],
environment: {
NODE_ENV: 'production',
},
commandHooks: {
afterBundling (inputDir: string, outputDir: string): string[] {
return [`cp -fr ${inputDir}/config ${outputDir}`];
},
beforeBundling (_inputDir: string, _outputDir: string): string[] {
return [];
},
beforeInstall (_inputDir: string, _outputDir: string): string[] {
return ['yum -y install openssl11-devel || true'];
}
}
},
layers: [layer],
memorySize: 512,
environment: {
APP_ENVIRONMENT: props.envName,
},
logRetention: logs.RetentionDays.TWO_MONTHS,
});
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.38.1

### Framework Version

_No response_

### Node.js Version

16.17.0

### OS

macOS Monterey 12.5

### Language

Typescript

### Language Version

4.7.4

### Other information

https://stackoverflow.com/questions/73543248/aws-cdk-nodejs-16-lambda-bundling-not-working
Bundle image: public.ecr.aws/sam/build-nodejs16.x

Contributor guide

Open the contributing guide

Research direction

Start with the NodejsFunction bundling configuration and the public.ecr.aws/sam/build-nodejs16.x image, then reproduce the failure with cdk synth using the shown beforeInstall yum command and nodeModules setting. Trace the permission and npm authentication errors during bundling. Done means Node.js 16 bundling works in Docker, including OS dependency and node module installation.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, docker, node.js, typescript
Domain
build-system, cloud, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.