今天开始几个小时,在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:找不到命令错误知道是什么问题吗?
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)"
]
}
我尝试使用spawnSync而不是execSync,它可以工作。
const {spawnSync} = require('child_process');
spawnSync使用进程环境来运行命令,而execSync使用shell环境。在外壳环境中显然没有配置卷曲路径。