Business Productivity

Event-Based Amazon Chime Webhooks using AWS Lambda and Amazon EventBridge

As complexity in projects and workloads increases, there is a constant need to automate manual tasks to save teams and customers time and resources. Customers have used this automation to set up reminders for deployments, on-call rotations, and maintenance. Amazon Chime is a communication service used by organizations to connect teams. Amazon Chime offers webhooks as a feature that allow users to programmatically send messages to chat rooms. Amazon EventBridge can be used to connect services together to deliver messages or data to targets like AWS Lambda based on an event rule or a schedule. AWS Lambda is a compute service that runs your code in response to events. In this blog, you will need to have the following prerequisites to set up event-driven and scheduled Amazon Chime webhooks with AWS Lambda and Amazon EventBridge: an Amazon Chime account and an AWS account to access the AWS console.

Solution overview

In this blog post, we show you how you can use Amazon EventBridge with AWS Lambda and Amazon Chime webhooks to help reduce manual workloads by scheduling messages. Note: Deploying this demo and receiving traffic from the demo created in this post can incur AWS charges.

An Amazon EventBridge rule is triggered with a target of an AWS Lambda. The AWS Lambda is then invoked, which makes a POST request to the Amazon Chime webhook, which then delivers a message to the specified chat room.

Figure 1: Example Solution Architecture

Figure 1: Example Solution Architecture

Prerequisites

  • Amazon Chime account
  • AWS account to access the console

Step 1: Setting up your Lambda function

  1. Set up your work environment by creating a directory by running the following command: mkdir ~/myProject
  2. Install Python 3.8 or greater for your respective platform. Follow the directions on the installation site here.
  3. Install Pipenv, a dependency manager for Python that simplifies management of dependencies. Install by running the following command:
    pip3 install --user pipenv
  4. Install the requests package using the following command: pipenv install requests

    NOTE: The Pipenv installation might require adding your user’s binary directory to your PATH. For linux, this is generally the fully expanded path of ~/.local/bin. You can check with python3 -m site --user-base and append “bin” to the end of the returned directory path. To ensure Pipenv is usable in your terminal every time it is reopened, add the following to your relevant shell file, i.e. .bashrc, .zshrc, etc

    if [ -d "$HOME/.local/bin" ] ; then
        PATH="$HOME/.local/bin:$PATH"
    fi
  5. Save the Virtualenv location in your notepad.

    Figure 2: Output after running pipenv install requests

Step 2: Generate Amazon Chime webhook URL

  1. Install Amazon Chime.
  2. Create a chat room, by clicking “Rooms” at the top of the Amazon Chime Window.

    Figure 3: Amazon Chime Rooms tab

  3. Click, “Create a chat room”.

    Figure 4: Create a chat room

  4. Name your chat room and click “Create.” The next screen will allow you to add others to the chat room. After adding participants, the chat room is ready to be used.

    Figure 5: Naming a chat room

  5. Add a webhook to the chat room by clicking the gear icon in the top right corner and click “Manage webhooks and bots”.

    Figure 6: Manage webhooks and bots

  6. Click “Add webhook”.

    Figure 7: Adding the webhook

  7. Name the webhook.

    Figure 7: Naming the webhook

  8. Click “Copy URL” and save it.

    Figure 8: Copying webhook URL

Step 3: Writing AWS Lambda Function

  1. Copy the contents of the following sample Python code and save it in a new file named webhook.py. Replace the webhook URL and message where indicated.
import json
import requests

def lambda_handler(event, context):
    data = {"Content":"ReplaceWithMessage"}

    response = requests.post(
        url="ReplaceWithWebhookURL",
        json=data
    )
    return {
        "statusCode": response.status_code,
        "body": json.dumps("Hello from Lambda!")
    }

