简体   繁体   English

AWS Lambda 执行 shell npm 安装报错

[英]AWS Lambda execute shell npm install error

I'm trying to build a web app in AWS lambda and upload it to an S3 bucket.我正在尝试在 AWS lambda 中构建一个 web 应用程序并将其上传到 S3 存储桶。 For that, I utilize child process, which is working fine locally.为此,我使用了在本地运行良好的子进程。

exec('cd /tmp/ && npm ci && ng build --prod="true"', (error, stderr, stdout) => {
      if (error) {
        console.log(error);
        return reject(error);
        }
      });

I also tried using npm install instead of npm ci, in all cases it fails with the error:我还尝试使用 npm install 而不是 npm ci,在所有情况下都失败并显示错误:

"npm ERR, errno -30": "npm ERR, rofs EROFS, read-only file system, mkdir '/home/sbx_user1051'", "npm ERR, rofs Often virtualized file systems. or other file systems", "npm ERR, rofs that don't support symlinks, give this error.", "npm ERR! code EROFS", "npm ERR! syscall mkdir", "npm ERR, errno -30": "npm ERR, rofs EROFS, read-only file system, mkdir '/home/sbx_user1051'", "npm ERR, rofs Often virtualized file systems. or other file systems", "npm ERR , rofs 不支持符号链接,给出这个错误。", "npm ERR! code EROFS", "npm ERR! syscall mkdir",

Command failed: cd /tmp/ && npm ci && ng build --prod="true"\nnpm ERR: code EROFS\nnpm ERR, syscall mkdir\nnpm ERR, path /home/sbx_user1051\nnpm ERR, errno -30\nnpm ERR.命令失败:cd /tmp/ && npm ci && ng build --prod="true"\nnpm ERR:代码 EROFS\nnpm ERR,系统调用 mkdir\nnpm ERR,路径 /home/sbx_user1051\nnpm ERR,errno -30\nnpm呃。 rofs EROFS: read-only file system, mkdir '/home/sbx_user1051'\nnpm ERR! rofs EROFS:只读文件系统,mkdir '/home/sbx_user1051'\nnpm 错误! rofs Often virtualized file systems, or other file systems\nnpm ERR! rofs 通常是虚拟化文件系统,或其他文件系统\nnpm ERR! rofs that don't support symlinks, give this error.\nnpm ERR!不支持符号链接的 rofs,给出这个错误。\nnpm ERR!

I understand that only the /tmp/ directory is writable in AWS Lambda. I just don't know how to tell npm to run in /tmp.我知道在 AWS Lambda 中只有 /tmp/ 目录是可写的。我只是不知道如何告诉 npm 在 /tmp 中运行。

Any ideas?有任何想法吗?

Usually, people install everything during the build phase and then they zip it and deploy as a lambda function. AWS provides some example projects for doing exactly thathttps://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html通常,人们在构建阶段安装所有东西,然后他们 zip 并将其部署为 lambda function。AWS 提供了一些示例项目来执行此操作 https://docs.aws.amazon.com/lambda/latest/dg/lambda- nodejs.html

Or with amplify https://aws.amazon.com/amplify/或者使用放大https://aws.amazon.com/amplify/

With node there is also an option to use serverless https://stackify.com/aws-lambda-with-node.js-a-complete-getting-started-guide/对于节点,还可以选择使用无服务器https://stackify.com/aws-lambda-with-node.js-a-complete-getting-started-guide/

Or, now, you may also consider using docker https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/或者,现在,您也可以考虑使用 docker https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/

Any of this can be prepared in a CI/CD pipeline ( https://www.weave.works/blog/what-cicd-tool-should-i-use ).任何这些都可以在 CI/CD 管道中准备 ( https://www.weave.works/blog/what-cicd-tool-should-i-use )。

Generally, I would avoid self-modifying lambdas.通常,我会避免自修改 lambda。 That's hardly repeatable.这很难重复。 One day one npm package will not be available for a minute, AWS will scale out your lambda and it will start failing and your whole application will crumble.有一天 npm package 将有一分钟不可用,AWS 将扩展您的 lambda 并且它将开始失败并且您的整个应用程序将崩溃。

If your goal is to repeatedly build node apps in a lambda go the docker path, that has the most flexibility.如果您的目标是在 lambda go docker 路径中重复构建节点应用程序,那将具有最大的灵活性。

Hi I've successfully got npm working by setting NPM_CONFIG_USERCONFIG environment variable to /tmp/.npmrc and set npm cache directory to /tmp/.npm .您好,我已经通过将NPM_CONFIG_USERCONFIG环境变量设置为/tmp/.npmrc并将 npm 缓存目录设置为/tmp/.npm成功地npm工作。

Like this:像这样:

  const dir = '/tmp/your_project_dir';
  process.env.NPM_CONFIG_USERCONFIG='/tmp/.npmrc';
  
  execSync(`npm config set cache /tmp/.npm`);
  execSync(`npm ci && npm run build`, {cwd: dir});

Also I found someone created a Lambda layer containing npm cli which can be used in non-nodejs Lambda environment.我还发现有人创建了一个包含npm cli 的 Lambda 层,可以在非 nodejs Lambda 环境中使用。 https://github.com/sambaiz/npm-lambda-layer https://github.com/sambaiz/npm-lambda-layer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM