Sub topic 15
Reacting to alarm state changes with EventBridge
This chapter assumes you can create a metric alarm and wire it to an SNS topic, as covered in the earlier alarm chapters. You should also know what an IAM role and a Lambda function are, and be comfortable running AWS CLI commands.
Why use EventBridge when alarms already have actions
An alarm action runs when the alarm enters a state. The set of actions is small: publish to SNS, run an EC2 action such as stop or reboot, run an Auto Scaling policy, or create a Systems Manager OpsItem or incident. Each action is attached to one alarm, so you configure it alarm by alarm.
Amazon EventBridge works from the other side. Every time an alarm changes state, CloudWatch also sends an event to the default event bus in the same account and Region. You write a rule that matches events by shape, and the rule sends matching events to one or more targets. A single rule can cover many alarms, and the targets can be Lambda functions, Step Functions state machines, SQS queues, SSM Automation runbooks, another event bus, and many other services.
| Need | Better fit |
|---|---|
| Email or page one team from one alarm | Alarm action to SNS |
| Reboot or recover one EC2 instance | Alarm EC2 action |
| Run custom code for every alarm whose name starts with `prod-` | EventBridge rule |
| Start a runbook only on a particular transition, such as `OK` to `ALARM` | EventBridge rule |
| Forward alarm events to a central account | EventBridge rule with a bus target |
You can use both mechanisms on the same alarm. They do not interfere with each other.
What the event looks like
CloudWatch publishes the event with source aws.cloudwatch and detail type CloudWatch Alarm State Change. The detail object carries the alarm's name, its new state, its previous state, and its configuration. This abbreviated example shows the fields you will match on most often:
{
"version": "0",
"source": "aws.cloudwatch",
"detail-type": "CloudWatch Alarm State Change",
"account": "111122223333",
"region": "us-east-1",
"resources": ["arn:aws:cloudwatch:us-east-1:111122223333:alarm:high-cpu-web"],
"detail": {
"alarmName": "high-cpu-web",
"state": {
"value": "ALARM",
"reason": "Threshold Crossed: 2 out of the last 3 datapoints were greater than or equal to the threshold (80.0).",
"timestamp": "2026-11-26T19:10:00.000+0000"
},
"previousState": { "value": "OK" }
}
}The state.value and previousState.value fields hold one of OK, ALARM, or INSUFFICIENT_DATA. Composite alarms emit the same event type, so one rule can handle both kinds.
The flow
Building the rule
The rule below matches any alarm whose name begins with high-cpu- and that has just entered ALARM. Save it as pattern.json:
{
"source": ["aws.cloudwatch"],
"detail-type": ["CloudWatch Alarm State Change"],
"detail": {
"alarmName": [{ "prefix": "high-cpu-" }],
"state": { "value": ["ALARM"] }
}
}An event pattern is an AND across fields and an OR across the values listed for one field. Here the event must have the right source, the right detail type, a matching name, and the state ALARM.
Create the rule and add a Lambda function as its target:
aws events put-rule \
--name alarm-in-alarm \
--event-pattern file://pattern.json
aws events put-targets \
--rule alarm-in-alarm \
--targets "Id=handler,Arn=arn:aws:lambda:us-east-1:111122223333:function:alarm-handler"The target needs permission to be invoked. For Lambda, that is a resource-based policy on the function that trusts EventBridge, scoped to this rule:
aws lambda add-permission \
--function-name alarm-handler \
--statement-id eventbridge-alarm \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn arn:aws:events:us-east-1:111122223333:rule/alarm-in-alarmOther target types use different permission models. SQS queues and SNS topics need a resource policy that allows events.amazonaws.com. Targets such as Step Functions and SSM Automation need an IAM role that EventBridge assumes, which you pass with RoleArn in the target definition.
A minimal handler
This Python handler reads the event, extracts the useful fields, and logs one line. Replace the logging with whatever remediation you need.
import json
def handler(event, context):
detail = event["detail"]
alarm = detail["alarmName"]
new_state = detail["state"]["value"]
old_state = detail["previousState"]["value"]
reason = detail["state"]["reason"]
print(json.dumps({
"alarm": alarm,
"transition": f"{old_state} -> {new_state}",
"reason": reason,
"region": event["region"],
}))
return {"handled": alarm}Testing without waiting for a real incident
Force a state with set-alarm-state. The call triggers the alarm's actions and sends the EventBridge event, so use an alarm that is safe to fire.
aws cloudwatch set-alarm-state \
--alarm-name high-cpu-web \
--state-value ALARM \
--state-reason "Testing EventBridge rule"CloudWatch returns the alarm to its real state at its next evaluation, which produces a second event for the move back to OK. Check the function's log group in CloudWatch Logs to confirm the handler ran.
Common mistakes
- Rule in the wrong Region. Alarm events go to the default bus of the alarm's own Region. A rule in
us-east-1never sees events from an alarm ineu-west-1. Create the rule where the alarm lives, or forward events to a central bus. - Matching on the wrong field. The new state is
detail.state.value. A pattern ondetail.previousState.valuematches the old state. Mixing them up produces a rule that fires on recoveries instead of failures. - Missing target permission. The rule shows as enabled and the pattern matches, but the target is never invoked. Check the
FailedInvocationsmetric for the rule in theAWS/Eventsnamespace, and review the Lambda resource policy or the role. - Reacting to every flap. An alarm that moves between
OKandALARMquickly will invoke your target each time. Usedatapoints-to-alarmto require several breaching periods, or put a composite alarm in front so the rule watches the quieter signal. - Ignoring
INSUFFICIENT_DATA. A pattern that lists onlyALARMwill miss alarms that lose their data source. If silence is dangerous for your workload, addINSUFFICIENT_DATAto the list, or set--treat-missing-data breachingon the alarm. - Assuming exactly-once delivery. EventBridge delivers at least once and retries failed targets for a limited time. Duplicate invocations can happen, so a remediation should be safe to run twice.
Where this fits
Use alarm actions for simple, direct responses and SNS fan-out to people. Use EventBridge when the response depends on logic, spans many alarms, or must leave the alarm's own account. The next chapters build on this with Synthetics canaries, whose failures raise ordinary alarms and therefore flow through this same path.
Exercise
You created a rule in us-east-1 with the pattern source "aws.cloudwatch", detail-type "CloudWatch Alarm State Change", and detail.state.value ["ALARM"]. A Lambda target is attached, and the permission is correct. An alarm named "api-latency" in eu-west-1 enters ALARM, but the function never runs. Give the likely cause and two ways to fix it.
Show answer
The alarm's event goes to the default bus in eu-west-1, and the rule exists only in us-east-1, so it never sees the event. Fix it by creating the same rule (and a Lambda target or a bus target) in eu-west-1, or by creating a rule in eu-west-1 that forwards matching events to the default bus in us-east-1, with a resource policy on that bus allowing the forwarding.
Exercise
A rule matches alarms with detail.state.value ["ALARM"]. An alarm enters ALARM at 10:00 and stays there until 14:00. How many events does the rule receive for that period, and what does this mean for handler design?
Show answer
One event, sent at the 10:00 transition. No further events are sent while the state stays the same. The handler must not assume it will be called again, and it should be idempotent. If it needs to know whether the alarm is still active later, it should query the alarm with describe-alarms.