title: Cannot get Lambda function to read S3 object contents
body: <p>I have an AWS Lambda function deployed with an IAM role with &quot;Full Access&quot; to Lamba, S3, and Cloudwatch resources.  It works for the most part.</p>
  <p>I have included an npm module <code>aws-sdk</code> in the function entry point <code>index.js</code>.  My goal is for the function to read the contents of a <code>.json</code> file once it is uploaded to the bucket.  I've created a trigger for the Lambda function to do this on an S3 PUT operation for the bucket.</p>
  <p>Here is the code I'm using, which is largely taken from <a href="https://stackoverflow.com/questions/30651502/how-to-get-contents-of-a-text-file-from-aws-s3-using-a-lambda-function">another StackOverflow answer</a>.  When I deploy, everything seems to work — I can get the name of the json file that is uploaded to the bucket, and print that to a log on Cloudwatch without issue.  The problem is that the <code>s3.getObject</code> method does not seem to fire off at all.  I cannot even get an error message logged.</p>
  <p>Here is the code I'm using:</p>
  <pre><code>var AWS = require('aws-sdk');
  var s3 = new AWS.S3();
  
  exports.handler = async (event, context, callback) =&gt; {
      
      console.log(&quot;Incoming Event: &quot;, event);
      
      var src_bucket = event.Records[0].s3.bucket.name;
      var src_key    = event.Records[0].s3.object.key;
  
      const filename = decodeURIComponent(src_key.replace(/\+/g, ' '));
      const message = `File is uploaded =&gt; ${src_bucket} -&gt; ${filename}`;
  
      console.log(message);
  
      s3.getObject({
          Bucket: src_bucket,
          Key: src_key
      }, function(err, data) {
  
          console.log(`Read S3 JSON object`);
  
          if (err) {
  
              console.log(err, err.stack);
              callback(err);
  
          } else {
  
              console.log(&quot;Raw text:\n&quot; + data.Body.toString('ascii'));
              callback(null, null);
  
          }
  
      });
  
  };
  </code></pre>
  <p>I'm not sure how to troubleshoot any further.  I did setup the S3 bucket permissions to explicitly let the IAM role read and write to it. But that did not seem to fix anything.  Why doesn't the function in the <code>s3.getObject</code> method output anything?  For example, the line <code>console.log(</code>Read S3 JSON object<code>);</code> does not show up in the Cloudwatch log, though the <code>console.log</code> message outside of that method work just fine.  I have ensured that <code>node_modules</code> folder is uploaded as well.  If the AWS SDK module was not uploaded to Lambda wouldn't the log report an error?  Any advice on troubleshooting so that I can understand why I cannot read a bucket object is appreciated.</p>