Reputation: 2187
I am new to AWS Lambda, I am pulling my hairs about calling S3 APIs in nodejs. I want to read the content of an S3 object, so here is my code:
exports.handler = function (event, context) {
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
s3.getObject({
Bucket: 'lambda-bucket',
Key: 'test.html'
}, function (err, data) {
console.log('this is happening!');
console.log(data);
});
context.done();
}
I followed the API doc, but I don't even see the first log in CloudWatch. I also tried putObject
, didn't seem to work as well. My runtime is nodejs4.3. Wish I could get some help here.
Thanks.
Upvotes: 2
Views: 1359
Reputation: 200998
I recommending learning how asynchronous calls work in NodeJS before attempting to tackle AWS Lambda. In your code you are calling context.done()
before your call to s3.getObject()
is complete. This is causing the function to terminate before your S3 call finishes. Moving the context.done()
call into the getObject
callback will fix this:
exports.handler = function (event, context) {
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
s3.getObject({
Bucket: 'lambda-bucket',
Key: 'test.html'
}, function (err, data) {
console.log('this is happening!');
console.log(data);
// Now that the S3 call is complete,
// we can terminate the Lambda function execution
context.done();
});
}
Upvotes: 5