在AWS Lambda上使用cURL时出现“command not found”错误

5

从今天开始的几个小时里,Lambda上的一个简单的curl命令失败了。
Lambda环境是NodeJs 10.x(也尝试过12.x)。

const { execSync } = require('child_process');

exports.handler = async (event) => {
   execSync('curl http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg -o /tmp/BigBuckBunny.jpg');
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

我收到了"/bin/sh curl: command not found"的错误信息,你有任何想法是什么问题吗?
Response:
{
  "errorType": "Error",
  "errorMessage": "Command failed: curl http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg -o /tmBigBuckBunny.jpg\n/bin/sh: curl: command not found\n",
  "trace": [
    "Error: Command failed: curl http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg -o /tmBigBuckBunny.jpg",
    "/bin/sh: curl: command not found",
    "",
    "    at checkExecSyncError (child_process.js:621:11)",
    "    at execSync (child_process.js:657:15)",
    "    at Runtime.exports.handler (/var/task/index.js:11:4)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

只是这个程序之前在 AWS Lambda 上是可以运行的。 - Dennis Mathew
哎呀,AWS 确实让事情变得混乱了,但这只是意味着你的 $PATH 变量没有设置正确。也许你可以将 /usr/bin/curl 添加到命令中?(或者添加正确的完整路径)删除之前的评论。祝你好运。(像往常一样;-)). - shellter
Lambda中的curl是否已被删除或移动到新位置?/usr/bin中没有curl。 - Dennis Mathew
2个回答

7

最终,我从亚马逊支持(以及他们的内部技术团队)那里得到了确认,即基于Amazon Linux 2的AWS Lambda环境中不再包含CURL二进制文件。这就是为什么我无法在Node 10和Node 12中使用execSync或spawnSync执行curl的原因。

根据他们的说法,替代方法是使用“requests”库https://github.com/request/request/blob/master/README.md#streaming


2

我尝试使用 spawnSync 而不是 execSync,它可以正常工作。

const {spawnSync} = require('child_process');

spawnSync 使用进程环境来运行您的命令,而 execSync 使用 shell 环境。curl 的路径显然没有在 shell 环境中配置。


1
根据AWS支持的说法:AWS Node.js 10.x使用Amazon Linux 2,而Node.js使用Amazon Linux,因此共享库存在差异,这就解释了错误,因为某些库在新运行时环境的不同路径目录中安装。根据他们的说法,spawnSync是正确的方法,因此我将其标记为其他用户可以看到的正确方法,但是spawnSync并没有解决我的下载问题。 - Dennis Mathew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接