Skip to main content
This lesson shows how to build a simple event-driven integration that routes EC2 instance state-change events to both an Amazon SNS topic and an AWS Lambda function using Amazon EventBridge. The overall flow:
  • EC2 emits an instance state-change event (for example: stopped)
  • EventBridge receives the event on the default event bus and evaluates it against a rule (an event pattern)
  • When the pattern matches, EventBridge forwards the event to configured targets (SNS and Lambda)
What you’ll configure:
  • An SNS topic to receive notifications
  • A Lambda function to process the event programmatically
  • An EventBridge rule with an event pattern that matches EC2 stopped events and targets both SNS and Lambda
  • A subscription to confirm delivery to an email endpoint
Prerequisites:

Create an SNS topic

Open the Amazon SNS console and go to Topics. Create a Standard topic (FIFO is unnecessary for this demo).
A screenshot of the Amazon Web Services SNS console showing the Topics page with no topics listed and a prominent "Create topic" button. The toolbar also shows search and action buttons like Edit, Delete, and Publish message.
Steps:
  1. Choose Create topic → Standard.
  2. Enter a name, for example ec2-alerts.
  3. Leave other defaults unless you have specific compliance or encryption needs.
  4. Create the topic and note the Topic ARN displayed on the topic details page.
Screenshot of the AWS Management Console on the Amazon SNS "Create topic" page. The Details section shows topic type options (FIFO and Standard) with Standard selected and a Name field below.
Once created you will see the topic details including the ARN, owner, and message size limits.
A screenshot of the AWS Management Console showing the Amazon SNS Topics page for a topic named "ec2-alerts." The topic details (ARN, owner, max message size) are visible with buttons to Edit, Delete, and Publish message.

Create the Lambda function

Open the AWS Lambda console and create a function. For this demo we author the function from scratch and choose Python as the runtime.
A screenshot of the AWS Lambda console showing the "Functions (0)" page with an empty functions table and a prominent "Create function" button. The header shows the account/region and a refresh timestamp.
Steps:
  1. Choose Create function → Author from scratch.
  2. Enter a function name (for example, ec2-event-handler) and select Python as the runtime.
  3. Leave permissions and execution role as default for the demo, or attach a role with minimal invoke/CloudWatch permissions in production.
  4. Create the function. The function will be invoked by EventBridge when the rule matches.
A screenshot of the AWS Lambda "Create function" console showing options to "Author from scratch," "Use a blueprint," or use a "Container image," with a Basic information section to enter a function name. The browser tab bar and AWS navigation header are visible across the top.
After creation, Lambda provisions the function and you can add simple logging or processing. For this demo, the Lambda need only accept the event and log it to CloudWatch.
Screenshot of the AWS Management Console on the AWS Lambda "Create function" page, showing custom settings with toggles for "Durable execution" and "EC2 capacity provider." An orange "Create function" button is visible at the bottom right.

Create an EventBridge rule

Open Amazon EventBridge and select Event buses → default. The default event bus receives service events from this AWS account (including EC2 state-change events). You can verify the default bus URL and ARN in the console. Example:
Create a new rule:
  1. Choose Create rule.
  2. Use the Advanced Builder (not the visual Enhanced Builder) to compose the JSON event pattern precisely.
  3. Name the rule (for example, ec2-state-change) and set Event bus to default.
  4. Keep the rule enabled so it evaluates matching events immediately.
A screenshot of the AWS Management Console showing the Amazon EventBridge "Create rule" page with the rule name and description set to "ec2-state-change" and the default event bus selected. The page shows the enable-rule toggle turned on and a "Next" button at the bottom.

Define the event pattern

Select AWS services as the event source and choose EC2. For this demo we want to match EC2 instance state-change notifications where the instance state is stopped. Use this event pattern:
This JSON instructs EventBridge to match EC2 events whose detail.state equals stopped.
Screenshot of the AWS Console (Amazon EventBridge) rule creation page. It shows a dropdown of EC2 instance states (pending, running, shutting-down, terminated, stopping, stopped) with "stopped" highlighted and buttons like "Copy" and "Edit pattern."

