11

Currently we have set up AWS Chatbot integration for Slack to receive notifications about CodePipeline - results of CodeBuilds and status of all stages of CodePipeline. I have noticed that the out of the box integration's messages aren't as descriptive as I would like.

For example: if a build fails due to a failed unit test the Chatbot will output POST_BUILD: COMMAND_EXECUTION_ERROR: Error while executing command: <command>. Reason: exit status 1 which is fine but I would much rather change that message to be something more descriptive - to tell me that a test has failed.

I figured that Chatbot is subscribed to SNS topics and once those are sent out it will post a message on Slack. Is there a way to customize the contents of the messages it sends?

RocketRacer
  • 123
  • 1
  • 8

3 Answers3

9

Official answer no, but there is a workaround I detected. You can use Cloudwatch EventBridge messages and adjust them to your likening.

Simply said

Publish following message

{
"version": "0",
"id": "13cde686-328b-6117-af20-0e5566167482",
"detail-type": "Your custom message",
"source": "aws.ecr",
"account": "123456789012",
"time": "2019-11-16T01:54:34Z",
"region": "azAZ0-9!-_/|?!.,",
"resources": [],
"detail": {}
}

and you'll see something like

AWS Chatbot custom event

And you are free to edit region, detail-type and account according to some limitations

Tom
  • 403
  • 3
  • 14
  • This no longer seems to work. What you can still do, is take an existing message format (say for "CodePipeline Stage Execution State Change"), manipulate it and send that to the SNS topic. – Hieron Feb 04 '23 at 12:07
6

UPDATE: After reading the docs further, I found out we can not modify event messages in aws chatbot.

I get an "Event received is not supported" error

Even though below approach is correct, but notification's originating service is not supported by AWS Chatbot.

Using AWS Chatbot with other AWS services

You can have a custom Event Rule like below, I choose for FAILED state you can customize whichever event you want and then subscribe a lambda to it where you can build a meaninful message and then send it to the SNS topic which has AWS Chatbot subscribed :

{
  "source": [
    "aws.codepipeline"
  ],
  "detail-type": [
    "CodePipeline Action Execution State Change"
  ],
  "detail": {
    "state": [
      "FAILED"
    ]
  }
}

I did something like this for PagerDuty calls so that on-call person gets a proper message with some more information attached to the alert.

samtoddler
  • 8,463
  • 2
  • 26
  • 21
  • Thanks, this is exactly what I needed! One step closer to what I'm trying to achieve - I'm receiving CodeBuild messages just fine, but the next step is to pull the information from within the CodeBuild itself to display what exactly has gone wrong. – RocketRacer Jan 25 '21 at 22:00
  • @RocketRacer yes inside the event you get the full message why the build failed. – samtoddler Jan 25 '21 at 22:24
  • My issue is that he event message contains incomplete information. It contains errors that are not descriptive of the issue actually is e.g. `['COMMAND_EXECUTION_ERROR: Error while executing command: dotnet test --filter Type=Unit -l:trx --collect:"XPlat Code Coverage" --settings runsettings.xml. Reason: exit status 1']` while the real reason the run has failed is a broken unit test that was run using that command in the buildspec. So I'm now trying to figure out if I can access CodeBuild report produced by this specific CodeBuild run and see what the real reason for failure is. – RocketRacer Jan 25 '21 at 22:27
  • @RocketRacer you wanna see the full log if I understand it correct, in that case you can have them logs available in s3 and get those inside your emails via the lambda. OR you can generate a one time URL via the lambda for the s3 logs and pass it in the email. – samtoddler Jan 25 '21 at 22:30
  • Created a separate question for this: https://stackoverflow.com/questions/65893406/how-to-access-aws-codebuild-reports-in-a-lambda – RocketRacer Jan 25 '21 at 22:40
  • 1
    I'm running into an issue where Chatbot says "Event received is not supported" and it doesn't publish my received message, how did you get around this? – RocketRacer Jan 26 '21 at 13:13
  • I'm also hitting the `Event received is not supported` wall right now. Is there any workaround or solution to this? – nonbeing Mar 27 '21 at 06:17
  • @nonbeing Seems like we can not modify the aws chatbot messages due to the limitation of the supported services by chatbot, even though the approach is correct. As I can not delete an accepted post, so I updated the answer. – samtoddler Mar 27 '21 at 09:05
  • 1
    Got it. Thanks for the update. I don't like how AWS has locked-down AWS Chatbot to specific services, and uncharacteristically there's no API, no source, nothing. There's no way to tinker with it. It's a total black-box. – nonbeing Mar 28 '21 at 04:26
  • 2
    @nonbeing yeah imo it's quite silly. I ended up going a completely different route - I just talk to a webhook-enabled Slackbot (with permissions to post in a specific channel) instead. I make a payload in my AWS Lambda and simply do a POST call to the bot's webhook. Works like a charm – RocketRacer Apr 01 '21 at 16:58
1

This was too long as a reply for @Tom's answer. But I'd like to add that this works for me (have to keep a sensible region name), and you can also include a lot of slack formatting:

{
"version": "0",
"id": "12345678-1234-aaaa-aaaa-123456789012",
"detail-type": "The heading for your message",
"source": "aws.events",
"account": "123456789012",
"time": "2023-06-02T00:58:25Z",
"region": "ap-southeast-2",
"resources": [
    "Some random text \nwith newline\n>indented too\nin *bold* and _italic_.",
    "A second dot point in the list.",
    "`code block`",
    "And a big code block:```with some\nmultiline\ncode.```"
],
"detail": {}
}
JoeAC
  • 852
  • 1
  • 8
  • 13