Reputation: 5767
I know how to create an alarm using AWS CDK but I don't know how to add a notification to that alarm. How do I do that?
This is my code for the alarm:
/**
* Alarm for CPU above 75%
*/
const metric = cluster.metricCPUUtilization();
new cloudwatch.Alarm(this, `CPU above 75% alarm` , {
metric: metric,
threshold: 75,
evaluationPeriods: 3,
datapointsToAlarm: 2,
});
This is what it looks like in the console:
Upvotes: 7
Views: 8646
Reputation: 41648
What you need is an SNS topic and a subscription.
First import the required packages:
import * as sns from "@aws-cdk/aws-sns";
import * as subscriptions from "@aws-cdk/aws-sns-subscriptions";
Then create a topic with an email subscription:
const topic = new sns.Topic(scope, 'Alarm topic', {
displayName: envSpecificName(props.displayName)
});
topic.addSubscription(
new subscriptions.EmailSubscription(email)
)
Finally register the topic as an alarm action:
import * as actions from "@aws-cdk/aws-cloudwatch-actions";
...
const metric = cluster.metricCPUUtilization();
const alarm = new cloudwatch.Alarm(this, `CPU above 75% alarm` , {
metric: metric,
});
alarm.addAlarmAction(new actions.SnsAction(topic));
You can find more examples in documentation
Upvotes: 21