question

Geoff avatar image
Geoff asked

How do you write to s3 from Lambda within Alexa handler

I've got an Alexa skill named Dominion Helper and I'd like to add the ability to set some user preferences and have them save in the background behind sessions. I'm working in Node.JS My plan has been to put these into JSON and then stringify it into an S3 object. I was under the impression that I was having a permissions error, but I finally had a test object save, I think through brute force and dumb luck. My current thinking is that the Lambda function hits the emit and exits prior to the callback/completion of the s3.putObject attempt.

How can I ensure that execution waits to save the file?

var s3 = new AWS.S3();
        var params = {
            Bucket : bucket,
            Key : key,
            Body : JSON.stringify(body),
            ContentType: "text/plain"
        };
        s3.putObject(params, function(err, data){
            var foo = "bar";
            if (err) 
                {console.log(err, err.stack); // an error occurred
                return err;
                }
            else
                {console.log(data);           // successful response
                return data;
                }
        }); 

Thanks in advance.

alexa skills kitlambdanode_js
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

KAIYIN@AMAZON.COM avatar image
KAIYIN@AMAZON.COM answered

You can try to use session attributes to store the status of the "shouldEndSession". For example, you can set session attribute ends only when the put oject attempt successful: this.attributes['endedSessionCount'] += 1;

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.