Updating Lambda function to new runtime. Why is cURL no longer working in Node 10 on AWS Lambda?

冷暖自知 提交于 2020-01-16 12:00:09

问题


This is the line in my Lambda function that's raising an error:

// running in Node 8
const { execSync } = require('child_process');
execSync('curl https://github.com');

I'm trying to upgrade my Lambda function from Node 8 to Node 10 or Node 12, since Node 8 on Lambda is being deprecated at the end of December, 2019 (so I won't be able to update it). However, when I dig into my CloudWatch logs I'm seeing the following error:

bin/sh: curl: command not found

And when I update my Lambda function to just run which curl I get a similar error: bin/sh: which: command not found

Per documentation here, I know that Node 8+, Python 3.8+ and Java 11+ are using the new AWS Lambda Runtime, Amazon Linux 2.

Any help is appreciated.


回答1:


Node V10 and V12 above is based on Amazon Linux 2 and you can not run curl.

cURL on AWS Lambda gives command not found error

You have two options

  • Use native binary package in AWS Lambda
  • Use nodejs request or http built-in module

How do I use Amazon Linux AMI native binary packages in an AWS Lambda deployment package?

Short Description

A Lambda deployment package is a .zip file that contains your code and any dependencies. The Lambda execution environment is based on a specific Amazon Linux AMI and kernel version. Any native binaries that are used in a Lambda deployment package must be compiled in this environment, and only 64-bit binaries are supported.

To use the Amazon Linux AMI native binary packages, you can extract the 64-bit libraries and then include them in your Lambda deployment package. (Another option, that is not covered in this article, is to download the source code to the shared library and then recompile the package.)

lambda-runtimes

So as suggested in the comment the other way is to use the node package.

you can try for testing purpose something like

exports.handler = async (event) => {
  const https = require('https');                
  var response=https.get('https://api.github.com');
  return JSON.stringify(response.output)
}




回答2:


Instead of zipping the native binary package with lambda function, I would suggest to have a look at AWS Lambda Layers (https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html)

This way if you have multiple lambda functions, they could share these binary packages. You will have a to upload the content in a nodejs folder and zip it. In your code you could access it /opt/nodejs/ path

By the way I would prefer to use nodejs to make http calls instead of using curl from nodejs.



来源:https://stackoverflow.com/questions/59164723/updating-lambda-function-to-new-runtime-why-is-curl-no-longer-working-in-node-1

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