This document discusses building a Slack bot using AWS Lambda and the Chalice framework. It describes how FaaS works, options for running functions through AWS Lambda including Python support. It then outlines how to build a Slack bot with Chalice that allows users to query Stack Overflow through natural language requests in Slack. Key steps include setting up the bot to handle requests, retrieving secrets securely, formatting responses, and deploying the code to AWS Lambda to be accessible through Slack. It also briefly discusses additional uses of FaaS beyond a basic bot including cron jobs and handling external events.
1. Function as a Service
Exploring slack bots with Chalice and AWS Lambda
richiverse
https://github.com/richiverse/slambda
2. What is a Function as a Service (FAAS)?
Function as a Service (FaaS) is a category of cloud
computing services that provides a platform allowing
customers to develop, run, and manage application
functionalities without the complexity of building and
maintaining the infrastructure typically associated with
developing and launching an app (Wikipedia)
3. Vendors
● AWS Lambda (node.js, java 8, python 2.7)
● Google Cloud Functions (node.js)
● Microsoft Azure Functions (JavaScript, C#, Python, PHP,
Bash, Batch, and PowerShell)
● IBM Whisk (NodeJS, Swift and arbitrary binary programs
encapsulated in Docker Containers)
4. Isn’t this Heroku (PaaS) all over again?
You are given very few options as to how your environment is
setup. You are given the freedom of choosing where/how
your functions get called.
Anywhere you are doing IO or responding to events or quick
running jobs (< 5 minutes) is a good candidate for using
FaaS.
No provisioning servers, no complex deployments.
5. ...So I went with AWS Lambda
One of the few that supports Python out of the box even
though it is using python 2.7 (...not bad)
● Gateway drug to other AWS services.
● Relatively stable, inexpensive relative to newcomers.
● More binaries may be announced early December
● Supports arbitrary binary execution with certain
deployment options.
6. Function Options
The point and click and CLI options give a lot of options over
how lambdas are run.
● Versioning
● Alias
● 1M free calls per month
● 4GB free compute memory per month
7. Frameworks to ease development
I ended up using Chalice because of the slick python
integration but if my project required tight integration with a lot
of other events or required docker containers I would have
gone with Gordon.
https://github.com/awslabs/chalice
https://github.com/jorgebastida/gordon
https://github.com/anaibol/awesome-serverless#frameworks
8. How does Chalice work?
Chalice takes your app.py as well as any dependencies in
requirements.txt, zips them up, and deploys an AWS API
Gateway endpoint to your function.
9. Requirements
Need AWS credentials saved in your home directory
Virtual environment and python 2.7 environment. I used
yyuu’s pyenv for a project specific 2.7 interpreter which
doesn’t interact with my system install of python 3.5 (also
using yyuu’s pyenv tool).
I created a user with the right policies for chalice to deploy
correctly.
10. Building a slack bot for Stackoverflow
I’m familiar with a python tool called howdoi which enables
asking SO questions at the CLI. How do I bring this useful
tool to my company? Ask them to pip install howdoi? That’s
too much to ask in this busy world!
Why not just bring the command line to them?! ;)
11. Hello World!
I used Pertinent Serpent’s Magic the Gathering slackbot as a
reference example of something pretty meaty over the
traditional hello world.
Things I did differently over this reference implementation
was using credstash for KMS to encrypt and store the Slack
token for authenticating users.
http://pertinentserpent.tumblr.com/post/147568685382/deploy
ing-a-slack-bot-on-amazon-lambda-with
12. Building a slackbot for Stackoverflow - Imports
I used python’s standard logging module to log events to
Amazon cloudwatch to track errors and log information about
the slack user/request.
13. Building a slackbot for Stackoverflow - Routes
Chalice has a similar syntax to Flask (Sinatra clone in
Python) where you decorate the route to your function. Here I
am using a built-in helper for query params.
14. Building a slackbot for Stackoverflow - secrets
By default, you deploy to dev env. Chalice roadmap includes
deploying to multiple stages. Here I also grab the keys that
I’m going to use from credstash. I setup some additional
contextual variables for retrieval in the credstash CLI.
15. Building a slackbot for Stackoverflow - Response
Here I quickly greet the user to avoid the 3 second Slack
timeout. I then process their query, format the response to
look nice in slack, then modify the payload and post it back in
the channel. After this, I run chalice deploy at the command
line and my code is available to test in slack.
17. Cron Jobs + Arbitrary executables
While this is actually cool enough, it doesn’t tap into all the
power of FaaS.
AWS Lambdas can run on a schedule (using cron)
AWS Lambdas can operate on arbitrary executables.
18. Event Driven development
In reaction to an event, such as:
● S3 Bucket events
● DynamoDB event
● SNS/Kinesis events
● IOT, Alexa, Log events