In this guide, I’ll show you how to create a simple serverless Reddit bot using Node.js and deploy it using AWS Lambda. Assuming you already have a Reddit account you want to use (either a personal one or one created just for the bot), as well as an AWS account, you should be up and running in about 5 minutes.
Register an app on Reddit
The first thing you will need to do is register a Reddit app by going to https://www.reddit.com/prefs/apps
Create an app button, and you will be presented with a form:
For this exercise, we’ll be creating a simple bot that looks for keywords or phrases in link titles, and saves the link if found. Of course, you’re free to choose your own use cases and implementations, so you can fill anything you like into the text fields, but make sure to select the
script radio button. Once you’re done with that, follow the link to register to use the API. Also, take a note of these two fields:
Create and configure AWS Lambda
In this section, we’ll create a Lambda function on AWS, deploy our code to it, add configuration, and then execute a test against it.
To get started, go to the AWS Lambda console and Create a function.
Let’s author it from scratch. You can call it anything you like. We’ll use the Node.js 10.x runtime for this bot.
Upon creating the function, you’ll be taken to the Configuration page where you’ll be able to configure various aspects of your function. We will only be concerned with the first 3 — the Designer, Function Code, and Environment variables configuration.
Let’s say you want this bot to run its process every 3 hours. To accomplish this, we can go to the designer section and add a trigger. In the trigger configuration, select CloudWatch Events, and create a new rule.
Next, we need to upload the code. I’ve created a scaffolding for an app and placed it in an S3 bucket that you can import from. Under
code entry type, select
upload a .zip file from Amazon S3, and enter the following link https://foobarraz.s3.amazonaws.com/reddit-bot-example.zip
Save. That should bring the code up in the editor. We’ll poke through the code in the next section below, but first, let’s take this bad boy for a test spin.
Before you can run it, you’ll need to set up the Environment Variables that the bot needs.
client_id — the client ID of the Reddit app
client_secret— the client secret of the Reddit app
username — the Reddit user you want to run the bot as
password— the password for that user
subreddits — the subreddits you want to scan, separated by commas
keywords — the keywords or phrases you want to scan for, separated by commas
Once you’ve entered the values, hit
Save. At this point, we’re up and running!
Let’s create a test event to verify that.
When you fire the test event, assuming any matches for your keyword came up, you will see the titles of the matching post in the log.
At this point, you should be able to see them under your Saved Posts in Reddit.
In this section, I’ll explain how to download the code, walk through some of the key areas of functionality, and show you how to run it locally.
You can freely view/edit the code in the embedded Lambda editor, but if you want to download it, you can do so from GitHub, or from the AWS Lambda Console by going to
Actions>Export function>Download deployment package
You’ll see a few files here…
├── bot.js // this is where all the logic for the bot resides
├── index.js // the entry-point for the AWS lambda
├── reddit.js // a small client for the Reddit API
└── webutil.js // a simple module that wraps https requests in a promise
webutil.js is simply a utility module that wraps requests in a Promise. The Promise API helps clean up some of the asynchronous code, and helps us ensure we can print our results to the console at the end of the execution.
reddit.js file is a small client for the Reddit API, which at this point, only contains methods used in the implementation. The way to use it is to import and invoke the
getClientInstance function that will fetch an authentication token, create a new client with that token, and return the client. The client instance will use the token for subsequent requests. It contains instance-scoped methods like
savePost. It also has general-purpose methods for getting and posting to the API, but I’d recommend creating methods for specific actions, as it allows consumers of the class not to know the implementation details of the Reddit API.
bot.js module contains all of the business logic for the bot wrapped inside an
invoke function. The
return statement on the function succinctly summarizes the operations performed:
index.js file is the entry-point for the app, and has a
handler function that is invoked from the lambda. In order to run the bot locally, you will need to create another entry-point file, or replace the contents of this one with something like this:
Then, you’ll need to set the same environment variables listed above in your shell, or in your IDE’s run configuration with the same values.
Once that’s done, you can run the
index.js file either through your IDE, or by executing
node index.js in your shell.
You should see the same results in the output as you did in the AWS Lambda console.
Where to go from here?
The bot in this example, while potentially useful, is quite rudimentary. Once you’ve got a handle on the basic elements involved, you’ll probably want to explore ways in which it can be extended or enhanced. Here are some thoughts on what you might be able to do next.
- Change the triggers so the function is invoked by actions or events other than cron. This will require changes in the AWS Lambda designer.
- Enhance the bot with more functionality — the world is your oyster here. You can have the bot do a large variety of things using the Reddit API. This will require changes to the code in
bot.jsas well as
- Have your function kick off other processes or operations. This will require changes to the AWS Lambda designer, as well as the
Have fun, and feel free to leave any feedback or questions in the comments below!