cURL on AWS Lambda gives command not found error

不问归期 提交于 2020-01-24 00:54:07

问题


Starting a few hours today, a simple curl command on Lambda is failing.
Lambda environment is NodeJs 10.x (have also tried in 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;
};

I get a /bin/sh curl: command not found error Any idea what the issue is?

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)"
  ]
}

回答1:


Finally i have a confirmation from Amazon support (and their internal tech team) that CURL binary is no longer included as part of the AWS Lambda environment based on Amazon Linux 2. Which is why I am not able to perform curl using either execSync or spawnSync in Node 10 and Node 12.

The alternative as per them is to use the "requests" library https://github.com/request/request/blob/master/README.md#streaming




回答2:


I tried using spawnSync instead of execSync and it's working.

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

The spawnSync uses a process environment to run your command, while an execSync uses a shell environment.The curl path is apparently not configured in the shell environment.



来源:https://stackoverflow.com/questions/58956951/curl-on-aws-lambda-gives-command-not-found-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!