A bit of coding, a byte of the cloud. Learning to code a cheap and cheerful alerting tool in an AWS Lambda function.

Code can do it. You can code. Do it.

Years ago I learned on the job to write little PERL programs and Shell scripts to manipulate data in files submitted by clients who could not deliver their transaction data conformed to a record layout that my employer’s application could ingest. Did I lose you already? I chopped up some stuff to make it work. That’s what I love about code, it can do anything to anything for anything that needs doing with data.

At Tesloop, needs are much more realtime. When one of our vehicles arrives at a location where it is scheduled to pick up or drop off passengers, it’s that moment and the minutes that follow when the driver (pilot) or passenger may need help from the Operations team. Our developers created an SNS topic on AWS that our serverless application (using Kinesis Analytics) posts to when a vehicle enters a custom geofence that we have defined around each location. To give the team a heads up, a Slack post is best. Our driver mobile app also posts to Slack when arriving at locations, but redundancy is good and triggering based on the vehicle data creates some additional benefits. All we needed was a way to make a Slack post for each post to the SNS topic.

Inspired by a post on Medium by Joseph Terranova it seemed trivial to extend his code to extract a vehicle ID number from the data and construct a link to our fleet monitoring dashboard. I did this as a proof of concept, to be improved upon or completely re-architected by our developers later. The immediate goal was simply to alert the Ops team.

Below is the code you can copy-paste into your Lambda function after following Joseph’s instructions for creating your Slack webhook and SNS topic. Don’t forget to replace the values for:

  1. Slack webhook path (line 1)
  2. Slack channel (line 2)
  3. Slack posting username (line 3)
  4. Switch values (line 10-18)
  5. Message template (line 28)
var services = '/services/…'; // Update this with your Slack webhook URL path
var channel = '#fleet-status'; // Update this with the Slack channel to post in
var username = 'Vehicle GeoFence Alert'; //Update this with the Slack username to show on the posts
var https = require('https');
var util = require('util');
function vehicleName(vehicleId) {
switch (vehicleId) {
case 9999991:return "Rex";
case 9999992:return "Toro";
case 9999993:return "Ruby";
case 9999994:return "Wally";
case 9999995:return "Deuxy";
case 9999996:return "Bravo";
case 9999997:return "Duke";
case 9999998:return "eHawk";
default:"Mystery Mobile"
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
var vehicleId = event.vehicle_id;
var message = vehicleName(vehicleId) + ' is arriving at a stop! See it on the fleet dashboard: https://fleet.tesloop.com/dashboard/vehicles/' + vehicleId;
var postData = {
"channel": channel,
"username": username,
"text": message,
"icon_emoji": ":round_pushpin:"
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: 'services'
var req = https.request(options, function(res) {
res.on('data', function (chunk) {
req.on('error', function(err) {
console.log('Houston, we have a problem: ' + err.message);
req.write(util.format("%j", postData));

view raw
hosted with ❤ by GitHub

The result for me was nice little notification posts like the one shown below. Done. Did it.

Slack say what?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s