Skip to main content
This walkthrough shows how to create an Amazon EventBridge rule that forwards EC2 instance state-change events to two targets: an Amazon SNS topic and a Lambda function. You will:
  • Create an SNS topic (ec2-alerts) and subscribe an endpoint.
  • Create a Lambda function (ec2-event-handler) to receive EventBridge events.
  • Build an EventBridge rule (Advanced Builder) that matches EC2 stopped events.
  • Add both SNS and Lambda as rule targets.
  • Generate a test event by stopping an EC2 instance and verify delivery.
Prerequisites: an AWS account with permissions to create SNS topics, Lambda functions, and EventBridge rules in the same region as your EC2 instance. Quick resource summary Useful references:

1 — Create the SNS topic

Open the Amazon SNS console and create a topic to receive published events.
A screenshot of the AWS Management Console on the Amazon SNS "Create topic" page showing the topic type options (FIFO vs. Standard) with Standard selected. The form below includes a topic name field ("MyTopic") and the browser/console navigation bar at the top.
Steps:
  • Console: Topics → Create topic.
  • Choose Standard (FIFO ordering is not required for this demo).
  • Name the topic (example: ec2-alerts).
  • Keep other settings default unless you require encryption, delivery retries, or custom access control.
After creation, the topic details page shows its ARN and configuration.
A screenshot of the AWS Management Console showing an Amazon SNS topic named "ec2-alerts," with its details (ARN, owner, max message size) and action buttons to Edit, Delete, or Publish message.

2 — Create the Lambda function

Create a basic Lambda function that will be invoked by EventBridge.
A screenshot of the AWS Lambda "Create function" console showing options to create a function (with "Author from scratch" selected) and the Basic information section for naming the function. The browser UI and AWS account/org are visible across the top.
Steps:
  • Lambda console → Create function → Author from scratch.
  • Function name: ec2-event-handler (or your preferred name).
  • Runtime: Python 3.9+ (choose the supported runtime available in your account).
  • Permissions: leave defaults (Lambda creates an execution role automatically).
  • Create the function. You can add processing code later; for testing, the default handler that logs the event is sufficient.

3 — Create the EventBridge rule (Advanced Builder)

EventBridge receives AWS service events on the default event bus. Create a rule to filter EC2 state-change events and route them to SNS and Lambda.
A screenshot of the AWS Management Console open to Amazon EventBridge showing the "default" event bus details. The page displays the event bus name, ARN, schema discovery status and action buttons like Edit, Send events, and Start discovery.
Steps:
  • EventBridge console → Event buses → select default.
  • Create rule → choose the Advanced Builder so you can view/edit the JSON event pattern.
Rule metadata:
  • Name: ec2-state-change
  • Event bus: default
  • Enabled: Yes (so it starts processing immediately)
A screenshot of the AWS Console on the Amazon EventBridge "Create rule" screen showing a rule named "ec2-state-change" with its description filled and the default event bus enabled. The left panel shows the rule creation steps and the bottom right has Cancel and Next buttons.
Event pattern (filter)
  • Event source: AWS events
  • Service name: EC2
  • Event type: EC2 Instance State-change Notification
  • Filter for the stopped state
Use this JSON pattern in the Advanced Builder:

4 — Configure targets: SNS and Lambda

Add both targets to the rule so a single matching event is delivered to multiple endpoints.
A screenshot of the AWS EventBridge console showing the "Targets" tab for a rule named "ec2-state-change," listing targets including an SNS topic called "ec2-alerts" and a Lambda function "ec2-event-handler" with their ARNs and roles. The page also shows navigation tabs, the Edit button, and the AWS top navigation bar.
Steps:
  • Add target #1:
    • Target type: AWS service → SNS topic
    • Select: ec2-alerts
    • Allow EventBridge to create the default role (it grants permission to publish to SNS)
  • Add target #2:
    • Target type: AWS service → Lambda function
    • Select: ec2-event-handler
    • Allow EventBridge to create/use the role for invoking Lambda
Note: EventBridge can deliver the same event to multiple targets simultaneously — useful for alerting and automated processing.

5 — Subscribe an endpoint to the SNS topic

To receive notifications, subscribe an endpoint (for example, an email address) to the SNS topic.
A screenshot of the AWS Console on the Amazon SNS "Subscriptions" page showing "Subscriptions (0)" and a "No subscriptions found" message. The page displays buttons like "Create subscription," "Edit," and "Delete."
Steps:
  • Open the ec2-alerts topic → Subscriptions → Create subscription.
  • Topic ARN: e.g., arn:aws:sns:us-east-2:635573991785:ec2-alerts
  • Protocol: Email (or Email-JSON, HTTPS, SMS, etc.)
  • Endpoint: your email address
  • Create the subscription; SNS sends a confirmation message to the endpoint.
After confirmation, the subscription state will show as Confirmed.
A screenshot of the AWS Management Console showing the Amazon SNS "Subscriptions" page with a prominent "Create subscription" button and a listed, confirmed email subscription. The left sidebar shows SNS navigation (Dashboard, Topics, Subscriptions) and the top bar displays account and region info.
Confirm subscription via the email link.
A Gmail window showing an open email titled "AWS Notification - Subscription Confirmation" from AWS Notifications with a visible "Confirm subscription" link. The left sidebar shows mailbox folders (Inbox, Starred, Sent, etc.) and reply/forward buttons are at the bottom.

6 — Generate a test event: stop an EC2 instance

Trigger a real EC2 state-change event by stopping an EC2 instance in the same account and region as your EventBridge rule. Steps:
  • Ensure you have a running EC2 instance in the same region/account.
  • Stop the EC2 instance (Actions → Instance State → Stop).
  • EC2 emits a state-change notification which is placed on the default EventBridge bus.
When the stopped event is emitted, EventBridge evaluates the event against your rule’s JSON pattern. If it matches, the rule routes the event to both the SNS topic and the Lambda function. Sample event delivered by EventBridge:

7 — Verify delivery

  • Lambda:
    • Open the ec2-event-handler function → Monitor tab.
    • View invocation metrics and CloudWatch Logs to confirm the function executed and to inspect the raw event payload.
  • SNS (email):
    • Check the subscribed email inbox for the notification message.
    • The message should contain instance ID, account, and the EC2 Instance State-change Notification details.
This confirms EventBridge published the event to SNS and invoked the Lambda function. Key workflow summary
  • EC2 emits an instance state-change event.
  • The event is sent to the EventBridge default event bus.
  • The ec2-state-change rule filters for state: stopped.
  • Matching events are routed to multiple targets: SNS (ec2-alerts) and Lambda (ec2-event-handler).
  • SNS delivers to subscribers and Lambda runs your processing code.
Make sure your Lambda’s execution role has appropriate permissions if you intend to process or publish messages from within the function. For basic invocation testing, the automatic role created by Lambda is usually sufficient. If your function needs to publish to SNS, write to other AWS services, or access resources, add those permissions explicitly to the execution role.

Watch Video

Practice Lab