question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code.fromAsset Bundling for NodeJS uses Wrong User

See original GitHub issue

When using the bundling option to provide Lambda code, the default behaviour for NODEJS based runtimes is broken, since it’s using a non-root user within the Docker container. It can be manually fixed by providing root as user as part of the bundling options.

Reproduction Steps

    new lambda.Function(this, 'Handler', {
      code: lambda.Code.fromAsset('/path/to/lambda/folder', {
        bundling: {
          image: lambda.Runtime.NODEJS_12_X.bundlingDockerImage,
          command: [
            'bash', '-c', [
              `cp -R /asset-input/* /asset-output/`,
              `cd /asset-output`,
              `npm install`
            ].join(' && ')
          ],
        },
      }),
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: "index.handler",
    });

Error Log

Failed to run bundling Docker image for asset Foo: Error: [Status 243] stdout:


stderr: npm ERR! correctMkdir failed to make directory /.npm/_locks
npm WARN origin-response@1.0.0 No description
npm WARN origin-response@1.0.0 No repository field.

npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /.npm
npm ERR! errno -13
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 501:20 "/.npm"
Subprocess exited with error 1

Environment

  • CLI Version : 1.46.0 (build 63860b2)
  • **Framework Version:**1.46.0
  • Node.js Version: v13.8.0
  • OS : macOS Catalina
  • Language (Version): all

Other

Since the fix is passing user: 'root' as argument, I think this change is causing the behaviour https://github.com/aws/aws-cdk/pull/8492


This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
ryan-marscommented, Oct 19, 2020

The missing thing for me was it needs to be run by root.

const myLayer = new LayerVersion(this, 'my-layer', {
  code: Code.fromAsset(assetPath, {
    assetHashType: cdk.AssetHashType.BUNDLE,
    bundling: {
      user: 'root',
...
4reactions
wchawscommented, Jan 11, 2021

@skorfmann This is because npm wants to write cache in root folder, to fix this:

 const layer = new lambda.LayerVersion(this, 'MyLayer', {
      code: lambda.Code.fromAsset(path.join(__dirname, '../lambda/'), {
        bundling: {
          image: lambda.Runtime.NODEJS_12_X.bundlingDockerImage,
          command: [
            'bash', '-xc', [
              'export npm_config_update_notifier=false',  // Disable npm upgrade check 
              'export npm_config_cache=$(mktemp -d)',  // Change npm default cache folder
              'cd $(mktemp -d)',
              'cp -v /asset-input/package*.json .',
              'npm i --only=prod',
              'mkdir -p /asset-output/nodejs/',
              'cp -au node_modules /asset-output/nodejs/',
            ].join('&&'),
          ],
        },
      }),
      compatibleRuntimes: [lambda.Runtime.NODEJS_12_X],
      description: 'A layer to test the L2 construct',
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Building, bundling, and deploying applications with the AWS ...
For this use case, our application consists of front-end and ... One method is CDK bundling. The lambda.Code.fromAsset() method takes a ...
Read more >
Problem using Lambda Layer Code in cdk Lambda nodejs
I have created a Lambda Layer in cdk in typescript. But I am unable to access the library inside my lambda code.
Read more >
AWS Lambda with Deno by AWS CDK - miyauci.me
This tutorial shows how to run AWS Lambda with Deno runtime. It uses AWS CDK for deployment and a multi-runtime project structure. It...
Read more >
Building Lambda functions inside Docker containers with CDK
NodeJS Lambda functions where you have to use tools like Webpack or ... for bundling NodeJS & Python Lambda function code via experimental ......
Read more >
Building customizable Serverless API with CDK. (3) Lambdas
Typescript and Lambdas · Use Typescript compiler tsc directly. · Use webpack bundling · Use experimental aws-lambda-nodejs module · Shared code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found