Creating a CloudWatch Log custom integration for Slack using AWS Lambda
Tags: slack, aws, lambda, cloudwatch
Published: 2021-02-22 06:30:58

> 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](https://slack.com/intl/en-mx/help/articles/115005265063-Incoming-webhooks-for-Slack)

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](https://console.aws.amazon.com/)
2. Create a new [IAM role](https://console.aws.amazon.com/iam/home#/roles), it will be used to access to CloudWatchLogs events.
    ![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thqewndyz8pvz5lbk1uq.png)

    ![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qj1upp9he3uqb83u3hwn.png)

    ![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/suwo3vzsn5h9jadg57x6.png)

3. Now you need to create 2 log groups in [CloudWatch](https://us-east-2.console.aws.amazon.com/cloudwatch/home), `/aws/lambda/Log2Slack` and `production-api` or whatever name you want to test. Note: _Retetion setting is used to assign a log lifetime_.

    ![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3duy85qyyseei7dv9y9v.png)

    ![5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efofzmqxzogntausik0w.png)

4. In order to create the lambda function you need to go to [Lambda](https://us-east-2.console.aws.amazon.com/lambda/home) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wh3ngx789wfri5fdr4w1.png)

    ![7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwcpec1bh88c1qh0yace.png)

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

    ![8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gd8igh20fu4p9qq3yhop.png)

    ![9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukkiz3xkretpqachb1au.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44uy7n9yephcz6tcvoc7.png)
    - Select a channel to post the alerts.
    - Click on `Add Incoming Webhook integration`.
    - Copy the value of `Webhook URL`.
    
    ![11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d39141bm8ftsgc2hnsoh.png)

    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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef68rvh0ibjlijlyg5d0.png)

    {% gist https://gist.github.com/AgusRdz/ff3631e781d308228e470c6946a91990 %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xx6rfu8qpwtbyqlgnqj.png)
    ![16](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkrlmqa8i6qy9s2xjtgn.png)
    ![17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cse500j7w9l9v5sd4di9.png)

This is the final result after a Cloudwatch log is sent to Slack 🎉
    ![18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9ep7ky3zcqpyr27716n.png)

Slack resources:

- [Block Kit](https://app.slack.com/block-kit-builder)
- [Mentions](https://api.slack.com/reference/surfaces/formatting#mentioning-users)
- [Special Mentions](https://api.slack.com/reference/surfaces/formatting#special-mentions)

### 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.