I\'ve had a AWS Lambda function running on S3 objects for the last 18 months and it died around a month ago after a minor update. I\'ve reverted it but it\'s still broken. I\'ve
While the other answers helped there was still a lot of work to get to a workable solution so below is how I managed to fix this, specifically for NodeJS.
Download: https://github.com/sina-masnadi/lambda-ghostscript
zip up the bin directory and upload it as a layer into Lambda.
Add https://github.com/sina-masnadi/node-gs to your NodeJS modules. You can either upload them as part of your project or the way I did it as a layer (along with all your other required ones).
Add https://github.com/serverlesspub/imagemagick-aws-lambda-2 as a layer. Best way to do this is to create a new function in Lambda, Select Browse serverless app repository, search for "ImageMagick" and select "image-magick-lambda-layer" (You can also build it and upload it as a layer too).
Add the three layers to your function, I've done it in this order
Add the appPath to the require statement for ImageMagick and GhostScript:
var gm = require("gm").subClass({imageMagick: true, appPath: '/opt/bin/'});
var gs = require('gs');
Mine was in an async waterfall so before my previous processing function I added this function to convert to a png if wasn't an image already:
function convertIfPdf(response, next) {
if (fileType == "pdf") {
fs.writeFile("/tmp/temp.pdf", response.Body, function(err) {
if (!err) {
gs().batch().nopause().executablePath('/opt/bin/./gs').device('png16m').input("/tmp/temp.pdf").output('/tmp/temp.png').exec(function (err, stdout, stderr){
if (!err && !stderr) {
var data = fs.readFileSync('/tmp/temp.png');
next(null, data);
} else {
console.log(err);
console.log(stderr);
}
});
}
});
} else {
next(null, response.Body);
}
}
From then on you can do what you were previously doing in ImageMagick as it's in the same format. There may be better ways to do the pdf conversion but I was having issues with the GS library unless working with files. If there are better ways let me know.
If you are having issues loading the libraries make sure the path is correct, it is dependent on how you zipped it up.