People have a love-hate relationship with Twitter bots. They can be useful for retweeting content that is relevant to things you are looking for, but they can also be annoying if they tweet too much or if they tweet about stuff that you don’t care about. However, you can get around this issue by creating your own bot.

Creating a Twitter bot is relatively straightforward. In true serverless style, it is possible to build your own in a cost-effective way. This article will focus on creating a Twitter bot in  Node, hosted in  AWS Lambda.

Why Serverless?

Using serverless patterns for a Twitter bot makes complete sense.

The trigger

The trigger is going to be based on a cron job. In true serverless style, I will be using Cloudwatch and utilise a cron-like expression to trigger my lambda.

Here is my event, driven through Terraform:

resource aws_cloudwatch_event_rule every_one_hour {
  name                = "every-one-hour"  
  description         = "Fires every one hour"  
  schedule_expression = "rate(1 hour)"
}

A Cloudwatch event rule will run every one hour. When this runs, I want it to call a lambda, so I also need to set up an event target.

resource aws_cloudwatch_event_target twitter_bot {  
  rule      = aws_cloudwatch_event_rule.every_one_hour.name 
  target_id = "lambda"  
  arn       = aws_lambda_function.lambda.arn
}

Not forgetting to set up to allow the function to be executed:

resource aws_lambda_permission allow_cloudwatch_to_call_check_foo {
  statement_id  = "AllowExecutionFromCloudWatch"  
  action        = "lambda:InvokeFunction"  
  function_name = aws_lambda_function.lambda.function_name
  principal     = "events.amazonaws.com"  
  source_arn    = aws_cloudwatch_event_rule.every_one_hour.arn
}

#terraform #serverless #twitter #aws #programming

Using Serverless Technologies to Create a Simple, Low-Cost Twitter Bot
13.20 GEEK