Matt
Matt

Reputation: 6320

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

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.

Upvotes: 2

Views: 4709

Answers (2)

Ashish Modi
Ashish Modi

Reputation: 7770

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.

Upvotes: 1

Adiii
Adiii

Reputation: 59926

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

enter image description here

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

Upvotes: 3

Related Questions