Sub topic 15
Reacting to alarm state changes with EventBridge
This chapter assumes you can create a metric alarm, know what an SNS alarm action does, and have used the AWS CLI. It covers how to react to alarm state changes with Amazon EventBridge when an SNS notification is not enough.
Why use EventBridge for alarms
An alarm action can notify an SNS topic. That works well for sending email or chat messages. Sometimes you want a program to react instead: open a ticket, start a runbook, or invoke a function.
CloudWatch publishes an event to the default EventBridge event bus every time an alarm changes state. An EventBridge rule matches those events and sends them to a target such as a Lambda function, an SQS queue, or an SSM Automation document.
The alarm event
The event has the source aws.cloudwatch and the detail type CloudWatch Alarm State Change. Its detail field holds the alarm name, the previous state, and the new state.
A trimmed example:
{
"source": "aws.cloudwatch",
"detail-type": "CloudWatch Alarm State Change",
"detail": {
"alarmName": "high-cpu-web",
"state": { "value": "ALARM" },
"previousState": { "value": "OK" }
}
}Create a rule
This rule matches only the moment high-cpu-web enters the ALARM state.
aws events put-rule \
--name high-cpu-web-alarm \
--event-pattern '{
"source": ["aws.cloudwatch"],
"detail-type": ["CloudWatch Alarm State Change"],
"detail": {
"alarmName": ["high-cpu-web"],
"state": { "value": ["ALARM"] }
}
}'Then attach a target. Replace the function ARN with your own:
aws events put-targets \
--rule high-cpu-web-alarm \
--targets 'Id=remediate,Arn=arn:aws:lambda:us-east-1:111122223333:function:remediate-web'Grant permission to the target
A rule can only invoke a Lambda function if the function's resource policy allows it:
aws lambda add-permission \
--function-name remediate-web \
--statement-id events-high-cpu \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn arn:aws:events:us-east-1:111122223333:rule/high-cpu-web-alarmCommon errors
- The rule never fires. The rule and the alarm must be in the same account and Region. Also check that the event pattern matches exactly. Values are arrays, and matching is case-sensitive.
- The target is not invoked. The target is missing its permission. A Lambda function needs the resource policy above. Other targets need an IAM role on the target.
- The rule fires on every transition. If you leave out the
statefilter, you also get events forOKandINSUFFICIENT_DATA. Filter onstate.valueto keep only the transitions you want. - Composite alarms. Composite alarms also publish state change events. Match on the composite alarm name to react once, not once per child alarm.
Exercise
You want a Lambda function to run only when the alarm high-cpu-web returns to normal. Which part of the event pattern changes from the example in this chapter, and why might the rule still not invoke the function after you fix it?
Show answer
Change state.value from ["ALARM"] to ["OK"]. The rule can still fail to invoke the function if the function has no resource policy that allows events.amazonaws.com to invoke it (added with aws lambda add-permission), or if the rule is in a different Region from the alarm.