Serverless comments

This blog is entirely made up of pre-generated HTML files and a visit to this site does not require any database queries or other fancy processing than serving a couple of files (HTML, CSS and fonts). The benefits of this is that it is fast, secure and simple to maintain, the down side is that the content is entirely static. On blogs it usually possible to write a comment that is displayed to other visitors, including the blogger. With a static site this is not possible unless the blogger manually adds the comments to the HTML source code. What is possible however is to add some JavaScript code to call an external endpoint when posting and reading comments. This is what services like Disqus does. The problem is that there are privacy concerns with using a third party service dependent on Facebook and Google and Disqus is quite heavy, performance wise. Because of this, plus the fact that its more fun doing it yourself, I made my own “serverless” commenting system. It is serverless in the sense that it is not dependent on any single server running some code. Instead it is based upon AWS Lambda and DynamoDB.

I made two different Lambda functions for this, one for posting and one for reading comments, both access a DynamoDB table containing the comments. When posting comments a POST request is sent to the Lambda endpoint which runs the following Node.js script.

If you took the time to read the full script you probably noticed that it calls both Akismet and reCaptcha, this is to prevent bots and Spam being posted. Additionally it calls PushBullet to send alerts to me whenever someone posts a comment. When storing the comment the referrer (event.headers.Referer) is used to determine which post the comment was made on.

To fetch the comments whenever someone reads a blog post the following script is used.

Its much simpler than the one for posting, it simply reads the blog post URL as a get parameter and fetches any comments from DynamoDB. In addition to these two scripts there is some code on this site which you can view by simply right clicking and then dig through the source code for any relevant parts.

As with all other AWS services you pay for what you use. I am currently on the free usage tier but once that’s finished this setup will cost $0.99 / month for DynamoDB and a couple of cents more for the Lambda service. The cost for Lambda depends on the number of visits, or rather the time the functions run for, the cost for DynamoDB is fixed as the capacity is constantly there and is set by the user (me).

Feel free to test the commenting system out below and if you have any questions you can find my contact details under about.

Write a comment