Using JavaScript & AWS CloudWatch for Scheduling Website Content [Tutorial]

Written by alex-daro | Published 2020/02/22
Tech Story Tags: javascript | cloudwatch | aws | aws-lambda | hackernoon-top-story | content-scheduling-automation | automation-top-story | javascript-top-story

TLDR Newclick.io is a tool to help you schedule promotional content on your website. CMS platforms like Shopify and Wordpress allow users to set publish dates for things like products and blog posts. The tool uses AWS CloudWatch and the AWS JavaScript SDK to create scheduled events. The developer wanted to use the serverless framework and deploy to AWS Lambda. He wanted to keep the development focused on code and less on server-related infrastructure. For each event that’s scheduled I created a new CloudWatch Rule that runs a Lambda function for a given date and time.via the TL;DR App

Scheduling website content is hard. Most content management systems (CMS) have features for publishing products, blog articles, and other types of platform-related content, but what about simply scheduling HTML? As developers sometimes we want to show a section of our website during a certain time frame.

Think holiday sales or launch events. Surprisingly this is harder to pull off then one might think. You have to set up some sort of cron that triggers some function that publishes some piece of content.
It can be tough to build! That’s why I’m building newclick.io, a tool to help you schedule promotional content on your website. We’re starting with something simple, announcement banners, but will soon branch out to different types of components.
In this article, I’ll briefly describe some of the things that went into developing the newclick.io Scheduling API.

Architecture ✏️

The first step was to figure out if this was even possible. How are developers scheduling content today? CMS platforms like Shopify and Wordpress allow users to set publish dates for things like products and blog posts, so it’s definitely achievable, but certainly, they aren’t using something like a crontab, are they?
No way!
It’s 2020 we run things in the cloud these days so any type of server-side crontab would be out of the question. This included out of the box solutions such as the node-schedule and node-cron libraries. These wouldn’t work with how I wanted to architect my servers.
From the very beginning, I wanted to use the serverless framework and deploy to AWS Lambda. We didn’t want to think about spinning up EC2 instances and worrying about uptime and other server-related things.
I wanted to keep the development focused on code and less on infrastructure. I was also intrigued by the inherent costs savings you get with Lambda functions. You don’t pay for idle server time, which made sense for my fledgling startup.
“So how am I going to pull this off?”, I thought. Well, I can’t use a traditional server cron so I needed to look elsewhere. That’s when I found AWS CloudWatch.
CloudWatch is used for what AWS describes as “application and infrastructure monitoring”, but also provides tooling around scheduling crons and other event-related processes. “This is great!” I thought to myself. I could use CloudWatch along with the AWS JavaScript SDK to create our scheduled events. Onward!

Development 🛠

So with my new tool in hand, I was ready to start writing some code. I started by figuring out how to use the AWS SDK to interface with CloudWatch.
First I needed to create a constant that would be available throughout our entire application. I started by defining and exporting a 
cloudwatchEvents
 service.
I used the CloudWatchEvents constructor to define a constant. It takes an AWS access key, secret key and region for it’s parameters. You can pass those in using environment variables, good practice in case these end up changing later.
Now that we have our 
cloudWatchEvents
 service ready to go, we have access to the 
putRule
 function. This allows you to schedule an expression using a cron-like syntax. We also have access to the 
putTargets
 function which allows you to define a target, in our case a Lambda function (more on this later). For each event that’s scheduled I created a new CloudWatch Rule that runs a Lambda function for a given date and time. The expression passed into the function looks a lot like your standard cron expression.
The last step is to attach our target. The target is the 
RuleArn
 returned back from our 
putRule
 call. This tells our CloudWatch event to only processes this ARN (Amazon Resource Names).
And there you have it! We have a scheduled task ready to rock🤘
In follow up article are go into how and why this wasn’t enough, and why I needed to “clean up” our rules after they executed. Stay tuned!
If you enjoyed this, feel free to follow me on Twitter and take a look at newclick.io if you’re interested in scheduling your next promotion.

Published by HackerNoon on 2020/02/22