Where’s the car? Learning to code an alerting solution in an AWS Lambda function.

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

As a co-founder and leader at Tesloop I was constantly finding ways that code could solve problems. Tesloop provided a new kind of shared-ride transportation service in Tesla cars. The shared ride model and the electric cars both presented unique challenges. 

One challenge was coordinating between drivers and passengers to meet for pick up. Tesloop picked up passengers at public locations to keep the ride moving for other passengers already in the vehicle. When a driver arrived, we automatically sent the passenger a text message update. Passengers often needed a little help finding the driver. We needed to know when to be ready to help. 

The Tesloop engineers had already created a solution that texted the passenger. When the vehicle location data told us the vehicle was at the location our code created a message on Amazon Simple Notification Service (SNS). The Tesloop operations team needed their own alerts to monitor the entire fleet of vehicles. The best place to get those alerts was in a Slack channel. I wanted to find a solution with the least amount of code to turn those SNS messages into Slack posts.

Reducing our unique challenge into a simple problem statement allowed me to search whether anyone had shared a solution. I found a post on Medium by Joseph Terranova that described a perfect solution. Joseph’s code only needed a few small additions to convert the data in the SNS message into a Slack post with all the details the operations team needed.

I followed Joseph’s instructions for creating a Slack webhook and SNS topic. Then I created my version of the Lambda function to complete the solution. My code is shown below. If you copy it, 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.setEncoding('utf8');
res.on('data', function (chunk) {
context.done(null);
});
});
req.on('error', function(err) {
console.log('Houston, we have a problem: ' + err.message);
});
req.write(util.format("%j", postData));
req.end();
};
view raw snsToSlack.js hosted with ❤ by GitHub

This gave us automatic Slack posts that kept the operations team updated like the one shown below. 

Slack say what?

Exactly what we needed with the least amount of code.

Done. Did it.

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 )

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