问题
I am using AWS Lambda to resize my image in s3 bucket into different size variants using node js when an image is put into the s3 bucket.
It was working till yesterday. Today when I use the same lambda function I get the following error:
{
"errorMessage": "Command failed: identify: not authorized `//bucketname.s3.amazonaws.com/imagename.jpg' @ error/constitute.c/ReadImage/454.\n",
"errorType": "Error",
"stackTrace": [
"",
"ChildProcess.proc.on.onExit (/var/task/node_modules/gm/lib/command.js:297:17)",
"emitTwo (events.js:87:13)",
"ChildProcess.emit (events.js:172:7)",
"maybeClose (internal/child_process.js:821:16)",
"Socket.<anonymous> (internal/child_process.js:319:11)",
"emitOne (events.js:77:13)",
"Socket.emit (events.js:169:7)",
"Pipe._onclose (net.js:469:12)"
]
}
I am unable to understand why this phenomenon occurred. All the given functions of my lambda function below are in async waterfall to first compute the aspect ratio and then convert the image into different size variants.
var request=require("request");
function getTheAspectRatio(callback) {
gm(s3Url) // I am constructing the image url in the AWS Lambda Function.
.size(function(err, size) {
if (!err) {
//Calculate the Aspect ratio
} else if (err) {
//Give Back the Error
}
});
}
function getTheImageBuffer(callback) {
request(imageUrl, function(err, res, res1) {
if (err) {
callback(err);
} else {
buffer = res1;
console.log("got the BUffer");
callback(null);
}
});
}
function convertToThumbNail(callback) {
//Convert to Thumbnail Image
}
function convertToFull(callback) {
//Convert to Full Image
}
function convertToBadge(callback) {
//Convert to Badge image
}
Can somebody help in debugging the issue? I am kind of stuck on this for the past 3 hours.My AWS Lambda is in Tokyo Region.
回答1:
I had the exact same error message occur on a process that had been running flawlessly for the last 5 weeks. After speaking with AWS support today, I was informed that native library support for Imagemagick was removed from AWS Lambda due to the vulnerability that was found recently documented here https://imagetragick.com/.
I was told I would have to rebuild my Lambda function and bundle in my own version of the native library - https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/
The support representative confirmed that there had not been a public announcement of this change.
TLDR: If you had been using an AWS Lambda function that was dependent on the bundled in version of Imagemagick, as of 05/04/2016, it is now broken and probably will not work until you redeploy with your own built and maintained version of the library. May the fourth be with you...
回答2:
Mitch Shields is correct, you now have to build / deploy it yourself onto AWS Lambda.
I have built a version that works for my project, and a NodeJS tool that will download it onto the lambda instance. The built tarbal is ~85mb, which is too large to package with your code, so you have to downloaded it onto lambda before running. It is stored in /tmp/imagemagick
, lambda attempts to cache the /tmp/
folder, so you shouldn't need to download it on every run.
GitHub Page: https://github.com/DoubleDor/imagemagick-prebuilt
Check the releases for the tarbal of the build ImageMagick https://github.com/DoubleDor/imagemagick-prebuilt/releases
回答3:
According to the AWS documentation: http://docs.aws.amazon.com/pt_br/lambda/latest/dg/current-supported-versions.html AWS Lambda still supporting imagemagick.
However, I had the same problem a few days ago, with a project that was working flawless. As the error message issues it seems to be a permission conflict while trying to read the S3 bucket rather than an imagemagick problem.
You can try changing the Bucket permissions Make a bucket public in Amazon S3.
Alternatively as a workaround you can always zip the image files with your lambda file and avoid such permissions conflics.
回答4:
https://alas.aws.amazon.com/ALAS-2016-699.html
"Note: This update contains an updated /etc/ImageMagick/policy.xml file that disables the EPHEMERAL, HTTPS, HTTP, URL, FTP, MVG, MSL, TEXT, and LABEL coders"
I changed my call from gm(my_url)
to gm(request(my_url))
and things seem to be working again. I.e I send a stream from a request() call to ImageMagick instead of letting ImageMagick try to download the image (which is disabled in ImageMagick's policy.xml, a file I can't modify).
回答5:
It seems like your Lambda function has no access to your S3 bucket. Make sure that your function has an according IAM policy applied, e.g.:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::*"
]
}]
}
Although your problem has nothing to do with graphicsmagick, please note that AWS Lambda only comes with imagemagick installed. So unless you provide the graphicsmagick executables yourself make sure to use the imagemagic sub class:
var gm = require("gm").subClass({ imageMagick: true });
来源:https://stackoverflow.com/questions/37046586/aws-lambda-not-working-along-with-the-gm-module