DEV Community

Cover image for Creating a CloudWatch Log custom integration for Slack using AWS Lambda
Agustín Rodríguez
Agustín Rodríguez

Posted on

Creating a CloudWatch Log custom integration for Slack using AWS Lambda

Why didn't the 🍊 orange win the race? It ran out of juice.

There exist Slack integrations for everything but sometimes you need something more specific so this time I'm going to show you a way to create your own customizable integration to be notified when your application logs something in CloudWatch.

A custom integration, what is it?

Incoming webhooks are a simple way to share information from external sources with your workspace. Slack Help Center

The API to create an integration is very complete, of course, it depends on the things you want to create but it covering enough to solve all needs. On other hand, is a way to have total control of your information and the format used to display it in your Slack workspace.

 What is a Lambda function?

Basically, it is a code snippet that only runs on demand and the price is cheaper than other Slack integrations that you have to pay even if they are not used. Using this function you can reduce your costs a bit by eliminating unnecessary integrations since if you only need to send error alerts, the use of this function should be very little because of course, we do not want our applications in production to be full of errors.

 Let's go to log

  1. First you need to access AWS Management Console
  2. Create a new IAM role, it will be used to access to CloudWatchLogs events.
    1

    2

    3

  3. Now you need to create 2 log groups in CloudWatch, /aws/lambda/Log2Slack and production-api or whatever name you want to test. Note: Retetion setting is used to assign a log lifetime.

    4

    5

  4. In order to create the lambda function you need to go to Lambda and click on Create button then assign a name, select the runtime language (Node.js for this example) and assign the role previously created.

    6

    7

  5. Add a trigger to listen to the CloudWatch events.

    8

    9

  6. Before adding the lambda code you need to create a custom integration using this URL https://{your-slack-workspace}.slack.com/apps/manage/custom-integrations then clicking on Incoming Webhooks

  7. Now click on Add to Slack.
    10

    • Select a channel to post the alerts.
    • Click on Add Incoming Webhook integration.
    • Copy the value of Webhook URL.

    11

    Note: In Integration Settings you can assign a name and a logo to provide a nicer look

  8. It has been a long road so far, but now you are very close to completing the integration, click on the name of your function to open the code editor and then double click on the index.js file and replace the current content with the code below.

    12

  9. Click on the Deploy button to complete the function.

  10. Testing, testing, and testing. There are two ways to test the function:

    • Creating a Log Stream directly into the Log Group, then enter to the log stream and triggering a log event manually. 15 16 17

This is the final result after a Cloudwatch log is sent to Slack 🎉
18

Slack resources:

Final thoughts

Create your own integration to log the relevant information about your applications is very easy and if you want you can customize each type of log level to show as success, info, warning or error providing an easy way to fix the issues without wasting time checking the logs directly in Cloudwatch.

Top comments (4)

Collapse
 
santoshah profile image
Santosh • Edited

Thanks for this awesome tutorial.

Is it possible to trigger this cloudwacth Logs only when the codebuild completes ? Right now the message is spamming the channel. the logs get updated everytime until the codebuild is running. How do I trigger only when codebuild completes (either failed or succeed)

Collapse
 
agusrdz profile image
Agustín Rodríguez

I think it should be doable but could be needed to review the event log and make the adjustments to only trigger it if is codebuild event and if the result is succeed.

Collapse
 
starpebble profile image
starpebble

Very nice. My feedback: Turning this on for the first may lead to surprises about where money is wasted on the cloud. Recovering from each tiny API error is a tiny expense where the sum can add up.

Collapse
 
agusrdz profile image
Agustín Rodríguez

Excellent observation!!!