How can I receive notifications for scheduled events for my EC2 instance using EventBridge?

Last updated: 2022-01-19

I want to receive notifications from Amazon EventBridge when there's a scheduled event for my Amazon Elastic Compute Cloud (Amazon EC2) instance. How can I do that?

Short description

You can create an EventBridge rule to trigger on a scheduled event for your EC2 instance. Then, you can configure Amazon Simple Notification Service (Amazon SNS) notifications for events that match your event pattern. You can also configure further processing through AWS Lambda.

Resolution

Important: Before you begin, be sure that you have an SNS topic for notifications. If you don't have one, create a new SNS topic and subscribe to it.

Create an EventBridge rule that triggers on an event using the EventBridge console:

1.    Assign a Name and optionally a Description to the rule.

2.    For Define Pattern, choose Event pattern.

3.    For Event matching pattern, choose Pre-defined by service.

4.    For Service provider, choose AWS.

5.    For Service Name, choose Health.

6.    For Event Type, choose Specific Health events.

7.    Choose Specific service(s), and then choose EC2.

8.    Choose Specific event type category(s), and then choose scheduledChange.

9.    (Optional) To create a more specific rule, include a Specific event type code(s) or Specific resource(s). By adding one of these options to the rule, notifications are sent for specific events or instances.

These settings create the following event pattern:

Note: You can also create your event pattern by copying and pasting the following code as your rule. After you choose Create Rule, choose Edit in the Event Pattern preview section. Insert the code below, choose Save, and then proceed to create an SNS target.

{
  "source": [
    "aws.health"
  ],
  "detail-type": [
    "AWS Health Event"
  ],
  "detail": {
    "service": [
      "EC2"
    ],
    "eventTypeCategory": [
      "scheduledChange"
    ]
  }
}

10.    Select your SNS target:
Choose Add Target.
For the target, choose SNS topic.
For Topic, choose the SNS topic that you want to use for notifications.

The following example is an EventBridge event that will be forwarded to the target:

{
  "version": "0",
  "id": "7fb65329-1628-4cf3-a740-95fg457h1402",
  "detail-type": "AWS Health Event",
  "source": "aws.health",
  "account": "123456789101",
  "time": "2016-06-05T06:27:57Z",
  "region": "us-east-1",
  "resources": ["i-12345678"],
  "detail": {
    "eventArn": "arn:aws:health:region::event/id",
    "service": "EC2",
    "eventTypeCode": "AWS_EC2_DEDICATED_HOST_NETWORK_MAINTENANCE_SCHEDULED",
    "eventTypeCategory": "scheduledChange",
    "startTime": "Sat, 05 Jun 2016 15:10:09 GMT",
    "eventDescription": [{
      "language": "en_US",
      "latestDescription": "A description of the event will be provided here"
    }],
    "affectedEntities": [{
      "entityValue": "i-12345678",
      "tags": {
        "stage": "prod",
        "app": "my-app"
      }
    }]
  }
}

If your use case requires delivering specific fields to the target, then attach an Input Transformer under Configure input. Or, attach a Lambda function as a target to run designated processing.