Use AWS Chatbot to run your AWS Lambda function from Slack

Photo by Alex Knight on Unsplash

Use AWS Chatbot to run your AWS Lambda function from Slack

In this tutorial we are going to use AWS Chatbot to run a Lambda function remotely from Slack.

To create the lambda and the SNS Topic to talk to the Bot, we are going to use CDK. To setup the ChatBot we'll do it on the console.

The Lambda

Let's create our project

mkdir chatBotLambda
cd chatBotLambda
npm init -y

Then install CDK and the packages we need

npm install -g aws-cdk
npm i @aws-cdk/aws-lambda
npm i @aws-cdk/aws-sns
npm i @aws-cdk/aws-sns-subscriptions
mkdir cdk-folder
cdk init app --language typescript

Open the file lib/cdk-folder-stack.ts

import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as sns from '@aws-cdk/aws-sns';
import * as subs from '@aws-cdk/aws-sns-subscriptions';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as path from 'path';

export class CdkFolderStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // sns topic
    const topic = new sns.Topic(this, 'my-lambda-notifications', {
      displayName: 'My SNS topic',
    topic.addSubscription(new subscriptions.EmailSubscription("your-email@adress");

    // lambda function
    const myLambdaFunction = new lambda.Function(this, 'my-lambda', {
      runtime: lambda.Runtime.NODEJS_14_X,
      memorySize: 256,
      timeout: cdk.Duration.seconds(60),
      handler: 'index.main',
      code: lambda.Code.fromAsset(path.join(__dirname, '/../app/my-lambda/')),

Lambda part Now the lambda code

mkdir app
cd app
mkdir my-lambda
cd my-lambda
npm init -y 
touch index.js

Very simple code for the lambda :

exports.main = async (event) => {
        console.log('Hello ChatBot');

Bootstrap and deploy

cdk bootstrap
⏳  Bootstrapping environment aws://XXXXXXXXXXXXX/us-east-1...
 ✅  Environment aws://XXXXXXXXXXXXXX/us-east-1 bootstrapped (no changes).

cdk deploy

Now we have our lambda, next step, the ChatBot

The ChatBot

Open the AWS Chatbot console.

Under Configure a Chat client choose Slack, and then choose Configure.

Capture d’écran 2022-02-01 à 13.49.18.png

In the upper right corner, choose the dropdown list, and then choose the Slack workspace that you want to use with AWS Chatbot and choose Allow.

Choose Configure new channel.

Under Configuration details, for Name, enter AWSChatBot.

Under Channel type, choose Private.

Navigate to Slack and create a private channel by choosing the + button to the right of Channels.

Choose Create a channel.

Name the channel AWSChatBot and make it private then hit create.

When prompted to add people, choose x.

Navigate back to the AWS Chatbot console and enter the private channel ID.

Define the IAM permissions that the chatbot uses for messaging your Slack chat room:

For Role name, enter AWSChatBotRole.

For Policy Templates, select Read-only command permissions and Lambda-invoke command permissions.

In the SNS topics section, choose the appropriate AWS Region under Region.

Under Topics, select the AWSChatBotNotifications topic.

Choose Configure.

Your ChatBot is ready !

Make them work together

Invite yout AWS Chatbot to your channel by doing the following in Slack :


hit enter and choose invite to the channel.

Enter the following command in Slack

@aws lambda invoke --function-name <your-function-name> --region <your region>

The output should be :

Capture d’écran 2022-01-31 à 17.40.52.png

Congrats ! You can now invoke your lambda from Slack ! Pretty convenient to test out some stuff !

Clean up

To destroy the lambda and the SNS topic :

cdk destroy

To delete the chatbot :

  • Open the AWS Chatbot console.

  • Choose Slack.

  • Choose the radio button next to the channel you created and then choose Delete.

Did you find this article valuable?

Support Sonia Manoubi by becoming a sponsor. Any amount is appreciated!