Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload an object into S3 in Lambda?

Can't seem to upload an object into S3 in Lambda. Everything works fine locally. No errors in logs that would show what's going wrong...

Code below:

console.log('Loading function');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();

exports.handler = function(event, context) {
    //console.log(JSON.stringify(event, null, 2));
    var s3 = new AWS.S3();
    var param = {Bucket: 'flow-logs', Key: 'test-lambda-x', Body: 'me me me'};
    console.log("s3");
    s3.upload(param, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);           // successful response
    });
    console.log('done');
    context.done();
};

Runs successfully w/o error, but the callback in s3.upload doesn't seem to be called. No object in bucket is created.

Verified IAM role permissions weren't a problem by granting full access, as well as testing out locally.

Output

START RequestId: d4847fdb-160c-11e5-8a8c-b555b123e14d
2015-06-18T22:53:29.750Z    d4847fdb-160c-11e5-8a8c-b555b123e14d    s3
2015-06-18T22:53:30.271Z    d4847fdb-160c-11e5-8a8c-b555b123e14d    done
END RequestId: d4847fdb-160c-11e5-8a8c-b555b123e14d
like image 338
Edwin Avatar asked Jun 18 '15 22:06

Edwin


People also ask

How do I upload S3 files to a lambda function?

Next, navigate to the Configuration tab of your lambda function and choose Environment variables to edit the variables. Add the BUCKET_NAME environment variable by setting the value to an existing S3 bucket. Our function will upload the S3 files to this bucket.

How do I test a lambda function in Amazon S3?

Create an Amazon S3 bucket and upload a test file to your new bucket. Your Lambda function retrieves information about this file when you test the function from the console. Open the Amazon S3 console .

How to read files from S3 bucket using Python Lambda?

We will use boto3 apis to read files from S3 bucket. Read a file from S3 using Python Lambda Function. List and read all files from a specific S3 prefix using Python Lambda Function. Login to AWS account and Navigate to AWS Lambda Service.

How do I configure an S3 trigger in AWS Lambda?

For Execution role, choose Create a new role from AWS policy templates . For Role name, enter my-s3-function-role . Under S3 trigger, choose the S3 bucket that you created previously. When you configure an S3 trigger using the Lambda console, the console modifies your function's resource-based policy to allow Amazon S3 to invoke the function.


1 Answers

I suspect you are calling the context.done() function before s3.upload() has a chance to return. If you move context.done() into the upload response code block, it should work.

var AWS = require('aws-sdk');
var s3 = new AWS.S3();

exports.handler = function(event, context) {
    //console.log(JSON.stringify(event, null, 2));
    var s3 = new AWS.S3();
    var param = {Bucket: 'flow-logs', Key: 'test-lambda-x', Body: 'me me me'};
    console.log("s3");
    s3.upload(param, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);           // successful response

        console.log('actually done!');
        context.done();
    });

    console.log('done?');
    //context.done();
};
like image 105
James Avatar answered Oct 18 '22 23:10

James