Search code examples
typescriptamazon-web-servicesaws-cdkaws-chatbot

Is there a way to customise slack message by AWS Chatbot?


I am trying to send slack notifications using aws chatbot created using cdk.

In production, there are many lambda functions which have cloudwatch alarm for particular 5xx errors or 4xx errors.

i have tried to replicate this situation in my environment by deploying a lambda and attaching a cloudwatch alarm which sends notification to sns and in turn chatbot sends a notification to slack.

Till this point everything works good, however i am not happy with the link recieved in slack by chatbot. it points to cloudwatch alarm.

Is it possible to add the link to the cloudwatch alarm on which metric was created so that on clicking to link i can be redirected to cloudwatch log rather than cloudwatch alarm.

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

    // Lambda
    const helloworldFn = new lambda.Function(this, "sample-lambda", {
      code: new lambda.AssetCode("./lib/lambda"),
      handler: "hello-world.handler",
      runtime: lambda.Runtime.NODEJS_14_X,
    });

    // Lambda Log Group setting
    const metricFilter = helloworldFn.logGroup.addMetricFilter(
      "Keyword Filter",
      {
        metricNamespace: "chatbot-sample",
        metricName: "filter-by-keyword",
        filterPattern: { logPatternString: "\"HELLO, Slack!\"" },
      }
    );

    // Chatbot Role & Policy
    const chatbotRole = new iam.Role(this, "chatbot-role", {
      roleName: "chatbot-sample-role",
      assumedBy: new iam.ServicePrincipal("sns.amazonaws.com"),
    });

    chatbotRole.addToPolicy(
      new iam.PolicyStatement({
        resources: ["*"],
        actions: [
          "cloudwatch:Describe*",
          "cloudwatch:Get*",
          "cloudwatch:List*",
        ],
      })
    );

    // SNS TOPIC
    const topic = new sns.Topic(this, "notification-topic", {
      displayName: "ChatbotNotificationTopic",
      topicName: "ChatbotNotificationTopic",
    });

    const alarm = new cloudwatch.Alarm(this, "Alarm", {
      metric: metricFilter.metric(),
      actionsEnabled: true,
      threshold: 0,
      evaluationPeriods: 5,
      datapointsToAlarm: 1,
      comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
    });

    // 通知さきのトピックを指定
    const action = new cwactions.SnsAction(topic);

    alarm.addAlarmAction(action);

    // Chatbot Slack Notification Integration
    const bot = new chatbot.SlackChannelConfiguration(
      this,
      "sample-slack-notification",
      {
        slackChannelConfigurationName: 'amplify-github-jatin-notifications',
        slackWorkspaceId: 'xxxxxx',
        slackChannelId: 'xxxxxx',
        notificationTopics: [topic],
      }
    );
  }
}

Solution

  • No, there isn't. AWS Chatbot doesn't allow you to specify your own templates for the messages, you have to use the ones defined by AWS.

    If you want custom Slack messages, you'll need to write your own Slack integration without using AWS Chatbot.