AWS Chatbot is ChatOps offering from AWS to monitor, troubleshoot, and operate your AWS environments natively from within your chat channels ( Teams, Slack channels etc)
Until now, there was a limitation that notifications from aws chatbot could not be customised, additional information in the notifications
But with this update, the aws chatbot just got new powers !!!
Outcome of the Blog
- By the end of this blog, you will know:
- How to send custom notifications using chatbot using lambda.
- Why this update will help users to understand and respond quickly to events.
About this Update [ 12/09/2023 ]
- This update will help users to understand more insights about the notification and which will help them to respond quicker.
- You can add specific details, tag team members, and include remediation steps. It's upto your creativity now that how insightful and information you can make your custom notifications.
- There are no additional costs to use custom notifications.
Prerequisites for sending custom notifications using chatbot?
- Custom notifications must use the following event format as mentioned in the docs.
- Take a note about the parameters which are required and which are optional in the given table in Parameters table.
{
"version": String,
"source": String,
"id": String,
"content": {
"textType": String,
"title": String,
"description": String,
"nextSteps": [ String, String, ... ],
"keywords": [ String, String, ... ]
},
"metadata": {
"threadId": String,
"summary": String,
"eventType": String,
"relatedResources": [ String, String, ... ],
"additionalContext" : {
"customerProvidedKey1": String,
"customerProvidedKey2": String
...
}
}
}
Use client specific syntax: If messages are being sent to slack then slack markdown format should be used within custom notification and vice-versa for Teams.
-
From the docs
You can add chat platform compatible emojis in your custom notifications. You can also tag team members using @mentions in your custom notifications for Slack. Tagged team members are notified when the custom notification is delivered to the chat channel. To tag a team member in Slack, use their user ID. For example, @userID. Tagging team members in Microsoft Teams isn't currently supported.
Architecture Pattern for using custom Notifications
- There will be a source which will emit the events with all the details.
- Either source will generate event (using Lambda) with all the custom information or use EventBridge Input Transformers to add custom information to the original event.
- Final notifications will be sent to SNS topic.
- Chatbot will continuously monitor that sns topic and send notification to slack channel.
Let's create a custom notification using aws Lambda.
- As we saw in architecture pattern we need 3 things to implement and test out custom notification: AWS Lambda, sns topic and chatbot configured for the slack channel
Note: For visual simplicity and understanding, I will be using AWS console for this blog but the same thing can be achieved using AWS CDK.
Create SNS topic
- Create SNS topic and note its arn.
Create a Lambda
We will create a AWS Lambda Function with nodejs runtime and we will expose lambda using function Urls
Code for Lambda
import {
EventBridgeClient,
PutEventsCommand,
} from "@aws-sdk/client-eventbridge"
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
const snsClient = new SNSClient({ region: process.env.AWS_REGION });
const snsTopicARN = process.env.SNS_TOPIC_ARN
export async function handler() {
await publishNotificationToSlack()
return {
body: JSON.stringify({ message: 'Successful lambda invocation' }),
statusCode: 200,
};
}
export const publishNotificationToSlack = async (
) => {
const slackFormatEvent = {
"version": "1.0",
"source": "custom",
"content": {
"description": ":fire: Your <https://www.linkedin.com/posts/jatinmehrotra_custom-notifications-are-now-available-for-activity-7107541447716814848-dAAv?utm_source=share&utm_medium=member_desktop|post> is gaining traction!!! :fire:",
"title": ":linkedin: Check Linkedin <@YOURSLACKID|YOURNAME>"
}
}
const response = await snsClient.send(
new PublishCommand({
Message: JSON.stringify(slackFormatEvent),
TopicArn: snsTopicARN,
})
);
};
- I have attached permissions of full access sns to lambda role so that lambda can send notifications to sns topic directly. From a security point of view you should limit access to only required SNS topic.
- Add environment variable to lambda configuration as
SNS_TOPIC_ARN: arn of the SNS topic
noted earlier
Configure AWS Chatbot client and Slack Channel
- In order to set up AWS chatbot client for Slack and slack channel, follow the steps mentioned here in docs.
- Note: While configuring the Slack channel, add the SNS topic created earlier.
Test out the Lambda function
- Visit the function URL of the lambda function
From the DevOps perspective
- This update enables DevOps teams to improve their observability.
- Allows the team to understand more insights about the notifications as it allows to define and add more information to your notifications.
- With the support of custom notifications and native client support like slack mentions, it definitely allows the team to respond even more quickly at the time of critical events.
Connect with me over Linkedin or Twitter and share your thoughts about this blog.
Top comments (0)