Step 4: Packaging AWS Lambda Code to Upload

  1. Replace the Virtualenv directory and Python file directory in the following commands with yours to zip the packages:
    cd <venvDirectory>/lib/python3.X/site-packages/
    zip -r <pythonFileDirectory>/packages.zip .
  2. Navigate to your Python file directory and add your script to the root of packages.zip:
    zip -g packages.zip webhook.py

Step 5: Create AWS Lambda function

  1. Navigate to the AWS Lambda console.
  2. Click “Create function”.

    Figure 9: Creating AWS Lambda function

  3. Choose “Author from scratch”.
  4. Give your function the name “Chime_Webhook”.
  5. Choose your Python runtime, matching the version you used.
  6. Leave all other options as default.
  7. Click “Create function”.

Step 6: Upload to AWS Lambda console

  1. Click your newly created AWS Lambda function and click the “Upload from” button then select your zip file. Once the upload is successful, you’ll see a green banner stating “Successfully updated the function Chime_Webhook”.

    Figure 10: Uploading AWS Lambda code

  2. In order for AWS Lambda to use our method, update the Handler in the runtime settings. Click “Edit” within “Runtime settings” and change the field to the Python file name and method name: webhook.lambda_handler

    Figure 11: Updating Handler

  3. Test the AWS Lambda by clicking the “Test” tab and create a new event from the “hello-world” template. Click “Test” and your message will be sent to the Amazon Chime chat room.

    Figure 12: Testing the AWS Lambda function

Step 7: Setting up the Amazon EventBridge rule

  1. Navigate to the Amazon EventBridge console.
  2. Click “Create rule” and give it a name.
  3. Choose “Schedule” pattern, then “Cron expression”.
  4. We’ll use a cron expression for every Friday at 4pm PST:
    0 23 ? * FRI *

    Figure 13: Defining Amazon EventBridge pattern

  5. Select the previously-created AWS Lambda as the target for this rule and click “Create rule”.

    Figure 14: Creating rule

You should now have set up an event-based Amazon Chime webhook using AWS Lambda and Amazon EventBridge.

Cleanup

For easy clean up, delete the AWS Lambda function, Amazon EventBridge rule, and Amazon Chime webhook.

Use the following steps to delete or disable an AWS Lambda function.

  1. Open the AWS Lambda console.
  2. Choose the name of the function to delete.
  3. Click “Actions” in the top right.
  4. Click “Delete function”.
  5. Click Delete.

Use the following steps to delete or disable an Amazon EventBridge rule.

  1. Open the Amazon EventBridge console.
  2. In the navigation pane, choose Rules.
  3. Under Event bus, select the event bus that is associated with the rule.
  4. Do one of the following:
    a. To delete a rule, select the button next to the rule and click Delete.
    b. To temporarily disable a rule, select the rule and choose Disable.

Open Amazon Chime and follow the steps below.

  1. Navigate to the “gear” icon on the top right.
  2. Click “Manage webhooks.”
  3. Select your webhook and click “Delete.”

Conclusion

In this blog, we’ve set up a pipeline that allows you to programmatically send messages to Amazon Chime chat rooms with the use of AWS Lambda and Amazon EventBridge. This process allows you to automate and trigger messages based on events which can help save you time on manual tasks.

For more information or related blogs visit:
Webhooks for Amazon Chime
Adding Webhooks to Chat Rooms
Amazon Chime Developer Guide
Amazon EventBridge User Guide

Matthew Johnson

Matthew Johnson

Matthew Johnson is a DevOps Consultant for AWS Professional Services based out of Seattle, WA. As a consultant, he supports customers with architecting, developing, and operating DevOps solutions for their applications. In addition to DevOps, Matthew's other passions include building custom mechanical keyboards and spending time with his wife and cats.

Jasmeen Sembhi

Jasmeen Sembhi

Jasmeen Sembhi is an Advisory Product Engineer for AWS Professional Services based out of San Francisco, CA. She helps customers in delivering strategic transformation, development, and operations strategies through product centric thinking. Jasmeen enjoys spending time with her dogs and bicycling to her favorite lakes.