UPDATE: Thank you all! I have solved this by creating a custom runtime for my PHP Lambda.
I am currently using Node.js 8.10 Runtime with a php.handler and my Lambda function works fine, but when I change the Runtime to 12.x, I get the following error:
“php-7-bin/bin/php: error while loading shared libraries: libcrypt.so.1: cannot open shared object file: No such file or directory”
exports.handler = function(event, context, callback) {
var php = spawn('php-7-bin/bin/php',['--php-ini', 'user.ini', process.env['PHPFILE']], {maxBuffer: 200 * 1024 * 200});
var output = "";
var statusCode = 0;
php.stdin.write(JSON.stringify(event));
php.stdin.end();
php.stdout.on('data', function(data) {
console.log("CHUNK: " + data);
output+=data;
});
php.stderr.on('data', function(data) {
console.log(data);
});
php.on('close', function(code) {
var obj = JSON.parse(output);
statusCode = obj.status.statusCode;
if(statusCode !== 0){
callback(output);
}else{
context.succeed(obj);
}
});
}
I need to update my Lambda to the latest node.js version, but I have no idea how to overcome this error, so any help would be greatly appreciated!
3
Answers
First, why on earth are you using node to load php?
But if you had this working before, why do you need to update to node 12?
If you are upgrading from Node 8, the runtime is different:
https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
So then take a look here:
https://aws.amazon.com/blogs/apn/aws-lambda-custom-runtime-for-php-a-practical-example/
You may need to create a new custom runtime based off the node12 built-in runtime for AWS.
Easy fix is to add on top of your PHP code:
If that won’t work you need to compile/build/install the missing modules/libraries by yourself:
Make sure the Lambda code have proper PATH defined to use Layer folder.
(Keep in mind that the Layer folder is mounted with read/write permissions).
I Fix this problem by adding extra library folder in my function’s zip.
Make a directory name
extra-libs
Copy all required libraries from
Amazon Linux 2
toExtra-libs
by using following steps :Run amazon Linux 2’s docker instance by following command
docker run --rm -it -v :/opt:rw,delegated amazonlinux:latest
Then in docker instance make directory using
mkdir deps
Copy all required libraries from lib64 to deps directory using
cp -f lib64/libcrypt.so.1 deps
(Taken libcrypt.so.1 for example purpose)Then open another terminal window and move all library files to local extra-libs
docker cp <DOCKER_CONTAINER_ID>:/deps/ . && mv deps/* ./extra-libs
Get container id by using
docker ps
Then in index.js file , add following line to php’s env setting.
Zip extra-libs folder with your lambda function and upload it.
Hope this helps.