title: Watermarking an image on AWS Lambda with node.js and gm
body: <p>I'm trying to resize and watermark an image (downloaded from S3) in an AWS Lambda function.</p>
  
  <p>The resizing part is working well, based on the sample code from the "getting started" project of AWS Lambda. </p>
  
  <p>Now, I have a problem adding a watermark to my files. </p>
  
  <p>On my local system, I can do this:</p>
  
  <pre><code>gm('martinrose.jpg')
  .draw(['image Over 0,0 0,0 wm-bas.png'])
  .write('brol.jpg', function(e){
   console.log(e||'done'); 
  }); 
  </code></pre>
  
  <p>And it works without problem. </p>
  
  <p>In the Lambda environment, I added the wm-bas.png file to the zip file uploaded to Amazon, and it seems to be found by my js code (I tested using lstatSync), but the real watermarking does not work.</p>
  
  <p>Here is the relevant part of what I do:  </p>
  
  <pre><code>gm(response.Body).size(function(err, size) {
      var scalingFactor = Math.min(
          newSize / size.width,
          newSize / size.height
      );
      var width  = scalingFactor * size.width;
      var height = scalingFactor * size.height;
  
      var fs = require('fs');
      var stats = fs.lstatSync('wm-bas.png');
      console.log(stats); // this outputs meaningful info, so, the file exists
  
      var ctx = this.resize(width, height);//this works
  
      if (shouldWatermark)
      {   
          console.log("trying to watermark");
          ctx = ctx.draw(['image Over 0,0 0,0 wm-bas.png']) //this doesn't work, although the previous log is written
      }
  
      ctx.toBuffer(imageType, function(err, buffer) 
          {
              if (err) {
                  next(err);
              } else {
                  next(null, response.ContentType, buffer);
              }
          }
      );
  });
  </code></pre>
  
  <p>What am I missing? Why doesn't this work? Is it related to the fact that I save in a buffer and not in a file?</p>
  
  <p>I import gm with this code, BTW:</p>
  
  <pre><code> var gm = require('gm')
          .subClass({ imageMagick: true });
  </code></pre>