AWS CloudWatch
AWS CloudWatch allows you to set up alarms to monitor metrics and send notifications when certain thresholds are met. You can integrate CloudWatch alarms with a webhook to receive notifications in an external system such as a chat application or an incident management system.
To set up a webhook integration for CloudWatch alarms, follow these steps:
- Create a webhook in your external system that can receive the CloudWatch alarm notifications. The webhook should be able to parse and process the payload sent by CloudWatch. Make sure to take note of the webhook URL.
- In the AWS Management Console, navigate to the CloudWatch service and select “Alarms” from the left-hand menu.
- Create a new alarm or select an existing alarm that you want to integrate with the webhook.
- Click on the “Actions” dropdown and select “Add notification”.
- Select “Lambda function” as the notification target.
- Click on the “Configure details” button and enter a name and description for the notification target.
- In the “Lambda function” section, select “Create a new Lambda function” and enter a name and description for the function.
- Choose a runtime for the function and select “Create a new role with basic Lambda permissions” as the execution role.
- Under the “Function code” section, select “Edit code inline”.
- Paste the following code into the editor, replacing the webhook URL with the URL of your webhook:
var https = require(‘https’);
exports.handler = function(event, context) {
console.log(‘Received CloudWatch notification:’, JSON.stringify(event, null, 2));
var postData = JSON.stringify(event);
var options = {
hostname: ‘your-webhook-url.com’,
port: 443,
path: ‘/webhook-endpoint’,
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘Content-Length’: postData.length
}
};
var req = https.request(options, function(res) {
console.log(‘Webhook response status code:’, res.statusCode);
context.succeed();
});
req.on(‘error’, function(e) {
console.log(‘Error sending webhook:’, e);
context.fail();
});
req.write(postData);
req.end();
};
- Click on the “Save” button to save the function code.
- Click on the “Create function” button to create the function and associate it with the CloudWatch alarm.
Once the integration is set up, CloudWatch will send notifications to the Lambda function whenever the alarm threshold is met. The Lambda function will then forward the notification payload to the webhook URL you specified, allowing you to receive notifications in your external system.
Registration Procedure:
- Open AWS console and search for sns (simple notification service)
2. Under topics click on Create Topic
3. Select Standard Topic type and give the unique topic name and display name and submit the topic by giving proper permissions
4. In view topic page click on “create subscription”
5. Copy the integration url and paste at create subscription by choosing the https protocol
6. To configure the above topic to send notification when aws cloudwatch alarms meet the threshold, please go to the alarms section in aws cloudwatch
7. Click on any existing alarm, under actions click on Edit
8. Update the metrics and conditions and click on Next
9. Under configure actions, select an existing SNS topic and choose the created topic at SNS and click on Update Alarm
10. To configure the above topic to send notification when aws cloudwatch alarms meet the threshold, please go to the alarms section in aws cloudwatch
11. Click on any existing alarm, under actions click on Edit
12. Update the metrics and conditions and click on Next
13. Under configure actions, select an existing SNS topic and choose the created topic at SNS and click on Update Alarm