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
- First you need to access AWS Management Console
-
Create a new IAM role, it will be used to access to CloudWatchLogs events.
-
Now you need to create 2 log groups in CloudWatch,
/aws/lambda/Log2Slack
andproduction-api
or whatever name you want to test. Note: Retetion setting is used to assign a log lifetime. -
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.
-
Add a trigger to listen to the CloudWatch events.
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 onIncoming Webhooks
-
- Select a channel to post the alerts.
- Click on
Add Incoming Webhook integration
. - Copy the value of
Webhook URL
.
Note: In Integration Settings you can assign a name and a logo to provide a nicer look
-
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. Click on the
Deploy
button to complete the function.-
Testing, testing, and testing. There are two ways to test the function:
- Creating a
Log Stream
directly into theLog Group
, then enter to the log stream and triggering a log event manually.
- Creating a
This is the final result after a Cloudwatch log is sent to Slack 🎉
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)
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)
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.
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.
Excellent observation!!!