¦ Atom ¦ RSS

Polyglot Twitter Bot, Part 2: Node.js + AWS Lambda

[The second in an (at least) 6-part series, all code on GitHub as always.]

  1. Node.js
  2. Node.js + AWS Lambda
  3. Python 2.7 + AWS Lambda
  4. Purescript
  5. Purescript + AWS Lambda
  6. Bonus: Purescript + Twitter Streaming

AWS has a recent-ish Lambda product, which lets you upload functions and then have them run on demand (or on a schedule), without having to actually run a server. There's a lot of interesting things you can do with this functionality, one of which is "run a Twitter bot".

These days Lambda allows Node.js, Python, or Java functions, but for now we'll modify and use the Node.js bot we built previously.

The only trick is that we need to fit it into the Lambda execution model.

Lambda expects our Node module to export a handler function with the following signature:

exports.handler = function(event, context) {
   // do something

Here event contains the parameters for the function invocation, and context contains Lambda-specific details. Our bot function won't take any parameters, since it has everything it needs (query, credentials, etc...) hardcoded in it. The context object contains succeed and fail methods that tell Lambda that the function is done running. (This is why we included them last time.)

So a handler that would work here is just

exports.handler = function(event, context) {
  searchAndTweet(context.succeed, context.fail);

Now, if your function doesn't use any fancy libraries, you can simply type the code into the AWS console. However, here we're using the twitter library, so we can't. Instead we have to create a deployment package, which is just a zip file.

$ ls
credentials.js  index.js  node_modules  package.json

In particular, we need to include the node_modules directory, which contains the Twitter library and its dependencies.

$ zip -r twitter.zip *

This gets me a ~1.3MB zip file. Now go to your AWS console and click "Create a Lambda Function".

"Skip" the "select blueprint" step, give your function a name, and upload the zip file.

The default handler name index.handler should work (index means to look in index.js, .handler means to use the function called that), and then create or choose a "basic execution" role.

The 128MB should be plenty of memory, but you might want to up the timeout to 10 seconds or so.

Finally, click "Create Function". At this point you want to "Save and Test" to make sure it works.

Finally, you probably want it to run on a schedule, so choose the "Event Sources" tab and click "Add event source". Go to "scheduled event" and enable it to run every 5 minutes (or every 15 if you want).

(I am not much of an AWS pricing guru, but my best estimate is that this Lambda function should cost you approximately $0.)

And that's it, your bot is done! Next time we'll do it in Python.

© Joel Grus. Built using Pelican. Theme based on pelican-svbhack. .