title: convert pdf pages into images using AWS s3 and Lambda
body: <blockquote>
    <p>I am using node 8.1,
    I want to convert PDF into images using S3 and lambda but the CloudWatch repeatedly giving the following error:
    "Unable to import module 'index': Error"
    The below file is named as index.js in the main project folder:</p>
  </blockquote>
  
  <pre><code>const util = require('util');
  const AWS = require('aws-sdk');
  const gm = require('gm').subClass({ imageMagick: true });
  const s3 = new AWS.S3();
  
  exports.handler = (event, context) =&gt; {
    const srcBucket = event.Records[0].s3.bucket.name;
    const srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
  
    const dstBucket = srcBucket;
    const dstKey = srcKey.replace('.pdf', '.png');
  
    s3.getObject({Bucket: srcBucket, Key: srcKey}, (err, response) =&gt; {
      if (err) {
        context.done('S3 get object error:', err);
        context.fail(err);
      }
  
      // conversion start
      gm(response.Body)
        .setFormat("png")
        .resize(200) // you can configure
        .quality(100) // you can configure
        .stream((err, stdout, stderr) =&gt; {
          if(err) {
            console.log("gm conversion process error: ");
            console.log(err,stdout,stderr);
            context.fail(err);
          }
          const chunks = [];
          stdout.on('data', (chunk) =&gt; {
            chunks.push(chunk);
          });
          stdout.on('end', () =&gt; {
            console.log('gm process finished');
            const buffer = Buffer.concat(chunks);
  
            // Upload start
            const params = {
              Bucket: dstBucket,
              Key: dstKey,
              ContentType: 'image/png',
              Body: buffer
            };
            s3.putObject(params, (err, data) =&gt; {
              if (err) {
                console.log("S3 upload error: " + err);
                context.fail(err);
              }
              console.log('S3 upload finished!');
              console.log('Bucket: ' + dstBucket);
              console.log('key: ' + dstKey);
              context.succeed({
                "error":false
              });
            });
          });
  
          stderr.on('data', (data) =&gt; {
            console.log('stderr data: ' +  data);
          });
        });
    });
  };
  </code></pre>
  
  <blockquote>
    <p>I am not much aware about the aws-lambda as well as the file conversion and this is the only area where I have got stuck and can't find any way to solve the problem.</p>
  </blockquote>
  
  <p>I think there might be some problem in the index.handler way of passing the functions but I don't know where the minor fault is situated at.</p>