Automatic Twitter Updates

Use the serverless magic of AWS Lambda to auto-tweet my posts

Mark Ewing

4 minute read

Automatic Twitter Updates

One of my goals with this site is to have as much as possible to automatic. Because it’s a static site, each time I write a post I have to copy all the files (new and updated) to the hosting location so you, the illustrious reader, can enjoy the content I create. Then, I need to promote it. This involves the laborious process of switching to my pinned Chrome tab with Twitter open and writing a tweet remember to paste in a link. I’d like to make it easier for me, so I turned to AWS Lambda.

AWS Lambda

Maybe you’re not familiar with the serverless magic that is AWS Lambda - if so, I’m sorry. It will change your life. Unless you only know R, then you’ll mostly just be frustrated.

One of the big points of the ‘cloud’ or IASS (Infrastructure as a Service) is the ability to scale (up or down) on demand - if you just partition an EC2 server on AWS and never spin up/down, you’re probably wasting money. And by probably I mean, almost certainly. Many Fortune 500 companies claim they’re saving money by switching to the cloud but this is often because they have big renewals coming up with their bare metal providers (maybe EMC storage contract is coming up) and so they get to trade infrequent-but-massive capital costs with frequent-but-smaller expense costs. Classic business internal shell games. If you’re a startup or a tech company that deals with highly flexible demand, IASS makes a ton of sense - did you just land a big client? Spin up more servers! Did you just lose a big client or they’ve been with you long enough you don’t care about their performance? Spin some servers down! If you’re writing a little blog, use S3 because you only pay for exactly what you’re storing/using and nothing more keeping your costs at pennies!

So how does AWS Lambda fit into all this? And what do you mean by serverless?

Serverless

Serverless is a bit of a misnomer because lambda (I’m going to stop saying AWS Lambda all the time) does run on a server. But, you don’t have to partition the server. Or configure it. Or spin it down. Lambda handles all that. It’s brilliant!

Lambda for Scale

With lambda you write a function that does a small block of work in one of the (few) supportedl languages. These are (as of now):

  • Python (2/3)
  • Node.js (4/6)
  • Java 8
  • C#

Then, you create a trigger for the lambda code to run. Examples include:

  • Call from an API Gateway
  • File being uploaded to S3
  • AWS IoT button being pushed
  • Alexa Skill

The trigger will cause AWS to spin up a container, load your function (and any additional components you included), execute your function and then spin down the container. You, as the developer, only pay by the millisecond that your code is actually running and you can, by default, have 100 of these running simultaneously without you having to manage or own a single server. Again - you only pay when they’re running so you can have an API which gets used once a month and only pay for the 2 seconds it runs but have 100% availability. This is incredible.

Auto Tweeting

There are lots of options for hosting a static website (including GitHub), so why choose AWS S3 which costs pennies a month? The main reason is the ability to integrate with lambda. I just finished writing a module in Python 2.7 using the tweepy library that is trigger each time I upload new posts to my S3 bucket which tweets out the title and the link. The Python code is pretty simple.

from __future__ import print_function

import json
import urllib
import boto3
import datetime
import tweepy
from titlecase import titlecase

now = datetime.datetime.now()

s3 = boto3.client('s3')


def lambda_handler(event, context):
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
    postDate = key[5:15]
    today = str(now.strftime("%Y-%m-%d"))
    if postDate == today:
        auth = tweepy.OAuthHandler('XXXX','XXXX')
        auth.set_access_token('XXXX','XXXX')
        api = tweepy.API(auth)
        postTitle = titlecase(key[16:][:-4].replace('-',' '))
        url = 'http://thug-r.life/'+key[:-4]+'/'
        
        post = 'I just wrote a post called "'+postTitle+'" at '+url
        print(post)
        api.update_status(post)
    else:
        print('Old post - ignore!')

I’m not going to go into how to get it all setup - I plan to write a tutorial on how I did everything with this blog in September so I can have at least the full first month costs.

Conclusion

AWS Lambda is pretty cool and you should learn about it. By using S3 to host this site, I can integrate with lambda to get extra automation around promoting my site.

comments powered by Disqus