Building with AWS ChatBot: Saying Hello to AWS ChatBot

Saying "Hello World" with AWS ChatBot to introduce the concepts of ChatOps

Posted by Steven Tan on 29th December 2023

Recently, I wanted to integrate my Slack workspace with AWS to build some chat-ops capabilities into my AWS environment. As part of this process, I experimented with AWS ChatBot. I decided to share some of my learnings in this series of blog posts called "Building with AWS ChatBot", where we will learn how to build ChatOps capabilities into your organisation and interact with your AWS environment directly from the chat window.

So what is ChatOps?

ChatOps is a way to facilitate development or IT operations tasks through a chatbot. Providing automation capabilities directly through chat allows self-service capabilities to users without having to navigate to a user interface and perform tasks manually.

AWS ChatBot is an AWS service that allows you to build ChatOps capabilities within your organisation's chat services. This service uses Amazon SNS to send native AWS and custom notifications to your chosen Slack channel. Click here for a list of supported AWS services.

A ChatOps example might be an approval step for AWS CodePipeline, where a notification is sent to a Slack Channel for someone to click on an "approval" button directly through the chat window. This action also provides some transparency, as interactions with ChatBots are publicly accessible and searchable to anyone in that channel.

Setting up AWS ChatBot

AWS ChatBot is configured initially in the AWS Console and primarily via your Slack chat window, making it a ClickOps-heavy service. Once you have it connected to your Slack workspace, you can configure your channels using AWS CDK.

chatbot_sns = sns.Topic(self, 'test_chatbot_sns')
slack_channel = chatbot.SlackChannelConfiguration(
    self,
    id="my_slack_channel",
    slack_channel_configuration_name="test",
    # Your Slack Workspace ID when viewing it via the web-browser and starts with T
    slack_workspace_id="T12345678",
    # Your Slack Channel ID (use the copy channel link and it's the segment that starts with C)
    slack_channel_id="C12345678",
)
slack_channel.add_notification_topic(chatbot_sns)

After deploying your AWS ChatBot Channel Configuration, you can invite it to your channel by mentioning it via @aws. Now, we are ready to start playing around with AWS ChatBot.

AWS ChatBot Caveats

Before you create your first custom command via AWS Lambda, there are some caveats I've found to using AWS ChatBot that you should know about.

  1. AWS Lambdas are executed as though they've been invoked via the AWS CLI, e.g. aws lambda invoke --function-name chatbot-test --region ap-southeast-2
  2. The Lambda event parameter is passed directly from the slack AWS CLI command, meaning that you will receive very little contextual information from the AWS ChatBot, e.g. aws lambda invoke --function-name chatbot-test --region ap-southeast-2 --payload '{ "name": $name }.'
  3. AWS ChatBot can use custom commands, but they are parameterised aliases for the AWS CLI commands you want to execute

Despite these caveats slightly hindering your ability to create complex interactions with AWS ChatBot, you can work around these limitations with some creativity which will be explored in a future blogpost.

Your First Custom Command

To create your first custom command, we will create a lambda function and provide AWS ChatBot permissions to invoke this. With AWS CDK, this is as simple as the following few lines of code:

with open("lambdacode/test.py", "r") as f:
    test_lmabda_code = f.read()
test_lambda = lambda_.Function(
    self, "test-lambda",
    code=lambda_.Code.from_inline(test_lmabda_code),
    handler="index.lambda_handler",
    runtime=lambda_.Runtime.PYTHON_3_9
)

test_lambda.grant_invoke(slack_channel)

We will write a simple "hello world" for the AWS Lambda Function to explore how it works before creating more complex interactions.

from typing import Dict

def lambda_handler(event: Dict[str, str], context):
    return f"hello, {event.get('name', 'anonymous')}"

Testing in Slack

Now that your AWS Lambda Function is deployed and ready to be used, let's try to run it from our AWS ChatBot. To successfully invoke it, you need to identify the name of your AWS Lambda Function and send the following message on your Slack channel.

@aws lambda invoke --function-name chatbot-test --region ap-southeast-2

You should receive a result similar to this.

AWS ChatBot Response

You can run the following command to pass through a payload via the events parameter.

@aws lambda invoke --function-name chatbot-test --region ap-southeast-2 --payload '{ "name": "Steven" }'

When received by your AWS Lambda function, your entrypoint events parameter will contain the following data. As you can see, the AWS ChatBot integration does not include any metadata about the message itself.

{
    "name": "Steven"
}

Sending an entire AWS CLI command over chat over and over would get old quickly, so let's move on to creating AWS ChatBot aliases.

Creating Your First Alias

Sending an entire AWS CLI command over chat over and over would get old quickly, so let's create an alias we can use instead.

Creating an AWS ChatBot alias must be performed in the Slack window, which is done by sending the message @aws alias create $alias_name $awscli_command where $awscli_command is the entire command you want to send but without the aws prefix. You can also use variables using the $paramatername syntax and execute them via the alias as positional values. For the Hello World example, this is what I used to create my Hello World alias.

@aws alias create hello lambda invoke --function-name chatbot-test --region ap-southeast-2 --payload '{ "name": "$name" }'

To use your alias, you will use the @aws run $alias_name $param1 $param2 syntax when sending your message. In our case, executing the Hello World alias will look like this.

@aws run hello steven

Sending that message results in a prompt from the chatbot asking to confirm the execution of the command before running the alias.

AWS ChatBot Alias Response

Pressing the [Run] command button will execute and return the response from your AWS Lambda Function.

AWS ChatBot Alias Response