Serverless Slack Commands with Go & The Serverless Framework
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
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 firstname.lastname@example.org 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 https://github.com/mangatadigital/sls-go-slack
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.
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.
Next let’s get the Slack side of things in place.
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.
Hopefully you found that useful and are excited to build out your own Slack applications with Go and Serverless.