Create a Serverless Slack Bot with AWS Lambda and Python

In true newline-fashion, we'll get *rolling* with a practical application right away. We're creating a fully functional, dice-rolling chat bot for Slack!

Serverless Computing may sound like a scary topic, but with the rise of AWS Lambda and mature tools supporting it - it's more approachable than ever. In this newline course, we're going to learn what serverless technology is and isn't, and why it belongs in your developer toolbox. We'll see that for certain common projects - like custom chat bot creation - AWS Lambda really shines for it's simplicity, affordability, and ability to integrate with the wider AWS ecosystem.

Some developers love using serverless tech because it helps them save money, scale quickly, and deliver on reliability goals. While all of those things are possible with Lambda (especially cost savings which are common - but certainly not guaranteed), it's the development agility and reduction of operations overhead that drives my love for the service. It's a relief to be able to get right into the code, without worrying about configuring operating systems, deploying containers, or scaling clusters. Serverless on Lambda gives developers the resources needed to write, deploy, and maintain production-ready software - all without significant operational overhead.

In true newline-fashion, we'll get rolling with a practical application right away. RPG players rejoice - we're creating a fully functional, dice-rolling chat bot for Slack! While the example bot will be for Slack, the same idea can be easily adapted to create a bot for Telegram, Discord, or nearly any other chat service with a bot API.

Once you've learned the foundations of serverless, you can make much more sophisticated bots (or full applications) that use the stack we're going to learn here.

We will be using Python 3 for this project, but it's worth noting that AWS Lambda works with other common languages and runtimes, including NodeJS, Java, ruby, go, and .NET. The basic mechanisms of creating, testing, and deploying your Lambda do not vary significantly based on your runtime choice.

Code for the Course#

We'll be showing code in this manuscript along the way, but if you want to skip ahead:

You can download the completed code for the course here.

About the Author#

I'm your author, Steph Prime. I'm a DevOps Engineer by title, and a technical generalist with years of AWS Lambda development experience in practice.

When I worked at AWS, I was one of the first Engineers to preview Lambda internally, and to use it before it was released to the public. Even in it's original, unpolished state - it was obvious that serverless technology was poised change the software development world in a big way.

Since my days working at AWS, I've worked at companies that range from small VOIP startups, to established managed services companies with massive enterprise clients. No matter the size or the stakes - every place I've worked has found a use for Lambda in it's ecosystem, and is still using it today.

The flexibility, resiliency, and cost effectiveness of the AWS Lambda stands the test of time, even in the fast moving tech world.

Lambda as a service itself is remarkable, but the what really makes it so powerful is the agility that effective Lambda developers can achieve. Thanks to serverless technology, it has never been easier to write, deploy, and maintain production-ready software - at any scale.

Why AWS Lambda?#

Amazon Web Services (AWS), which released AWS Lambda in 2014 - is estimated to control 50% of the cloud computing market. At this time of this writing, it's estimated that AWS controls 3x more resources than its closest competitor, Microsoft Azure. AWS is widely used by startups and enterprises alike, has relationships with schools and startup incubators to provide free credits, and is battle tested from years of production experience. The underlying technology that AWS provides to it's customers, is the same technology that Amazon uses to host their own site - and it's available to use throughout the world for reasonable prices.

I spent some time working at AWS in Seattle - and I saw the internals of this organization first-hand. Even though my role at AWS revolved around troubleshooting things that could go wrong - I was always impressed by how much AWS does right. No company is perfect, but the folks at AWS take their job seriously and the core systems at AWS are as solid as technology gets. I remember attending an meeting with the Lambda team presenting their work internally, right before the public announcement was released. Since then, I've grown even more excited as I've seen the great work that AWS has put into improving Lambda, adding features and integrations - all without sacrificing reliability and scalability. No matter the size or the stakes - every place I've worked at in my post-AWS career has found a practical use for Lambda in their technological ecosystem, and is happy to have made the investment in using it.

Why Python?#

Python has been supported by AWS Lambda since the very early days. If you search around, you'll find a treasure trove of documentation, blog posts, and GitHub repos for Python Lambdas, many of these written by AWS employees themselves!

It's a language that is approachable both to systems engineers and developers, and it has mature and widely-used libraries that work great for everything from Image Processing, Machine Learning, and network automation. It's a great general utility language to be familiar with, and works with Lambda right out of the box.

Some terms to know#

These terms are commonly used in the Cloud Computing industry and do relate to serverless - so it's good to have some familiarity with them.

  • IaaS (Infrastructure as a Service) A service that provides network and server infrastructure that consumers can use to run software on top of. Typically, this means a set of APIs that allow consumers to create virtualized servers that operate on hardware which is managed by the provider, and typically shared by multiple clients.

    Examples: Amazon Web Services (AWS), Microsoft Azure, and Google Compute are common IaaS providers.

  • PaaS (Platform as a Service) A service that provides tools to streamline software deployment on top of IaaS providers. These services typically abstract the of infrastructure management side away from the consumer, and handle automatic scaling of infrastructure.

    Examples: Heroku, AWS Elastic Beanstalk, Google App Engine, Managed Kubernetes (offered by multiple providers)

  • Serverless An architecture used for developing, and deploying software without managing any underlying servers. In many cases, the code is executed within a container that was is managed by a PaaS provider, which runs on a server managed by an IaaS provider. But since the end user is not seeing or managing the server underneath everything, we just pretend like it isn't there and call it serverless.

    Examples: AWS Lambda, Azure Serverless.

    NOTE: there is a particular application framework for managing certain serverless deployments that is called serverless . That specific tooling is not necessary to use Serverless technology in general, and frankly naming a framework serverless was kind of a confusing move. We won't be using that framework it in this course, because everything we need is provided by a different framework, AWS Serverless Application Model.


Before we get to the fun stuff though - we must be prepared to configure a few pieces of tooling in our local development environment. If you're already using AWS, this should go fairly quickly. If you don't have an AWS Account, you will need to create an AWS account and get it configured and secured according to best practices - which may take a few hours to complete. Whatever your situation, select the link that applies to your situation and we'll help you get sorted out!

Start a new discussion. All notification go to the author.