0

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],
      }
    );
  }
}
Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
  • Not in CDK most likely. This is something that will change based on the moment it runs, so you will need some sort of custom resource to figure out what the log group that triggered the alrarm and generate the message. (note, due to the way Log Groups work you might be better served by sending a link to an Insight Query targeting the time period the alarm was triggered from) – lynkfox Jun 10 '22 at 15:21

1 Answers1

0

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.

gshpychka
  • 8,523
  • 1
  • 11
  • 31