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)
![simple notification service](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS1.png)
2. Under topics click on Create Topic
![Amozon SNS](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS2.png)
3. Select Standard Topic type and give the unique topic name and display name and submit the topic by giving proper permissions
![Standard](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS3-1.png)
4. In view topic page click on “create subscription”
![create subscription](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS4.png)
5. Copy the integration url and paste at create subscription by choosing the https protocol
![create subscription details](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS5.png)
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
![use the default delivery policy](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS6.png)
7. Click on any existing alarm, under actions click on Edit
![maximum delay retries - 0](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS7-1.png)
8. Update the metrics and conditions and click on Next
![request confirmation](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS8-1.png)
9. Under configure actions, select an existing SNS topic and choose the created topic at SNS and click on Update Alarm
![show recent payload - AWS CloudWatch](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS9.png)
![the file does not appear to have any style information](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS92.png)
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
![alarms(2)](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS10.png)
11. Click on any existing alarm, under actions click on Edit
![select edit from the actions dropdown menu](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS11..png)
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
![configure actions](https://docs.zapoj.com/wp-content/uploads/2023/04/AWS13-1.png)