Add targets

Add two targets to demonstrate multiple destinations for a single rule: When configuring targets, you can provide an input transformer or pass the full matched event to each target. For this demo the default payload (the full event) is fine. Proceed to review and create the rule.

Subscribe to the SNS topic

To receive notifications, subscribe an endpoint to the ec2-alerts topic. Common choices include Email, HTTP(S), SQS, or Lambda. Example subscription details:
  • Topic ARN:
  • Protocol: Email
  • Endpoint: your.email@example.com (use an address you can access)
A screenshot of the AWS Management Console showing Amazon SNS topic details for a topic named "ec2-alerts" with its ARN, type, owner and action buttons (Edit, Delete, Publish message). The left sidebar shows SNS navigation items like Dashboard, Topics and Subscriptions.
After creating the subscription, you must confirm it from the email inbox before Amazon SNS will deliver notifications to that address. Look for a confirmation email from no-reply@sns.amazonaws.com and click the contained link.
You’ll receive a confirmation email that contains a subscription confirmation link. Confirm the subscription to change the status from “Pending confirmation” to “Confirmed.”
A Gmail window showing an open message titled "AWS Notification - Subscription Confirmation" from no-reply@sns.amazonaws.com with a confirmation link for an AWS SNS topic. The left sidebar shows mail folders like Inbox, Starred, and Drafts.

Generate an EC2 state-change event

Ensure you have an EC2 instance running in the same AWS account and region as the EventBridge rule:
  1. From the EC2 console, stop a running instance (Actions → Instance State → Stop).
  2. When the instance transitions to stopped, EC2 emits an instance state-change event to the account’s default EventBridge bus.
  3. EventBridge evaluates the incoming event against the rule’s event pattern. If detail.state is stopped, the rule matches.
EventBridge will then forward the matched event to both the SNS topic and the Lambda function.

Verify Lambda invocation and SNS delivery

  • Lambda: Open the ec2-event-handler function, select the Monitor tab (CloudWatch metrics), and verify that one or more invocations were recorded at the time you stopped the instance.
A screenshot of the AWS Management Console showing the Lambda function "ec2-event-handler" monitoring dashboard. The image displays metric charts (invocations, duration in milliseconds, errors/success rate, throttles) with a tooltip for an invocation at 2026-09-29 16:40:00 UTC.
  • SNS / Email: Check the subscribed email inbox for the notification message from SNS. The notification contains details such as the instance ID, AWS account ID, and the event type “EC2 Instance State-change Notification” indicating the stopped state.
If the Lambda metrics show an invocation and the email is delivered, your EventBridge rule successfully matched the EC2 event and routed it to both targets.

Troubleshooting tips

  • No SNS email received:
    • Verify the subscription is Confirmed (not Pending).
    • Check the SNS topic’s delivery status and CloudWatch logs for failed deliveries.
  • Lambda not invoked:
    • Confirm the EventBridge rule has the Lambda target and that the rule is enabled.
    • Verify EventBridge has invoke permissions for the Lambda (check resource-based policy on the function).
    • Inspect CloudWatch Logs for any Lambda errors.
  • Event not matching:
    • Confirm the detail-type, source, and detail.state in the event pattern match the actual event payload. Use EventBridge’s Event Archive or CloudWatch Events history to inspect event samples.

Summary

This walkthrough demonstrates an event-driven integration that:
  • Captures EC2 instance state-change events on the default EventBridge bus
  • Uses an EventBridge rule with an event pattern to filter for stopped instances
  • Routes matched events to multiple targets (SNS and Lambda)
  • Delivers notifications via SNS subscriptions (email in this demo) and invokes Lambda for programmatic processing
EventBridge enables flexible, decoupled workflows that trigger notifications and custom processing in response to AWS service events. For more examples and best practices, see the Amazon EventBridge developer guide.

Watch Video

Practice Lab