I have an AWS Lambda function deployed with an IAM role with "Full Access" to Lamba, S3, and Cloudwatch resources. It works for the most part.
I have included an npm module aws-sdk
in the function entry point index.js
. My goal is for the function to read the contents of a .json
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.
Here is the code I'm using, which is largely taken from another StackOverflow answer. 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 s3.getObject
method does not seem to fire off at all. I cannot even get an error message logged.
Here is the code I'm using:
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.handler = async (event, context, callback) => {
console.log("Incoming Event: ", 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 => ${src_bucket} -> ${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("Raw text:\n" + data.Body.toString('ascii'));
callback(null, null);
}
});
};
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 s3.getObject
method output anything? For example, the line console.log(
Read S3 JSON object);
does not show up in the Cloudwatch log, though the console.log
message outside of that method work just fine. I have ensured that node_modules
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.