Serverless Slack Commands with Go & The Serverless Framework

Kevin Marquardsen
4 min readSep 23, 2017

Very much inspired by TJ Holowaychuk’s great post on using Up for a similar purpose. Check it out here.

In this post I’ll be walking you through a brief tutorial for creating Serverless Slack commands with Go and AWS Lambda using the Serverless Framework.

We’ll be creating a /dogs <breed> command which calls an external API to get an image of your favorite breed to display in Slack. In order to achieve this we’ll leverage the serverless-golang project.

First let’s build the app

If you‘re following along you’ll need to have serverless, go, make, npm, and docker installed and your project must reside in $GOPATH/src/.

At the time of this writing Serverless Golang is using the latest release of the Serverless framework 1.22.0. You can run: npm i -g serverless@1.22.0 to install and sls -v (shorthand for serverless commands) to validate. To save time I’ve provided an application we can use to get started. Assuming you have the Serverless framework installed simply run:

sls install -u

If all went well you’ll see something like:

Serverless: Downloading and installing “sls-go-slack”…
Serverless: Successfully installed “sls-go-slack”

You can now cd sls-go-slack and run cp .env.example .env.

.env is where we’ll be storing our AWS credentials. Replace WORKDIR with your own /go/src/your-path/sls-go-slack and populate AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION then you’re ready to deploy.

Simply run make build deploy

This will take roughly a minute while CloudFormation provisions all the necessary AWS resources for our application. If you see Serverless: Stack update finished… the service should be up and running. Save the POST url from under endpoints. We’ll need to give this to Slack in a moment.

So what just happened? Essentially, the Serverless Framework processes our serverless.yml file and generates a Cloudformation stack template. Our code also gets packaged as a zip and pushed to an S3 bucket. The zip contains a python shim which enables us to run our Go code from within the Lambda python runtime. When an event is received through the API gateway our Lambda will run the corresponding handler function.

One thing to call out in our handler is where we leverage an in channel response because “By default, the response messages sent to commands will only be visible to the user that issued the command” and we want persistent dog images to brighten everyone’s day.

The full handler can be found here. If you want to see more about what you can do with Slack slash commands checkout the documentation here.

Next let’s get the Slack side of things in place.

Slack Setup

First we’ll create a new Slack application.

Next we’ll need to register a new slash command.

After clicking Create New Command you’ll be taken to the Create New Command screen. This is where we’ll need our POST endpoint from the application deployment. Paste it in as the Request URL and fill in some other details.

Save the command and you should see a brief success message. Now we’ll need to install the app into our workspace.

You’ll be taken to another screen to authorize the application.

If you’re redirected to the application settings with an Oauth Access Token you’re all set and it’s time to test out the app.

Try out a few of the breeds from the Dog API’s breed list and see what kind of images come up. The full application can be found here if you want it to use it as a reference.

Hopefully you found that useful and are excited to build out your own Slack applications with Go and Serverless.