Create Amazon EventBridge rules to monitor EC2 Fleet events - Amazon Elastic Compute Cloud
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Create Amazon EventBridge rules to monitor EC2 Fleet events

When a notification of a state change is emitted for an EC2 Fleet, the event for the notification is sent to Amazon EventBridge in the form of a JSON file. You can write an EventBridge rule to automate what actions to take when an event pattern matches the rule. If EventBridge detects an event pattern that matches a pattern defined in a rule, EventBridge invokes the target (or targets) specified in the rule.

The following fields form the event pattern that is defined in the rule:

"source": "aws.ec2fleet"

Identifies that the event is from EC2 Fleet.

"detail-type": "EC2 Fleet State Change"

Identifies the event type.

"detail": { "sub-type": "submitted" }

Identifies the event sub-type.

For the list of EC2 Fleet events and example event data, see EC2 Fleet event types.

Create an EventBridge rule to send a notification

The following example creates an EventBridge rule to send an email, text message, or mobile push notification every time that Amazon EC2 emits an EC2 Fleet state change notification. The signal in this example is emitted as an EC2 Fleet State Change event, which triggers the action defined by the rule.

Before creating the EventBridge rule, you must create the Amazon SNS topic for the email, text message, or mobile push notification.

To create an EventBridge rule to send a notification when an EC2 Fleet state changes

  1. Open the Amazon EventBridge console at https://console.amazonaws.cn/events/.

  2. Choose Create rule.

  3. For Define rule detail, do the following:

    1. Enter a Name for the rule, and, optionally, a description.

      A rule can't have the same name as another rule in the same Region and on the same event bus.

    2. For Event bus, choose default. When an Amazon service in your account generates an event, it always goes to your account's default event bus.

    3. For Rule type, choose Rule with an event pattern.

    4. Choose Next.

  4. For Build event pattern, do the following:

    1. For Event source, choose Amazon events or EventBridge partner events.

    2. For Event pattern, for this example you’ll specify the following event pattern to match the EC2 Fleet Instance Change event.

      { "source": ["aws.ec2fleet"], "detail-type": ["EC2 Fleet Instance Change"] }

      To add the event pattern, you can either use a template by choosing Event pattern form, or specify your own pattern by choosing Custom pattern (JSON editor), as follows:

      1. To use a template to create the event pattern, do the following:

        1. Choose Event pattern form.

        2. For Event source, choose Amazon services.

        3. For Amazon Service, choose EC2 Fleet.

        4. For Event type, choose EC2 Fleet Instance Change.

        5. To customize the template, choose Edit pattern and make your changes to match the example event pattern.

      2. (Alternative) To specify a custom event pattern, do the following:

        1. Choose Custom pattern (JSON editor).

        2. In the Event pattern box, add the event pattern for this example.

    3. Choose Next.

  5. For Select target(s), do the following:

    1. For Target types, choose Amazon service.

    2. For Select a target, choose SNS topic to send an email, text message, or mobile push notification when the event occurs.

    3. For Topic, choose an existing topic. You first need to create an Amazon SNS topic using the Amazon SNS console. For more information, see Using Amazon SNS for application-to-person (A2P) messaging in the Amazon Simple Notification Service Developer Guide.

    4. (Optional) Under Additional settings, you can optionally configure additional settings. For more information, see Creating Amazon EventBridge rules that react to events (step 16) in the Amazon EventBridge User Guide.

    5. Choose Next.

  6. (Optional) For Tags, you can optionally assign one or more tags to your rule, and then choose Next.

  7. For Review and create, do the following:

    1. Review the details of the rule and modify them as necessary.

    2. Choose Create rule.

For more information, see Amazon EventBridge rules and Amazon EventBridge event patterns in the Amazon EventBridge User Guide

Create an EventBridge rule to trigger a Lambda function

The following example creates an EventBridge rule to trigger a Lambda function every time that Amazon EC2 emits an EC2 Fleet instance change notification for when an instance is launched. The signal in this example is emitted as an EC2 Fleet Instance Change event, sub-type launched, which triggers the action defined by the rule.

Before creating the EventBridge rule, you must create the Lambda function.

To create the Lambda function to use in the EventBridge rule

  1. Open the Amazon Lambda console at https://console.amazonaws.cn/lambda/.

  2. Choose Create function.

  3. Enter a name for your function, configure the code, and then choose Create function.

    For more information about using Lambda, see Create a Lambda function with the console in the Amazon Lambda Developer Guide.

To create an EventBridge rule to trigger a Lambda function when an instance in an EC2 Fleet changes state

  1. Open the Amazon EventBridge console at https://console.amazonaws.cn/events/.

  2. Choose Create rule.

  3. For Define rule detail, do the following:

    1. Enter a Name for the rule, and, optionally, a description.

      A rule can't have the same name as another rule in the same Region and on the same event bus.

    2. For Event bus, choose default. When an Amazon service in your account generates an event, it always goes to your account's default event bus.

    3. For Rule type, choose Rule with an event pattern.

    4. Choose Next.

  4. For Build event pattern, do the following:

    1. For Event source, choose Amazon events or EventBridge partner events.

    2. For Event pattern, for this example you’ll specify the following event pattern to match the EC2 Fleet Instance Change event and launched sub-type.

      { "source": ["aws.ec2fleet"], "detail-type": ["EC2 Fleet Instance Change"], "detail": { "sub-type": ["launched"] }

      To add the event pattern, you can either use a template by choosing Event pattern form, or specify your own pattern by choosing Custom pattern (JSON editor), as follows:

      1. To use a template to create the event pattern, do the following:

        1. Choose Event pattern form.

        2. For Event source, choose Amazon services.

        3. For Amazon Service, choose EC2 Fleet.

        4. For Event type, choose EC2 Fleet Instance Change.

        5. Choose Edit pattern, and add "detail": {"sub-type": ["launched"] to match the example event pattern. For proper JSON format, insert a comma (,) after the preceding square bracket (]).

      2. (Alternative) To specify a custom event pattern, do the following:

        1. Choose Custom pattern (JSON editor).

        2. In the Event pattern box, add the event pattern for this example.

    3. Choose Next.

  5. For Select target(s), do the following:

    1. For Target types, choose Amazon service.

    2. For Select a target, choose SNS topic to send an email, text message, or mobile push notification when the event occurs.

    3. For Topic, choose Lambda function, and for Function, choose the function that you created to respond when the event occurs.

    4. (Optional) Under Additional settings, you can optionally configure additional settings. For more information, see Creating Amazon EventBridge rules that react to events (step 16) in the Amazon EventBridge User Guide.

    5. Choose Next.

  6. (Optional) For Tags, you can optionally assign one or more tags to your rule, and then choose Next.

  7. For Review and create, do the following:

    1. Review the details of the rule and modify them as necessary.

    2. Choose Create rule.

For a tutorial on how to create a Lambda function and an EventBridge rule that runs the Lambda function, see Tutorial: Log the State of an Amazon EC2 Instance Using EventBridge in the Amazon Lambda Developer Guide.