Building a Custom Slack Slash Command using AWS Lambda

Terrence Mahnken
5 min readFeb 27, 2020

--

Photo by James Pond on Unsplash

TL;DR

  1. Create your API using a language/framework of your choice.
  2. Package and deploy to AWS using Claudia.
  3. Create your custom Slack Slash Command to point to your deployed API.

Background & Context

Slack’s Slash Commands provide great functionality with minimal user input. In my own personal experience, I’ve continually used such slash commands as /remind, /giphy, and the infamous /shrug … ¯\_(ツ)_/¯. Slack allows users to create their own slash commands to meet the needs or their workspace.

In my case, I wanted to develop a custom slash command for my class’ Slack workspace in order to generate a random name.

Slack provides documentation about their Slash Commands and a handy tutorial for creating your first slash command and custom integration. In full disclosure, the tutorial is about building a legacy custom integration, but I figured the same concepts would still apply.

Slash Slash Command Data Flow.

Additionally, I chose AWS Lambda because …

  1. I have been trying to get more hands-on experience with the AWS platform.
  2. Based on my expected usage of the custom Slack integration, the free Lambda tier would be more than sufficient. From the AWS Lambda Pricing guide, “The AWS Lambda free usage tier includes 1M free requests per month and 400,000 GB-seconds of compute time per month.

Yeah … probably more than enough. 👍 Plus, standing up an entire EC2 instance just to run 1 API endpoint is a bit overkill. Lambda to the rescue!

Prerequisites

  1. Access to a Slack Workspace
  2. Knowledge of Node.js and Express.js*
  3. AWS account and/or AWS Administrative access
  4. AWS CLI (Installation Guide)
  5. Claudia.js

*You can use any back-end language and web framework you are comfortable with. I chose Node.js and Express.js for this integration project.

Building your API Endpoint

1.) Initialize your Node application.

npm init -y

2.) Install Express.js as a project dependency.

npm install express -s

3.) Create an app.js file and add your Express GET endpoint code:

"use strict"
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello World!");
});
const port = process.env.PORT || 3000;app.listen(port, () => {
console.log(`Server is listening on port ${port}.`)
});

4.) Test your endpoint.

node app.js
Starting Node Application
Testing Local API Endpoint with Postman

Preparing your Endpoint for Deployment

1.) In the AWS Console, create a new IAM User called “Claudia” and assign the following policies:

  • AWSLambdaFullAccess
  • IAMFullAccess
  • AmazonAPIGatewayAdministrator
AWS Claudia User IAM Permissions

2.) After creating the Claudia AWS IAM User, copy the Access Key ID and Secret Access Key values and add them to your ~/.aws/credentials file.

[claudia]
aws_access_key_id = <Claudia_AWS_User_Key_ID>
aws_secret_access_key = <Claudia_AWS_User_Secret_Key

3.) Copy your app.js file to app.local.js. This will allow you to continue testing locally by running node app.local.js. Any changes will need to be copied over to app.js.

cp app.js app.local.js

4.) Modify your app.js file to prepare for deployment to AWS Lambda via Claudia.

Remove

const port = process.env.PORT || 3000;app.listen(port, () => {
console.log(`Server is listening on port ${port}.`)
});

and replace with

module.exports = app

5.) Install Claudia as a project development dependency.

npm install claudia -D

6.) Modify your package.json file to include Claudia scripts.

"claudia-generate": "claudia generate-serverless-express-proxy --version dev --express-module app",
"claudia-create": "claudia create --handler lambda.handler --deploy-proxy-api --region us-east-2 --profile claudia",
"claudia-update": "claudia update --profile claudia"

*For the --region argument, specify the appropriate AWS region.

Deploying your API Endpoint to AWS

1.) Run npm run claudia-generate

This will generate a lambda.js file and install the aws-serverless-express NPM package.

2.) Run npm run claudia-create

This will create a REST API in AWS Application Gateway, create the AWS Lambda function, and provide an AWS endpoint:

{
"lambda": {
"role": "slack-test-executor",
"name": "slack-test",
"region": "us-east-2"
},
"api": {
"id": "<API_Gateway_ID>",
"url": "https://<API_Gateway_ID>.execute-api.us-east-2.amazonaws.com/latest"
}
}

3.) Copy the provided API URL and test using Postman or a browser.

Browser

Testing AWS Endpoint in Browser

Postman

Testing AWS API Endpoint in Postman

4.) Run npm run claudia-update to deploy updated code. The API Endpoint URL will still end with /latest.

Testing AWS API Endpoint in Browser after Update

Connecting your Endpoint to Slack

Now, the moment you’ve all been waiting for! Drum roll, please!!! 🥁

🎉🎉🎉 Connecting our Endpoint to Slack!!! 🎉🎉🎉

1.) From your Slack Workspace, select “Customize Slack” and on the left-side menu, select “Configure apps.”

2.) In the top search bar, search for “Slash Commands.”

3.) Click the “Add to Slack” button.

4.) Enter the slash command that will envoke our AWS API Endpoint. For example, “/hello”.

5.) On the subsequent configuration page, configure the following settings:

  • URL = Provided Lambda Endpoint
  • Method = GET
  • Customize Name = Hello From AWS
  • Customize Icon = Choose an Emoji

6.) Click “Save Integration.”

7.) Test your newly created Slash Command.

End-to-End Testing

Summary

Here is the high-level overview of the steps we took to deploy and integrate our endpoint:

  1. Build and test your API endpoint locally.
  2. Configure an AWS IAM User with the appropriate permissions.
  3. Reconfigure the app to use Claudia.
  4. Deploy and test your AWS endpoint.
  5. Create and configure your custom Slack Slash Command.

Additionally, you can customize your slash command to accept arguments, display custom errors, and change the message visibility. More information can be found at Slack’s Slash Command Documentation.

It should be noted, that this is a legacy setup but is still currently supported by Slack. Slack’s recommendation is to replace custom integration with Slack Apps. Perhaps, a follow-up project and article are in the offing. 😉

Acknowledgments & Contact

First of all, BIG shoutout to Slobodan Stojanović for his incredibly helpful article about deploying Express apps to AWS using Claudia. 💯

Secondly, thanks to Alec Down for the idea. 👍

Thirdly, You can find my working Slack Integration code in my GitHub Repo.

Finally, if you have any questions, comments, or suggestions, I would love to hear from you. You can also find me at the following outlets:

--

--

Terrence Mahnken

Husband. Father. Student. Senior Front End Engineer @ CoStar Group