AlexaDev Tuesday: Using Javascript To Write A File To S3 On The Fly

Post Published 6/26/18 – Short and sweet today: I’ve got a tip for the Javascript skill devs out there. Did you know it’s possible to have your Lambda function write a file to S3 on the fly?

Afraid To Debug

Think It Through

Before I share the javascript snippet to use, a warning: there are security implications in letting your skill write a file to your repository in real time, because of course the skill must have the appropriate permissions to write and/or overwrite files.

Managing permissions in AWS is a large and complex topic that’s outside the scope of this post. See Amazon’s AWS Identity and Access Management Documentation to get a better understanding of how to manage AWS resource access and security.

The Code: Faking An RSS Feed In A Flash Briefing Skill

Imagine a Flash Briefing skill whose content comes from a hard-coded array rather than an RSS feed. For a ‘compliment of the day’ or ‘this day in history’ type of skill, a hard-coded array could actually provide a more reliable and stable solution than an RSS feed. However, Flash Briefing skills expect to access their data in the form of an RSS feed so even when your data is hard-coded, you’ve got to trick Alexa into thinking it’s an RSS feed.

My use case here is pretty specific, but I figured it might be a tip others can use too. You can see where I pull an entry from my array, provide the rest of the required fields for the RSS feed .json file, then write that file to my S3 bucket. Every time the skill runs, it overwrites the existing S3 file with new content that’s pulled from a hard-coded array in the script.

Write To S3

This script mimics the output of an RSS feed, but the same code can be adapted to write any type of file to S3. The putObjectToS3 function is the one to pay attention to here. All it requires is a bucket name, a key (aka file name), and a data package to write to that file. You could use this same code to write audio, image, text and other types of files to S3 on the fly.

Click here to view a .txt version of the code snippet above, which you can copy and paste.