Building a Twitter Bot with AWS Serverless
Tags: twitter, python, aws
Published: 2021-08-03 05:20:01

Sharing my experience of how I am running the Twitter Bot on AWS for almost free using AWS serverless technologies. 

Social Bot is nothing but a program that mimics the behaviours of a user based on the rules you set. Twitter is one of the BOT friendly platforms where you can automate a lot of things from liking, re-posting, follow, unfollow and messaging using their API. 
 
Before we get down to the nitty-gritty of how it works, there is an [official guidelines](https://help.twitter.com/en/rules-and-policies/twitter-automation) from Twitter to follow whenever we create a bot.  

## My Bot account

{% twitter 1409561715813982212 %}

I've created this account last year June 2020, but it outperformed my other account which I have been using for 10+ years with a lot more followers, tweets and engagements so the same content I post in this account reaches much more audience than the actual one. 

To create your bot all we need is a Twitter account and an AWS account to host your application.

## Solution Overview

![TwitterBOT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cj6j0tegx9uso6sr06xf.jpg)
 
 
### AWS Lambda
 
Lambda is a serverless compute service from AWS that let you write and run your own code without managing any servers. Lambda comes with 1 million execution and 400,000 GB-seconds of compute time for free. It also supports multiple languages such as Node.js, Python, Java and many more. I've used Python to develop this bot which will run multiple times a day to source the best content based on the keywords and hashtags. 

### AWS Secrets Manager 

Secrets Manager helps to keep our secret keys secure within AWS. The cost is based on the number of keys so it may cost you ~$1 to keep our Twitter access keys securely.
 
Alternatively, you can also keep the keys in your Lambda environment variable which won't cost you anything, but never store your keys straight in the code as a best practice.    

### AWS DynamoDB 

DynamoDB is a NoSQL database from AWS which allows you to store the data in key-value pairs. I am using this to store the metadata of the content I shared on my Twitter to avoid repetitive content in my feed. Since am using on-demand, the cost is based on the read and writes so in my case it's costing me less than $1 per month. 

### AWS CloudWatch
 
CloudWatch is a monitoring and observability tool which provides data to make actionable insights to monitor our application. I am writing all the Lambda logs back to CloudWatch to keep track of how it is progressing. We can run this within the free tier so there is no additional cost to keep these logs. 

### Slack

Lastly, I am using Slack to monitor the content it shares. Now you may have a question why can't we directly use the Twitter app to check now and then, having that comes as a message in Slack is so much more comfortable than checking my Twitter feed. 

## Final words

Serverless architectures provide great benefits, but mainly the scalability and the cost. There are paid services to manage your Twitter content, but to run our own is nearly free using a platform like AWS. Similarly, there are several applications and use-cases you can develop using serverless technologies.