Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphicsmagick not working in Elastic Beanstalk with nodejs and S3

I'm using nodejs and graphicsmagick to process images with text, then streaming the final jpg to S3.

Using postman, I was able to test this flow on my localhost and everything works fine. However, I'm having issues now that I moved it to Elastic Beanstalk. When I post to the endpoint, it uploads a blank file to S3 and there are no errors logged in EB. I think it has something to do with the software but am a bit stuck. Any advice appreciated! Thanks!

Top file is from localhost, bottom file is from Elastic Beanstalk: http://cl.ly/image/0O231k171N0W

var gm              = require('gm');
var appRoot         = require('app-root-path').path;

function createImage(caption, res) {
    var originalImage   = '/images/2015-02-24.jpg';
    var textColor       = 'white';

    gm(appRoot + originalImage)
        .fill(textColor)
        .font( appRoot + '/fonts/BentonSans-Book.otf')
        .drawText(0, 0, caption, 'Center')
        .stream(function(err, stdout, stderr) {
            sendToS3(err, stdout, stderr, originalImage, res);
        });
}

function sendToS3(err, stdout, stderr, originalImage, client_response) {
    var imageName       = shortId.generate();
    var buff            = new Buffer('');

    stdout.on('data', function(data) {
        buff = Buffer.concat([buff, data]);
    });

    stdout.on('end', function(data) {
        var data = {
            Bucket: S3_bucket,
            Key: imageName + '.jpg',
            Body: buff,
            ContentType: mime.lookup(originalImage)
        };

        s3.putObject(data, function(err, res) {
            client_response.send('done');
        });
    });
}

===============================================================

EDIT: Instead of streaming to S3, I changed it to write directly to the filesystem. The error being thrown in AWS EB logs is:

err { [Error: Command failed: gm convert: Request did not return an 
image.] code: 1, signal: null }

I believe I'm missing some dependencies for ImageMagick. Any thoughts?

This is from running convert --version in my local terminal:

Version: ImageMagick 6.8.9-7 Q16 x86_64 2014-08-31
http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib freetype jng jpeg ltdl lzma png xml zlib

This is from running convert --version in my EC2 instance (The Delegates section is empty):

Version: ImageMagick 6.9.1-1 Q16 x86_64 2015-04-10
http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: DPC OpenMP
Delegates (built-in): 
like image 614
Rob Avatar asked Feb 10 '23 11:02

Rob


2 Answers

How are you installing GraphicsMagick on your EC2 instance in ElasticBeanstalk? Are you using a custom AMI? The default AMI (at least the ones I've used) didn't have GraphicsMagick, I don't know about ImageMagick though.

You can use container commands to install packages with yum. I used the one below on a project where I needed GraphicsMagick.

Create a folder at the root of your project with the name ".ebextensions." Inside of that folder, create a file called "package.config" with the following contents:

commands:
  01-command:
    command: yum install -y --enablerepo=epel GraphicsMagick

This will install it when the instance is created. I have a feeling this should resolve your issue, if not you may want to use command line options for yum to use the same version or install the delegates as well:

commands:
  01-command:
    command: yum install -y --enablerepo=epel GraphicsMagick
  02-command:
    command: yum install -y --enablerepo=epel GraphicsMagick-devel
like image 65
DarylChymko Avatar answered Apr 06 '23 07:04

DarylChymko


I lowered my elasticbeanstalks' nodejs version from node 12 to node 8.15.0, and yum CAN find Graphicsmagick and installs it successfully. (I listed Graphicsmagick in .ebextensions/packages.config) Hope this will help someone!

like image 20
jenlai1345 Avatar answered Apr 06 '23 09:04

jenlai1345