

# Amazon EventBridge tutorials
Tutorials

EventBridge integrates with a number of AWS services and SaaS partners. These tutorials are designed to help you get familiar with the basics of EventBridge and how it can be part of your serverless architecture.

**Getting started**

The following tutorials help you explore the features of EventBridge and how to use them.
+ [Archive and replay events](eb-tutorial-archive-replay.md)
+ [Create a sample application](eb-tutorial-get-started.md)
+ [Download code bindings](eb-schema-download-binding-tutorial.md)
+ [Use input transformer](eb-input-transformer-tutorial.md)

**AWS tutorials**

Amazon EventBridge works with other AWS services to process events or invoke an AWS resource as the target of a rule. The following tutorials show you how to integrate EventBridge with other AWS services.
+ [Log Auto Scaling group states](eb-log-as-group-state.md)
+ [Create rule for AWS API calls via CloudTrail](eb-log-api-call.md)
+ [Log Amazon EC2 instance states](eb-log-ec2-instance-state.md)
+ [Log Amazon S3 object operations](eb-log-s3-data-events.md)
+ [Send events using schemas](eb-relay-events-kinesis-stream.md)
+ [Schedule automated Amazon EBS snapshots](eb-scheduled-snapshot.md)
+ [Send an email when events happen](eb-s3-object-created-tutorial.md)
+ [Schedule a Lambda function](eb-run-lambda-schedule.md)

**SaaS providers tutorials**

EventBridge can work directly with SaaS partner applications and services to send and receive [events](eb-events.md). The following tutorials show you how to integrate EventBridge with SaaS partners.
+ [Send events to Datadog](eb-tutorial-datadog.md)
+ [Send events to Salesforce](eb-tutorial-salesforce.md)
+ [Send events to Zendesk](eb-tutorial-zendesk.md)

# Tutorial: Create a sample Amazon EventBridge application
Create a sample application

You can use EventBridge to route [events](eb-events.md) to specific Lambda functions using [rules](eb-rules.md).

In this tutorial, you’ll use the AWS CLI, Node.js, and the code in the [ GitHub repo](https://github.com/aws-samples/amazon-eventbridge-producer-consumer-example) to create the following:
+ An [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) function that produces events for bank ATM transactions.
+ Three Lambda functions to use as [targets](eb-targets.md) of an EventBridge rule.
+ and the rule that routes the created events to the correct downstream function based on an [event pattern](eb-event-patterns.md).

This example uses AWS SAM templates to define the EventBridge rules. To learn more about using AWS SAM templates with EventBridge see [Using AWS Serverless Application Model templates to deploy Amazon EventBridge resources](eb-use-sam.md).

In the repo, the *atmProducer* subdirectory contains `handler.js`, which represents the ATM service producing events. This code is a Lambda handler written in Node.js, and publishes events to EventBridge via the [AWS SDK](https://www.npmjs.com/package/aws-sdk) using this line of JavaScript code.

```
const result = await eventbridge.putEvents(params).promise()
```

This directory also contains `events.js`, listing several test transactions in an Entries array. A single event is defined in JavaScript as follows:

```
{
  // Event envelope fields
  Source: 'custom.myATMapp',
  EventBusName: 'default',
  DetailType: 'transaction',
  Time: new Date(),

  // Main event body
  Detail: JSON.stringify({
    action: 'withdrawal',
    location: 'MA-BOS-01',
    amount: 300,
    result: 'approved',
    transactionId: '123456',
    cardPresent: true,
    partnerBank: 'Example Bank',
    remainingFunds: 722.34
  })
}
```

The *Detail* section of the event specifies transaction attributes. These include the location of the ATM, the amount, the partner bank, and the result of the transaction.

The `handler.js` file in the *atmConsumer* subdirectory contains three functions:

```
exports.case1Handler = async (event) => {
  console.log('--- Approved transactions ---')
  console.log(JSON.stringify(event, null, 2))
}

exports.case2Handler = async (event) => {
  console.log('--- NY location transactions ---')
  console.log(JSON.stringify(event, null, 2))
}

exports.case3Handler = async (event) => {
  console.log('--- Unapproved transactions ---')
  console.log(JSON.stringify(event, null, 2))
}
```

Each function receives transaction events, which are logged via the `console.log` statements to [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html). The consumer functions operate independently of the producer and are unaware of the source of the events.

The routing logic is contained in the EventBridge rules that are deployed by the application’s AWS SAM template. The rules evaluate the incoming stream of events, and route matching events to the target Lambda functions.

The rules use event patterns that are JSON objects with the same structure as the events they match. Here's the event pattern for the one of the rules.

```
{
  "detail-type": ["transaction"],
  "source": ["custom.myATMapp"],
  "detail": {
    "location": [{
      "prefix": "NY-"
    }]
  }
}
```

**Topics**
+ [

## Prerequisites
](#eb-gs-prereqs)
+ [

## Step 1: Create application
](#eb-gs-create-application)
+ [

## Step 2: Run application
](#eb-gs-run-application)
+ [

## Step 3: Check the logs and verify the application works
](#eb-gs-check-logs)
+ [

## Step 4: Clean up your resources
](#cleanup)

## Prerequisites


To complete this tutorial, you'll need the following resources:
+ An AWS account. [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you don't already have one.
+ AWS CLI installed. To install the AWS CLI, see the [Installing, updating, and uninstalling the AWS CLI version 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html).
+ Node.js 12.x installed. To install Node.js, see [ Downloads](https://nodejs.org/en/download/).

## Step 1: Create application


To set up the example application, you'll use the AWS CLI and Git to create the AWS resources you'll need.

**To create the application**

1. [Sign in to AWS](https://console.aws.amazon.com/console/home).

1. [Install Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) and [install the AWS Serverless Application Model CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) on your local machine.

1. Create a new directory, and then navigate to that directory in a terminal.

1. At the command line, enter `git clone https://github.com/aws-samples/amazon-eventbridge-producer-consumer-example`.

1. At the command line run the following command:

   ```
   cd ./amazon-eventbridge-producer-consumer-example
   sam deploy --guided
   ```

1. In the terminal, do the following:

   1. For `Stack Name`, enter a name for the stack. For example, name the stack `Test`.

   1. For `AWS Region`, enter the Region. For example, `us-west-2`.

   1. For `Confirm changes before deploy`, enter `Y`.

   1. For `Allow SAM CLI IAM role creation`, enter `Y`

   1. For `Save arguments to configuration file`, enter `Y`

   1. For `SAM configuration file`, enter `samconfig.toml`.

   1. For `SAM configuration environment`, enter `default`.

## Step 2: Run application


Now that you've set up the resources, you'll use the console to test the functions.

**To run the application**

1. Open the [Lambda console](https://console.aws.amazon.com/lambda/) in the same Region where you deployed the AWS SAM application.

1. There are four Lambda functions with the prefix **atm-demo**. Select the **atmProducerFn** function, then choose **Actions**, **Test**.

1. Enter `Test` for the **Name**.

1. Choose **Test**.

## Step 3: Check the logs and verify the application works


Now that you've run the application, you'll use the console to check the CloudWatch Logs.

**To check the logs**

1. Open the [CloudWatch console](https://console.aws.amazon.com/cloudwatch/) in the same Region where you ran the AWS SAM application.

1. Choose **Logs**, and then choose **Log groups**.

1. Select the log group containing **atmConsumerCase1**. You see two streams representing the two transactions approved by the ATM. Choose a log stream to view the output.

1. Navigate back to the list of log groups, and then select the log group containing **atmConsumerCase2**. You'll see two streams representing the two transactions matching the *New York* location filter.

1. Navigate back to the list of log groups, and select the log group containing **atmConsumerCase3**. Open the stream to see the denied transactions.

## Step 4: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the Lambda function(s)**

1. Open the [Functions page](https://console.aws.amazon.com/lambda/home#/functions) of the Lambda console.

1. Select the function(s) that you created.

1. Choose **Actions**, **Delete**.

1. Choose **Delete**.

**To delete the CloudWatch Logs log group(s)**

1. Open the [Cloudwatch console](https://console.aws.amazon.com/Cloudwatch/home).

1. Choose **Logs**, **Log groups**.

1. Select the log group(s) that were created in this tutorial.

1. Choose **Actions**, **Delete log group(s)**.

1. Choose **Delete**.

# Tutorial: Archive and replay events in Amazon EventBridge
Archive and replay events

You can use EventBridge to route [events](eb-events.md) to specific [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) functions using [rules](eb-rules.md).

In this tutorial, you’ll create a function to use as the target for the EventBridge rule using the Lambda console. Then, you'll create an [archive](eb-archive-event.md) and a rule that'll archive test events using the EventBridge console. Once there are events in that archive, you'll [replay](eb-replay-archived-event.md) them. 

**Topics**
+ [

## Step 1: Create a Lambda function
](#eb-create-lambda-function)
+ [

## Step 2: Create archive
](#eb-ar-create-archive)
+ [

## Step 3: Create rule
](#eb-ar-create-rule)
+ [

## Step 4: Send test events
](#eb-ar-send-test-events)
+ [

## Step 5: Replay events
](#eb-ar-replay-events)
+ [

## Step 6: Clean up your resources
](#cleanup)

## Step 1: Create a Lambda function


First, create a Lambda function to log the events.

**To create a Lambda function:**

1. Open the AWS Lambda console at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).

1. Choose **Create function**.

1. Choose **Author from scratch**.

1. Enter a name and description for the Lambda function. For example, name the function `LogScheduledEvent`.

1. Leave the rest of the options as the defaults and choose **Create function**.

1. On the **Code** tab of the function page, double-click **index.js**.

1. Replace the existing JavaScript code with the following code:

   ```
   'use strict';
   
   exports.handler = (event, context, callback) => {
       console.log('LogScheduledEvent');
       console.log('Received event:', JSON.stringify(event, null, 2));
       callback(null, 'Finished');
   };
   ```

1. Choose **Deploy**.

## Step 2: Create archive


Next, create the archive that will hold all the test events.

**To create an archive**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Archives**.

1. Choose **Create archive**.

1. Enter a name and description for the archive. For example, name the archive `ArchiveTest`.

1. Leave the rest of the options as the defaults and choose **Next**.

1. Choose **Create archive**.

## Step 3: Create rule


Create a rule to archive events that are sent to the event bus.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `ARTestRule`.

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

1. For **Event bus**, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select **default**. When an AWS service in your account emits an event, it always goes to your account’s default event bus.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **Other**.

1. For **Event pattern**, enter the following:

   ```
   {
     "detail-type": [
       "customerCreated"
     ]
   }
   ```

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **Lambda function** from the drop-down list.

1. For **Function**, select the Lambda function that you created in the **Step 1: Create a Lambda function** section. In this example, select `LogScheduledEvent`.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Send test events


Now that you've set up the archive and the rule, we'll send test events to make sure the archive is working correctly.

**Note**  
It can take some time for events to get to the archive.

**To send test events (console)**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Event buses**.

1. In the **Default event bus** tile, choose **Actions**, **Send events**.

1. Enter an event source. For example, `TestEvent`.

1. For **Detail type**, enter `customerCreated`.

1. For **Event detail**, enter `{}`.

1. Choose **Send**.

## Step 5: Replay events


Once the test events are in the archive you can replay them.

**To replay archived events (console)**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Replays**.

1. Choose **Start new replay**.

1. Enter a name and description for the replay. For example, name the replay `ReplayTest`.

1. For **Source**, select the archive you created in the **Step 2: Create archive** section.

1. For **Replay time frame**, do the following.

   1.  For **Start time**, select the date you sent test events and a time before you sent them. For example, `2021/08/11` and `08:00:00`. 

   1.  For **End time**, select the current date and time. For example, `2021/08/11` and `09:15:00`. 

1. Choose **Start Replay**.

## Step 6: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the Lambda function(s)**

1. Open the [Functions page](https://console.aws.amazon.com/lambda/home#/functions) of the Lambda console.

1. Select the function(s) that you created.

1. Choose **Actions**, **Delete**.

1. Choose **Delete**.

**To delete the EventBridge archives(s)**

1. Open the [Archives page](https://console.aws.amazon.com/events/home#/archives) of the EventBridge console.

1. Select the archive(s) you created.

1. Choose **Delete**.

1. Enter the archive name and choose **Delete**.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

# Tutorial: Download code bindings for event schemas in EventBridge
Download code bindings

You can generate [code bindings](eb-schema-code-bindings.md) for [event schemas](eb-schema.md) to speed development for Golang, Java, Python, and TypeScript. You can get code bindings for existing AWS services, schemas you create, and for schemas you generate based on [events](eb-events.md) on an [event bus](eb-event-bus.md). You can generate code bindings for a schema using one of the following:
+ EventBridge console
+ EventBridge schema registry API
+ Your IDE with an AWS toolkit

In this tutorial you generate and download code bindings from an EventBridge schema for the events of an AWS service.

**To generate code bindings from an EventBridge schema**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Schemas**.

1. Select the **AWS event schema registry** tab.

1. Find the schema for the AWS service that you would like code bindings for, either by browsing through the schema registry, or by searching for a schema.

1. Select the schema name.

1. On the **Schema details** page, in the **Version** section, select **Download code bindings**.

1. On the **Download code bindings** page, select the language of the code bindings you want to download.

1. Select **Download**.

   It may take a few seconds for your download to begin. The download file will be a .zip file of code bindings for the language you selected.

1. Unzip the downloaded file and add it to your project.

   The downloaded package contains a README file that explains how to configure the package's dependencies in various frameworks.

Use these code bindings in your own code to help quickly build applications using this EventBridge event.

# Tutorial: Use input transformers to transform events in EventBridge
Use input transformer

You can use the [Input transformer](eb-transform-target-input.md) in EventBridge to customize text from an [event](eb-events.md) before you send it to the target of a [rule](eb-rules.md). 

To do this, you define JSON paths from the event and assign their outputs to different variables. Then you can use those variables in the input template. The characters < and > can't be escaped. For more information, see [Amazon EventBridge input transformation](eb-transform-target-input.md)

**Note**  
If you specify a variable to match a JSON path that doesn't exist in the event, that variable isn't created and doesn't appear in the output.

In this tutorial, you create a rule that matches an event with `detail-type: "customerCreated"`. The input transformer maps the `type` variable to the \$1.detail-type JSON path from the event. Then EventBridge puts the variable into the input template "This event was <type>." The result is the following Amazon SNS message.

```
"This event was of customerCreated type."
```

**Topics**
+ [

## Step 1: Create an Amazon SNS topic
](#eb-input-transformer-tutorial-create-topic)
+ [

## Step 2: Create an Amazon SNS subscription
](#eb-input-transformer-tutorial-create-sns)
+ [

## Step 3: Create a rule
](#eb-input-transformer-create-rule)
+ [

## Step 4: Send test events
](#eb-input-transformer-send-test-events)
+ [

## Step 5: Confirm success
](#success)
+ [

## Step 6: Clean up your resources
](#cleanup)

## Step 1: Create an Amazon SNS topic


Create a topic to receive the events from EventBridge.

**To create a topic**

1. Open the Amazon SNS console at [https://console.aws.amazon.com/sns/v3/home](https://console.aws.amazon.com/sns/v3/home).

1. In the navigation pane, choose **Topics**.

1. Choose **Create topic**.

1. For **Type**, choose **Standard**.

1. Enter **eventbridge-IT-test** as the name of the topic.

1. Choose **Create topic**.

## Step 2: Create an Amazon SNS subscription


Create a subscription to get emails with the transformed information.

**To create a subscription**

1. Open the Amazon SNS console at [https://console.aws.amazon.com/sns/v3/home](https://console.aws.amazon.com/sns/v3/home).

1. In the navigation pane, choose **Subscriptions**.

1. Choose **Create subscription**.

1. For **Topic ARN**, choose the topic you created in step 1. For this tutorial, choose **eventbridge-IT-test**.

1. For **Protocol**, choose **Email**.

1. For **Endpoint**, enter your email address.

1. Choose **Create subscription**.

1. Confirm the subscription by choosing **Confirm subscription** in the email you receive from AWS notifications.

## Step 3: Create a rule


Create a rule to use the input transformer to customize the instance state information that goes to a target.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `ARTestRule`

1. For **Event bus**, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select **default**. When an AWS service in your account emits an event, it always goes to your account’s default event bus.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **Other**.

1. For **Event pattern**, enter the following:

   ```
   {
     "detail-type": [
       "customerCreated"
     ]
   }
   ```

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **SNS topic** from the drop-down list.

1. For **Topic**, select the Amazon SNS topic that you created in step 1. For this tutorial, choose **eventbridge-IT-test**.

1. For **Additional settings**, do the following:

   1. For **Configure target input**, choose **Input transformer** from the drop-down list.

   1. Choose **Configure input transformer**

   1. for **Sample events**, enter the following:

      ```
      {
        "detail-type": "customerCreated"
      }
      ```

   1. For **Target input transformer** do the following:

      1. For **Input Path**, enter the following:

         ```
         {"detail-type":"$.detail-type"}
         ```

      1. For **Input Template**, enter the following:

         ```
         "This event was of <detail-type> type."
         ```

   1. Choose **Confirm.**.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Send test events


Now that you've set up the SNS topic and the rule, we'll send test events to make sure the rule is working correctly.

**To send test events (console)**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Event buses**.

1. In the **Default event bus** tile, choose **Actions**, **Send events**.

1. Enter an event source. For example, `TestEvent`.

1. For **Detail type**, enter `customerCreated`.

1. For **Event detail**, enter `{}`.

1. Choose **Send**.

## Step 5: Confirm success


If you get an email from AWS notifications that matches the expected output, you've successfully completed the tutorial.

## Step 6: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the SNS topic**

1. Open the [Topics page](https://console.aws.amazon.com/sns/v3/home#/topics) of the SNS console.

1. Select the topic that you created.

1. Choose **Delete**.

1. Enter **delete me**.

1. Choose **Delete**.

**To delete the SNS subscription**

1. Open the [ Subscriptions page](https://console.aws.amazon.com/sns/v3/home#/subscriptions) of the SNS console.

1. Select the subscription that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

# Tutorial: Log the state of an Auto Scaling group using EventBridge
Log Auto Scaling group states

You can run an [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) function that logs an [events](eb-events.md) whenever an Auto Scaling group launches or terminates an Amazon EC2 instance that indicates whether an event was successful.

For information about more scenarios that use Amazon EC2 Auto Scaling events, see [Use EventBridge to handle Auto Scaling events](https://docs.aws.amazon.com/autoscaling/latest/userguide/automating-ec2-auto-scaling-with-eventbridge.html) in the *Amazon EC2 Auto Scaling User Guide*.

In this tutorial, you create a Lambda function, and you create a [rule](eb-rules.md) in the EventBridge console that calls that function when an Amazon EC2 Auto Scaling group launches or terminates an instance.

**Topics**
+ [

## Prerequisites
](#eb-as-prereqs)
+ [

## Step 1: Create a Lambda function
](#eb-as-create-lambda-function)
+ [

## Step 2: Create a rule
](#eb-as-create-rule)
+ [

## Step 3: Test the rule
](#eb-as-test-rule)
+ [

## Step 4: Confirm success
](#success)
+ [

## Step 5: Clean up your resources
](#cleanup)

## Prerequisites


To complete this tutorial, you'll need the following resources:
+ An Auto Scaling group. For more information about creating one, see [Creating an Auto Scaling group using a launch configuration](https://docs.aws.amazon.com/autoscaling/latest/userguide/create-asg.html) in the Amazon EC2 Auto Scaling User Guide.

## Step 1: Create a Lambda function


Create a Lambda function to log the scale-out and scale-in events for your Auto Scaling group. 

**To create a Lambda function**

1. Open the AWS Lambda console at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).

1. Choose **Create function**.

1. Choose **Author from scratch**.

1. Enter a name for the Lambda function. For example, name the function `LogAutoScalingEvent`.

1. Leave the rest of the options as the defaults and choose **Create function**.

1. On the **Code** tab of the function page, double-click **index.js**.

1. Replace the existing code with the following code.

   ```
   'use strict';
   
   exports.handler = (event, context, callback) => {
       console.log('LogAutoScalingEvent');
       console.log('Received event:', JSON.stringify(event, null, 2));
       callback(null, 'Finished');
   };
   ```

1. Choose **Deploy**.

## Step 2: Create a rule


Create a rule to run the Lambda function you created in Step 1. The rule runs when your Auto Scaling group starts or stops an instance.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `TestRule`

1. For **Event bus**, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select **default**. When an AWS service in your account emits an event, it always goes to your account’s default event bus.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **AWS services**.

1. For **Event pattern**, do the following:

   1. For **Event source**, select **Auto Scaling** from the drop-down list.

   1. For **Event type**, select **Instance Launch and Terminate** from the drop-down list.

   1. Choose **Any instance event** and **Any group name**.

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **Lambda function** from the drop-down list.

1. For **Function**, select the Lambda function that you created in the **Step 1: Create a Lambda function** section. In this example, select `LogAutoScalingEvent`.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 3: Test the rule


You can test your rule by manually scaling an Auto Scaling group so that it launches an instance. Wait a few minutes for the scale-out event to occur, and then verify that your Lambda function was invoked.

**To test your rule using an Auto Scaling group**

1. To increase the size of your Auto Scaling group, do the following:

   1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

   1. In the navigation pane, choose **Auto Scaling**, **Auto Scaling Groups**.

   1. Select the check box for your Auto Scaling group.

   1. On the **Details** tab, choose **Edit**. For **Desired**, increase the desired capacity by one. For example, if the current value is **2**, enter **3**. The desired capacity must be less than or equal to the maximum size of the group. If your new value for **Desired** is greater than **Max**, you must update **Max**. When you're finished, choose **Save**.

1. To view the output from your Lambda function, do the following:

   1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

   1. In the navigation pane, choose **Logs**.

   1. Select the name of the log group for your Lambda function (`/aws/lambda/function-name`).

   1. Select the name of the log stream to view the data provided by the function for the instance that you launched.

1. (Optional) When you're finished, you can decrease the desired capacity by one so that the Auto Scaling group returns to its previous size.

## Step 4: Confirm success


If you see the Lambda event in the CloudWatch logs, you've successfully completed this tutorial. If the event isn't in your CloudWatch logs, start troubleshooting by verifying the rule was created successfully and, if the rule looks correct, verify the code of your Lambda function is correct.

## Step 5: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the Lambda function(s)**

1. Open the [Functions page](https://console.aws.amazon.com/lambda/home#/functions) of the Lambda console.

1. Select the function(s) that you created.

1. Choose **Actions**, **Delete**.

1. Choose **Delete**.

# Tutorial: Create an EventBridge rule that reacts to AWS API calls via CloudTrail
Create rule for AWS API calls via CloudTrail

You can use Amazon EventBridge [rules](eb-rules.md) to react to API calls made by an AWS service that are recorded by AWS CloudTrail.

In this tutorial, you create an [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) trail, a Lambda function, and a rule in the EventBridge console. The rule invokes the Lambda function when an Amazon EC2 instance is stopped.

**Topics**
+ [

## Step 1: Create an AWS CloudTrail trail
](#eb-log-api-create-ct-trail)
+ [

## Step 2: Create an AWS Lambda function
](#eb-api-create-lambda-function)
+ [

## Step 3: Create a rule
](#eb-api-create-rule)
+ [

## Step 4: Test the rule
](#eb-api-test-rule)
+ [

## Step 5: Confirm success
](#success)
+ [

## Step 6: Clean up your resources
](#cleanup)

## Step 1: Create an AWS CloudTrail trail


If you already have a trail set up, skip to step 2.

**To create a trail**

1. Open the CloudTrail console at [https://console.aws.amazon.com/cloudtrail/](https://console.aws.amazon.com/cloudtrail/).

1. Choose **Trails**, **Create trail**.

1. For **Trail name**, type a name for the trail.

1. For **Storage location**, in **Create a new S3 bucket**.

1. For **AWS KMS alias**, type an alias for the KMS key.

1. Choose **Next**.

1. Choose **Next**.

1. Choose **Create trail**.

## Step 2: Create an AWS Lambda function


Create a Lambda function to log the API call events. 

**To create a Lambda function**

1. Open the AWS Lambda console at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).

1. Choose **Create function**.

1. Choose **Author from scratch**.

1. Enter a name and description for the Lambda function. For example, name the function `LogEC2StopInstance`.

1. Leave the rest of the options as the defaults and choose **Create function**.

1. On the **Code** tab of the function page, double-click **index.js**.

1. Replace the existing code with the following code.

   ```
   'use strict';
   
   exports.handler = (event, context, callback) => {
       console.log('LogEC2StopInstance');
       console.log('Received event:', JSON.stringify(event, null, 2));
       callback(null, 'Finished');
   };
   ```

1. Choose **Deploy**.

## Step 3: Create a rule


Create a rule to run the Lambda function you created in step 2 whenever you stop an Amazon EC2 instance.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `TestRule`

1. For **Event bus**, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select **default**. When an AWS service in your account emits an event, it always goes to your account’s default event bus.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **AWS services**.

1. For **Event pattern**, do the following:

   1. For **Event source**, select **EC2** from the drop-down list.

   1. For **Event type**, select **AWS API Call via CloudTrail** from the drop-down list.

   1. Choose **Specific operation(s)** and enter `StopInstances`.

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **Lambda function** from the drop-down list.

1. For **Function**, select the Lambda function that you created in the **Step 1: Create a Lambda function** section. In this example, select `LogEC2StopInstance`.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Test the rule


You can test your rule by stopping an Amazon EC2 instance using the Amazon EC2 console. Wait a few minutes for the instance to stop, and then check your AWS Lambda metrics on the CloudWatch console to verify that your function ran.

**To test your rule by stopping an instance**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. Launch an instance. For more information, see [Launch Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/LaunchingAndUsingInstances.html) in the *Amazon EC2 User Guide*.

1. Stop the instance. For more information, see [Stop and Start Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html) in the *Amazon EC2 User Guide*.

1. To view the output from your Lambda function, do the following:

   1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

   1. In the navigation pane, choose **Logs**.

   1. Select the name of the log group for your Lambda function (`/aws/lambda/function-name`).

   1. Select the name of the log stream to view the data provided by the function for the instance that you stopped.

1. (Optional) When you're finished, terminate the stopped instance. For more information, see [Terminate Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html) in the *Amazon EC2 User Guide*.

## Step 5: Confirm success


If you see the Lambda event in the CloudWatch logs, you've successfully completed this tutorial. If the event isn't in your CloudWatch logs, start troubleshooting by verifying the rule was created successfully and, if the rule looks correct, verify the code of your Lambda function is correct.

## Step 6: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the Lambda function(s)**

1. Open the [Functions page](https://console.aws.amazon.com/lambda/home#/functions) of the Lambda console.

1. Select the function(s) that you created.

1. Choose **Actions**, **Delete**.

1. Choose **Delete**.

**To delete the CloudTrail trail(s)**

1. Open the [Trails page](https://console.aws.amazon.com/cloudtrail/home#/trails) of the CloudTrail console.

1. Select the trail(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

# Tutorial: Log the state of an Amazon EC2 instance using EventBridge
Log Amazon EC2 instance states

You can create an [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) function that logs a state change for an [Amazon EC2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) instance. Then you can create a [rule](eb-rules.md) that runs your Lambda function whenever there is a state transition or a transition to one or more states that are of interest. In this tutorial, you log the launch of any new instance.

**Topics**
+ [

## Step 1: Create an AWS Lambda function
](#eb-ec2-create-lambda-function)
+ [

## Step 2: Create a rule
](#eb-ec2-create-rule)
+ [

## Step 3: Test the rule
](#eb-api-test-rule)
+ [

## Step 4: Confirm success
](#success)
+ [

## Step 5: Clean up your resources
](#cleanup)

## Step 1: Create an AWS Lambda function


Create a Lambda function to log the state change [events](eb-events.md). When you create your rule in Step 2, you specify this function.

**To create a Lambda function**

1. Open the AWS Lambda console at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).

1. Choose **Create function**.

1. Choose **Author from scratch**.

1. Enter a name and description for the Lambda function. For example, name the function `LogEC2InstanceStateChange`.

1. Leave the rest of the options as the defaults and choose **Create function**.

1. On the **Code** tab of the function page, double-click **index.js**.

1. Replace the existing code with the following code.

   ```
   'use strict';
   
   exports.handler = (event, context, callback) => {
       console.log('LogEC2InstanceStateChange');
       console.log('Received event:', JSON.stringify(event, null, 2));
       callback(null, 'Finished');
   };
   ```

1. Choose **Deploy**.

## Step 2: Create a rule


Create a rule to run the Lambda function you created in Step 1. The rule runs when you launch an Amazon EC2 instance.

**To create the EventBridge rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `TestRule`

1. For **Event bus**, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select **default**. When an AWS service in your account emits an event, it always goes to your account’s default event bus.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **AWS services**.

1. For **Event pattern**, do the following:

   1. For **Event source**, select **EC2** from the drop-down list.

   1. For **Event type**, choose **EC2 Instance State-change Notification** from the drop-down list.

   1. Choose **Specific states(s)** and choose **running** from the drop-down list.

   1. Choose **Any instance**

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **Lambda function** from the drop-down list.

1. For **Function**, select the Lambda function that you created in the **Step 1: Create a Lambda function** section. In this example, select `LogEC2InstanceStateChange`.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 3: Test the rule


You can test your rule by stopping an Amazon EC2 instance using the Amazon EC2 console. Wait a few minutes for the instance to stop, and then check your AWS Lambda metrics on the CloudWatch console to verify that your function ran.

**To test your rule by stopping an instance**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. Launch an instance. For more information, see [Launch Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/LaunchingAndUsingInstances.html) in the *Amazon EC2 User Guide*.

1. Stop the instance. For more information, see [Stop and Start Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html) in the *Amazon EC2 User Guide*.

1. To view the output from your Lambda function, do the following:

   1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

   1. In the navigation pane, choose **Logs**.

   1. Select the name of the log group for your Lambda function (`/aws/lambda/function-name`).

   1. Select the name of the log stream to view the data provided by the function for the instance that you stopped.

1. (Optional) When you're finished, terminate the stopped instance. For more information, see [Terminate Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html) in the *Amazon EC2 User Guide*.

## Step 4: Confirm success


If you see the Lambda event in the CloudWatch logs, you've successfully completed this tutorial. If the event isn't in your CloudWatch logs, start troubleshooting by verifying the rule was created successfully and, if the rule looks correct, verify the code of your Lambda function is correct.

## Step 5: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the Lambda function(s)**

1. Open the [Functions page](https://console.aws.amazon.com/lambda/home#/functions) of the Lambda console.

1. Select the function(s) that you created.

1. Choose **Actions**, **Delete**.

1. Choose **Delete**.

# Tutorial: Log Amazon S3 object-level operations using EventBridge
Log Amazon S3 object operations

You can log the object-level API operations on your [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) buckets. Before Amazon EventBridge can match these [events](eb-events.md), you must use [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) to set up and configure a trail to receive these events.

In this tutorial, you create CloudTrail trail, create a [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) function, and then create [rule](eb-rules.md) in the EventBridge console that invokes that function in response to an S3 data event.

**Topics**
+ [

## Step 1: Configure your AWS CloudTrail trail
](#eb-configure-trail)
+ [

## Step 2: Create an AWS Lambda function
](#eb-log-s3-create-lambda-function)
+ [

## Step 3: Create a Rule
](#eb-log-s3-create-rule)
+ [

## Step 4: Test the Rule
](#eb-log-s3-test-rule)
+ [

## Step 5: Confirm success
](#success)
+ [

## Step 6: Clean up your resources
](#cleanup)

## Step 1: Configure your AWS CloudTrail trail


To log data events for an S3 bucket to AWS CloudTrail and EventBridge, you first create a trail. A *trail* captures API calls and related events in your account and then delivers the log files to an S3 bucket that you specify. You can update an existing trail or create one.

For more information, see [Data Events](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-management-and-data-events-with-cloudtrail.html#logging-data-events) in the *AWS CloudTrail User Guide*. 

**To create a trail**

1. Open the CloudTrail console at [https://console.aws.amazon.com/cloudtrail/](https://console.aws.amazon.com/cloudtrail/).

1. Choose **Trails**, **Create trail**.

1. For **Trail name**, type a name for the trail.

1. For **Storage location**, in **Create a new S3 bucket**.

1. For **AWS KMS alias**, type an alias for the KMS key.

1. Choose **Next**.

1. For **Event type**, choose **Data events**

1. For **Data events,** do one of the following:
   + To log data events for all Amazon S3 objects in a bucket, specify an S3 bucket and an empty prefix. When an event occurs on an object in that bucket, the trail processes and logs the event.
   + To log data events for specific Amazon S3 objects in a bucket, specify an S3 bucket and the object prefix. When an event occurs on an object in that bucket and the object starts with the specified prefix, the trail processes and logs the event.

1. For each resource, choose whether to log **Read** events, **Write** events, or both.

1. Choose **Next**.

1. Choose **Create trail**.

## Step 2: Create an AWS Lambda function


Create a Lambda function to log data events for your S3 buckets. 

**To create a Lambda function**

1. Open the AWS Lambda console at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).

1. Choose **Create function**.

1. Choose **Author from scratch**.

1. Enter a name and description for the Lambda function. For example, name the function `LogS3DataEvents`.

1. Leave the rest of the options as the defaults and choose **Create function**.

1. On the **Code** tab of the function page, double-click **index.js**.

1. Replace the existing code with the following code.

   ```
   'use strict';
   
   exports.handler = (event, context, callback) => {
       console.log('LogS3DataEvents');
       console.log('Received event:', JSON.stringify(event, null, 2));
       callback(null, 'Finished');
   };
   ```

1. Choose **Deploy**.

## Step 3: Create a Rule


Create a rule to run the Lambda function you created in Step 2. This rule runs in response to an Amazon S3 data event.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `TestRule`

1. For **Event bus**, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select **default**. When an AWS service in your account emits an event, it always goes to your account’s default event bus.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **AWS services**.

1. For **Event pattern**, do the following:

   1. For **Event source**, select **Simple Storage Service (S3)** from the drop-down list.

   1. For **Event type**, select **Object-Level API call via CloudTrail** from the drop-down list.

   1. Choose **Specific operation(s)**, and then choose **PutObject**.

   1. By default, the rule matches data events for all buckets in the Region. To match data events for specific buckets, choose **Specify bucket(s) by name** and enter one or more buckets.

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **Lambda function** from the drop-down list.

1. For **Function**, select the `LogS3DataEvents` Lambda function that you created in step 1.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Test the Rule


To test the rule, put an object in your S3 bucket. You can verify that your Lambda function was invoked.

**To view the logs for your Lambda function**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. In the navigation pane, choose **Logs**.

1. Select the name of the log group for your Lambda function (`/aws/lambda/function-name`).

1. Select the name of the log stream to view the data provided by the function for the instance that you launched.

You can also check your CloudTrail logs in the S3 bucket that you specified for your trail. For more information, see [Getting and Viewing Your CloudTrail Log Files](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/get-and-view-cloudtrail-log-files.html) in the *AWS CloudTrail User Guide*.

## Step 5: Confirm success


If you see the Lambda event in the CloudWatch logs, you've successfully completed this tutorial. If the event isn't in your CloudWatch logs, start troubleshooting by verifying the rule was created successfully and, if the rule looks correct, verify the code of your Lambda function is correct.

## Step 6: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the Lambda function(s)**

1. Open the [Functions page](https://console.aws.amazon.com/lambda/home#/functions) of the Lambda console.

1. Select the function(s) that you created.

1. Choose **Actions**, **Delete**.

1. Choose **Delete**.

**To delete the CloudTrail trail(s)**

1. Open the [Trails page](https://console.aws.amazon.com/cloudtrail/home#/trails) of the CloudTrail console.

1. Select the trail(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

# Tutorial: Send events to Amazon Kinesis using EventBridge schemas
Send events using schemas

You can send AWS API call [events](eb-events.md) in EventBridge to an [Amazon Kinesis stream](https://docs.aws.amazon.com/streams/latest/dev/introduction.html), create Kinesis Data Streams applications, and process large amounts of data. In this tutorial, you create a Kinesis stream, and then create a [rule](eb-rules.md) in the EventBridge console that sends events to that stream when an [Amazon EC2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) instance stops.

**Topics**
+ [

## Prerequisites
](#eb-stream-prerequisite)
+ [

## Step 1: Create an Amazon Kinesis stream
](#eb-stream-create-stream)
+ [

## Step 2: Create a rule
](#eb-stream-create-rule)
+ [

## Step 3: Test the rule
](#eb-stream-test-rule)
+ [

## Step 4: Verify that the event was sent
](#eb-stream-verify-event)
+ [

## Step 5: Clean up your resources
](#cleanup)

## Prerequisites


In this tutorial, you'll use the following:
+ Use the AWS CLI to work with Kinesis streams.

  To install the AWS CLI, see the [Installing, updating, and uninstalling the AWS CLI version 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html).

**Note**  
This tutorial uses AWS events and the built in `aws.events` schema registry. You can also create an EventBridge rule based on the schema of your custom events by adding them to a custom schema registry manually, or by using schema discovery.   
For more information on schemas, see [Amazon EventBridge schemas](eb-schema.md). For more information on creating a rule using other event pattern options, see [Creating rules in Amazon EventBridge](eb-create-rule-visual.md).

## Step 1: Create an Amazon Kinesis stream


To create a stream, at a command prompt, use the `create-stream` AWS CLI command.

```
aws kinesis create-stream --stream-name test --shard-count 1
```

When the stream status is `ACTIVE`, the stream is ready. To check the stream status, use the `describe-stream` command.

```
aws kinesis describe-stream --stream-name test
```

## Step 2: Create a rule


Create a rule to send events to your stream when you stop an Amazon EC2 instance.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `TestRule`

1. For **Event bus**, select **default**.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **AWS events or EventBridge partner events**.

1. For **Creation method**, choose **Use schema**.

1. For **Event pattern**, do the following:

   1. For **Schema type**, choose **Select schema from Schema registry**.

   1. For **Schema registry**, choose **aws.events** from the drop-down list.

   1. For **Schema**, choose **aws.ec2@EC2InstanceStateChangeNotification** from the drop-down list.

      EventBridge displays the event schema under **Models**.

      EventBridge displays a red asterisk next to any properties that are required *for the event*, not for the event pattern. 

   1. In **Models**, set the following event filter properties: 

      1. Select **\$1 Edit** next to the **state** property. 

         Leave **Relationship** empty. For **Value**, enter `running`. Choose **Set**. 

      1. Select **\$1 Edit** next to the **source** property. 

         Leave **Relationship** empty. For **Value**, enter `aws.ec2`. Choose **Set**. 

      1. Select **\$1 Edit** next to the **detail-type** property. 

         Leave **Relationship** empty. For **Value**, enter `EC2 Instance State-change Notification`. Choose **Set**. 

   1. To view the event pattern you've constructed, choose **Generate event pattern in JSON**

      EventBridge displays the event pattern in JSON:

      ```
      {
        "detail": {
          "state": ["running"]
        },
        "detail-type": ["EC2 Instance State-change Notification"],
        "source": ["aws.ec2"]
      }
      ```

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **Kinesis stream** from the drop-down list.

1. For **Stream**, select the Kinesis stream that you created in the **Step 1: Create an Amazon Kinesis stream** section. In this example, select `test`.

1. For **Execution role**, choose **Create a new for role for this specific resource**.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 3: Test the rule


To test your rule, stop an Amazon EC2 instance. Wait a few minutes for the instance to stop, and then check your CloudWatch metrics to verify that your function ran.

**To test your rule by stopping an instance**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. Launch an instance. For more information, see [Launch Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/LaunchingAndUsingInstances.html) in the *Amazon EC2 User Guide*.

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

   Choose the name of the rule that you created and choose **Metrics for the rule**.

1. (Optional) When you're finished, terminate the instance. For more information, see [Terminate Your Instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html) in the *Amazon EC2 User Guide*.

## Step 4: Verify that the event was sent


You can use the AWS CLI to get the record from the stream to verify that the event was sent.

**To get the record**

1. To start reading from your Kinesis stream, at a command prompt, use the `get-shard-iterator` command.

   ```
   aws kinesis get-shard-iterator --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON --stream-name test
   ```

   The following is example output.

   ```
   {
       "ShardIterator": "AAAAAAAAAAHSywljv0zEgPX4NyKdZ5wryMzP9yALs8NeKbUjp1IxtZs1Sp+KEd9I6AJ9ZG4lNR1EMi+9Md/nHvtLyxpfhEzYvkTZ4D9DQVz/mBYWRO6OTZRKnW9gd+efGN2aHFdkH1rJl4BL9Wyrk+ghYG22D2T1Da2EyNSH1+LAbK33gQweTJADBdyMwlo5r6PqcP2dzhg="
   }
   ```

1. To get the record, use the following `get-records` command. Use the shard iterator from the output in the previous step.

   ```
   aws kinesis get-records --shard-iterator AAAAAAAAAAHSywljv0zEgPX4NyKdZ5wryMzP9yALs8NeKbUjp1IxtZs1Sp+KEd9I6AJ9ZG4lNR1EMi+9Md/nHvtLyxpfhEzYvkTZ4D9DQVz/mBYWRO6OTZRKnW9gd+efGN2aHFdkH1rJl4BL9Wyrk+ghYG22D2T1Da2EyNSH1+LAbK33gQweTJADBdyMwlo5r6PqcP2dzhg=
   ```

   If the command is successful, it requests records from your stream for the specified shard. You can receive zero or more records. Any records returned might not represent all records in your stream. If you don't receive the data that you expect, keep calling `get-records`. 

1. Records in Kinesis are encoded in Base64. Use a Base64 decoder to decode the data so that you can verify that it's the event that was sent to the stream in JSON form.

## Step 5: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the Kinesis stream(s)**

1. Open the [Data streams page](https://console.aws.amazon.com/kinesis/home#/streams/list) of the Kinesis console.

1. Select the stream(s) that you created.

1. Choose **Actions**, **Delete**.

1. Enter **delete** in the fiekd and choose **Delete**.

# Tutorial: Schedule automated Amazon EBS snapshots
Schedule automated Amazon EBS snapshots

**Note**  
Scheduled rules are a legacy feature of EventBridge.  
EventBridge offers a more flexible and powerful way to create, run, and manage scheduled tasks centrally, at scale: EventBridge Scheduler. With EventBridge Scheduler, you can create schedules using cron and rate expressions for recurring patterns, or configure one-time invocations. You can set up flexible time windows for delivery, define retry limits, and set the maximum retention time for failed API invocations.   
Scheduler is highly customizable, and offers improved scalability over scheduled rules, with a wider set of target API operations and AWS services. We recommend that you use Scheduler to invoke targets on a schedule.  
For more information, see [Create a schedule](using-eventbridge-scheduler.md#using-eventbridge-scheduler-create) or the *[EventBridge Scheduler User Guide](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html)*.

This tutorial previously demonstrated how to create automated Amazon EBS snapshots using EventBridge scheduled rules. We now recommend using one of the following alternatives, depending on your use case.

## Recommended: Amazon Data Lifecycle Manager


For automated Amazon EBS snapshot lifecycle management, including creation, retention, and deletion on a schedule, we recommend [Amazon Data Lifecycle Manager](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/snapshot-lifecycle.html) (Amazon Data Lifecycle Manager). Amazon Data Lifecycle Manager is purpose-built for Amazon EBS snapshot automation and provides policy-based lifecycle management without requiring you to build and maintain scheduling infrastructure.

## Alternative: Amazon EventBridge Scheduler


If you need more flexible scheduling options or want to combine snapshot creation with other AWS API actions, you can use Amazon EventBridge Scheduler. EventBridge Scheduler offers improved scalability over EventBridge scheduled rules, with a wider set of target API operations, flexible time windows, and built-in retry support.

To get started with EventBridge Scheduler, see [Getting started with Amazon EventBridge Scheduler](https://docs.aws.amazon.com/scheduler/latest/UserGuide/getting-started.html) in the *Amazon EventBridge Scheduler User Guide*.

## Legacy: EventBridge scheduled rules


If you still need to use EventBridge scheduled rules, see [Creating a scheduled rule (legacy) in Amazon EventBridge](eb-create-rule-schedule.md).

# Tutorial: Send an email when events happen using Amazon EventBridge
Send an email when events happen

You can send email notifications when [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) objects are created using Amazon EventBridge and [Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/welcome.html). In this tutorial, you will create an SNS topic and subscription. Then, you will create a [rule](eb-rules.md) in the EventBridge console that sends [events](eb-events.md) to that topic when Amazon S3 `Object Created` events are received.

**Topics**
+ [

## Prerequisites
](#eb-s3-object-created-tutorial-prerequisite)
+ [

## Step 1: Create an Amazon SNS topic
](#eb-s3-object-created-tutorial-create-topic)
+ [

## Step 2: Create an Amazon SNS subscription
](#eb-s3-object-created-tutorial-create-sns)
+ [

## Step 3: Create a rule
](#eb-s3-object-created-tutorial-create-rule)
+ [

## Step 4: Test the rule
](#eb-s3-object-created-tutorial-test-rule)
+ [

## Step 5: Clean up your resources
](#cleanup)

## Prerequisites


To recieve Amazon S3 events in EventBridge, you must enable EventBridge in the Amazon S3 console. This tutorial assumes EventBridge is enabled. For more information, see [Enabling Amazon EventBridge in the S3 console](https://docs.aws.amazon.com/AmazonS3/latest/userguide/enable-event-notifications-eventbridge.html).

## Step 1: Create an Amazon SNS topic


Create a topic to receive the events from EventBridge.

**To create a topic**

1. Open the Amazon SNS console at [https://console.aws.amazon.com/sns/v3/home](https://console.aws.amazon.com/sns/v3/home).

1. In the navigation pane, choose **Topics**.

1. Choose **Create topic**.

1. For **Type**, choose **Standard**.

1. Enter **eventbridge-test** as the name of the topic.

1. Choose **Create topic**.

## Step 2: Create an Amazon SNS subscription


Create a subscription to get email notifications from Amazon S3 when events are received by the topic.

**To create a subscription**

1. Open the Amazon SNS console at [https://console.aws.amazon.com/sns/v3/home](https://console.aws.amazon.com/sns/v3/home).

1. In the navigation pane, choose **Subscriptions**.

1. Choose **Create subscription**.

1. For **Topic ARN**, choose the topic you created in step 1. For this tutorial, choose **eventbridge-test**.

1. For **Protocol**, choose **Email**.

1. For **Endpoint**, enter your email address.

1. Choose **Create subscription**.

1. Confirm the subscription by choosing **Confirm subscription** in the email you receive from AWS notifications.

## Step 3: Create a rule


Create a rule to send events to your topic when an Amazon S3 object is created.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, name the rule `s3-test`

1. For **Event bus**, select **default**.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **AWS events or EventBridge partner events**.

1. For **Creation method**, choose **Use pattern form**.

1. For **Event pattern**, do the following:

   1. For **Event source**, select **AWS services** from the drop-down list.

   1. For **AWS service**, select **Simple Storage Service (S3)** from the drop-down list.

   1. For **Event type**, choose ** Amazon S3 Event Notification** from the drop-down list.

   1. Choose **Specific events(s)** and choose **Object Created** from the drop-down list.

   1. Choose **Any bucket**

1. Choose **Next**.

1. For **Target types**, choose **AWS service**.

1. For **Select a target**, choose **SNS topic** from the drop-down list.

1. For **Topic**, select the Amazon SNS topic that you created in the **Step 1: Create an SNS topic** section. In this example, select `eventbridge-test`.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Test the rule


To test your rule, create an Amazon S3 object by uploading a file to an EventBridge-enabled bucket. Then, wait a few minutes and verify if you receive an email from AWS notifications.

## Step 5: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the SNS topic**

1. Open the [Topics page](https://console.aws.amazon.com/sns/v3/home#/topics) of the SNS console.

1. Select the topic that you created.

1. Choose **Delete**.

1. Enter **delete me**.

1. Choose **Delete**.

**To delete the SNS subscription**

1. Open the [ Subscriptions page](https://console.aws.amazon.com/sns/v3/home#/subscriptions) of the SNS console.

1. Select the subscription that you created.

1. Choose **Delete**.

1. Choose **Delete**.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

# Tutorial: Schedule a AWS Lambda function
Schedule a Lambda function

**Note**  
Scheduled rules are a legacy feature of EventBridge.  
EventBridge offers a more flexible and powerful way to create, run, and manage scheduled tasks centrally, at scale: EventBridge Scheduler. With EventBridge Scheduler, you can create schedules using cron and rate expressions for recurring patterns, or configure one-time invocations. You can set up flexible time windows for delivery, define retry limits, and set the maximum retention time for failed API invocations.   
Scheduler is highly customizable, and offers improved scalability over scheduled rules, with a wider set of target API operations and AWS services. We recommend that you use Scheduler to invoke targets on a schedule.  
For more information, see [Create a schedule](using-eventbridge-scheduler.md#using-eventbridge-scheduler-create) or the *[EventBridge Scheduler User Guide](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html)*.

This tutorial previously demonstrated how to invoke a Lambda function on a schedule using EventBridge scheduled rules. We now recommend using Amazon EventBridge Scheduler instead. EventBridge Scheduler offers improved scalability, a wider set of target API operations, flexible time windows, and built-in retry and dead-letter queue support.

For a complete walkthrough of scheduling a Lambda function using EventBridge Scheduler, see [Invoke a Lambda function on a schedule](https://docs.aws.amazon.com/lambda/latest/dg/with-eventbridge-scheduler.html) in the *AWS Lambda Developer Guide*.

For more information about EventBridge Scheduler, including how to create schedules using the console, AWS CLI, or SDKs, see the [Amazon EventBridge Scheduler User Guide](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html).

If you still need to use EventBridge scheduled rules, see [Creating a scheduled rule (legacy) in Amazon EventBridge](eb-create-rule-schedule.md).

# Tutorial: Send events to Datadog from Amazon EventBridge
Send events to Datadog

You can use EventBridge to route [events](eb-events.md) to third-party services,such as [https://www.datadoghq.com/](https://www.datadoghq.com/).

In this tutorial, you'll use the EventBridge console to create a connection to Datadog, an [API destination](eb-api-destinations.md) that points to Datadog, and a [rule](eb-rules.md) to route events to Datadog. 

**Topics**
+ [

## Prerequisites
](#eb-dd-prereqs)
+ [

## Step 1: Create connection
](#eb-dd-create-connection)
+ [

## Step 2: Create API destination
](#eb-dd-api-destination)
+ [

## Step 3: Create rule
](#eb-dd-create-rule)
+ [

## Step 4: Test the rule
](#eb-dd-test-rule)
+ [

## Step 5: Clean up your resources
](#cleanup)

## Prerequisites


To complete this tutorial, you'll need the following resources:
+ A [Datadog account](https://www.datadoghq.com/free-datadog-trial/).
+ A [Datadog API key](https://docs.datadoghq.com/account_management/api-app-keys/).
+ An EventBridge-enabled [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) bucket.

## Step 1: Create connection


To send events to Datadog, you'll first have to establish a connection to the Datadog API.

**To create the connection**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **API destinations**.

1. Choose the **Connections** tab, and then choose **Create connection**.

1. Enter a name and description for the connection. For example, enter **Datadog** as a name, and **Datadog API Connection** as a description.

1. For **Authorization type**, choose **API key**.

1. For **API key name**, enter **DD-API-KEY**.

1. For **Value**, paste your Datadog secret API key.

1. Choose **Create**.

## Step 2: Create API destination


Now that you've created the connection, next you'll create the API destination to use as the [target](eb-targets.md) of the rule.

**To create the API Destination**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **API destinations**.

1. Choose **Create API destination**.

1. Enter a name and description for the API destination. For example, enter **DatadogAD** for the name, and **Datadog API Destination** for the description..

1. For **API destination endpoint**, enter the Datadog Logs endpoint: **https://http-intake.logs.datadoghq.com/api/v2/logs**.
**Note**  
This tutorial delivers events to Datadog Logs. You can also deliver events to Datadog using the events endpoint: `https://api.datadoghq.com/api/v1/events`.

1. For **HTTP method**, choose **POST**.

1. For **Invocation rate limit**, enter **300**.

1. For **Connection**, choose **Use an existing connection** and choose the `Datadog` connection you created in step 1.

1. Choose **Create**.

## Step 3: Create rule


Next, you'll create a rule to send events to Datadog when an Amazon S3 object is created.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, enter **DatadogRule** for the name, and **Rule to send events to Datadog for S3 object creation** for the description.

1. For **Event bus**, choose **default**.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **Other**.

1. For **Event pattern**, enter the following:

   ```
   {
     "source": ["aws.s3"]
   }
   ```

1. Choose **Next**.

1. For **Target types**, choose **EventBridge API destination**.

1. For **API destination**, choose **Use an existing API destination**, and then choose the `DatadogAD` destination you created in step 2.

1. For **Execution role**, choose **Create a new for role for this specific resource**.

1. For **Additional settings**, do the following:

   1. For **Configure target input**, choose **Input transformer** from the drop-down list.

   1. Choose **Configure input transformer**

   1. for **Sample events**, enter the following:

      ```
      {
        "detail":[]
      }
      ```

   1. For **Target input transformer** do the following:

      1. For **Input Path**, enter the following:

         ```
         {"detail":"$.detail"}
         ```

      1. For **Input Template**, enter the following:

         ```
         {"message": <detail>}
         ```

   1. Choose **Confirm.**.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Test the rule


To test your rule, create an [Amazon S3 object](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html) by uploading a file to an EventBridge-enabled bucket. The created object will be logged in the Datadog Logs console.

## Step 5: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge Connections(s)**

1. Open the [API destination page](https://console.aws.amazon.com/events/home#/apidestinations) of the EventBridge console.

1. Choose the **Connections** tab.

1. Select the Connection(s) you created.

1. Choose **Delete**.

1. Enter the name of the connection and choose **Delete**.

**To delete the EventBridge API destination(s)**

1. Open the [API destination page](https://console.aws.amazon.com/events/home#/apidestinations) of the EventBridge console.

1. Select the API destinations(s) you created.

1. Choose **Delete**.

1. Enter the name of the API destination and choose **Delete**.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

# Tutorial: Send events to Salesforce from Amazon EventBridge
Send events to Salesforce

You can use EventBridge to route [events](eb-events.md) to third-party services, such as [https://www.salesforce.com/](https://www.salesforce.com/).

In this tutorial, you'll use the EventBridge console to create a connection to Salesforce, an [API destination](eb-api-destinations.md) that points to Salesforce, and a [rule](eb-rules.md) to route events to Salesforce. 

**Topics**
+ [

## Prerequisites
](#eb-sf-prereqs)
+ [

## Step 1: Create connection
](#eb-sf-create-connection)
+ [

## Step 2: Create API destination
](#eb-dd-api-destination)
+ [

## Step 3: Create rule
](#eb-dd-create-rule)
+ [

## Step 4: Test the rule
](#eb-dd-test-rule)
+ [

## Step 5: Clean up your resources
](#cleanup)

## Prerequisites


To complete this tutorial, you'll need the following resources:
+ A [Salesforce account](https://login.salesforce.com/).
+ A [Salesforce connected app](https://help.salesforce.com/s/articleView?id=sf.connected_app_create_basics.htm).
+ A [Salesforce security token](https://help.salesforce.com/s/articleView?id=sf.user_security_token.htm).
+ A [Salesforce custom platform event](https://developer.salesforce.com/docs/atlas.en-us.234.0.platform_events.meta/platform_events/platform_events_define.htm).
+ An EventBridge-enabled [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) bucket.

## Step 1: Create connection


To send events to Salesforce, you'll first have to establish a connection to the Salesforce API.

**To create the connection**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **API destinations**.

1. Choose the **Connections** tab, and then choose **Create connection**.

1. Enter a name and description for the connection. For example, enter **Salesforce** as a name, and **Salesforce API Connection** as a description.

1. For **Destination type**, choose **Partners** and for **Partner Destinations**, select Salesforce from the drop-down list.

1. For **Authorization endpoint**, enter one of these:
   + If you're using a production org, enter **https://*MyDomainName*.my.salesforce.com./services/oauth2/token**
   + If you're using a sandbox without enhanced domains, enter **https://*MyDomainName*--*SandboxName*.my. salesforce.com/services /oauth2/token**
   + If you're using a sandbox with enhanced domains, enter **https://*MyDomainName*--* SandboxName*.sandbox.my.salesforce.com/services/oauth2/token**

1. For **HTTP method**, choose **POST** from the drop-down list.

1. For **Client ID**, enter the client ID from your Salesforce connected app.

1. For **Client secret**, enter the client secret from your Salesforce connected app.

1. For **OAuth Http Parameters**, enter the following key/value pair:    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/eventbridge/latest/userguide/eb-tutorial-salesforce.html)

1. Choose **Create**.

## Step 2: Create API destination


Now that you've created the connection, next you'll create the API destination to use as the [target](eb-targets.md) of the rule.

**To create the API Destination**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **API destinations**.

1. Choose **Create API destination**.

1. Enter a name and description for the API destination. For example, enter **SalesforceAD** for the name, and **Salesforce API Destination** for the description..

1. For **API destination endpoint**, enter **https://*MyDomainName*.my.salesforce.com/services/data/v54.0/sobjects/*MyEvent\$1\$1e*** where **Myevent\$1\$1e** is the platform event where you want to send information.

1. For **HTTP method**, choose **POST** from the drop-down list.

1. For **Invocation rate limit**, enter **300**.

1. For **Connection**, choose **Use an existing connection** and choose the `Salesforce` connection you created in step 1.

1. Choose **Create**.

## Step 3: Create rule


Next, you'll create a rule to send events to Salesforce when an Amazon S3 object is created.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, enter **SalesforceRule** for the name, and **Rule to send events to Salesforce for S3 object creation** for the description.

1. For **Event bus**, choose **default**.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **Other**.

1. For **Event pattern**, enter the following:

   ```
   {
     "source": ["aws.s3"]
   }
   ```

1. Choose **Next**.

1. For **Target types**, choose **EventBridge API destination**.

1. For **API destination**, choose **Use an existing API destination**, and then choose the `SalesforceAD` destination you created in step 2.

1. For **Execution role**, choose **Create a new for role for this specific resource**.

1. For **Additional settings**, do the following:

   1. For **Configure target input**, choose **Input transformer** from the drop-down list.

   1. Choose **Configure input transformer**

   1. for **Sample events**, enter the following:

      ```
      {
        "detail":[]
      }
      ```

   1. For **Target input transformer** do the following:

      1. For **Input Path**, enter the following:

         ```
         {"detail":"$.detail"}
         ```

      1. For **Input Template**, enter the following:

         ```
         {"message": <detail>}
         ```

   1. Choose **Confirm.**.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Test the rule


To test your rule, create an [Amazon S3 object](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html) by uploading a file to an EventBridge-enabled bucket. The information about the created object will be sent to the Salesforce platform event.

## Step 5: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge Connections(s)**

1. Open the [API destination page](https://console.aws.amazon.com/events/home#/apidestinations) of the EventBridge console.

1. Choose the **Connections** tab.

1. Select the Connection(s) you created.

1. Choose **Delete**.

1. Enter the name of the connection and choose **Delete**.

**To delete the EventBridge API destination(s)**

1. Open the [API destination page](https://console.aws.amazon.com/events/home#/apidestinations) of the EventBridge console.

1. Select the API destinations(s) you created.

1. Choose **Delete**.

1. Enter the name of the API destination and choose **Delete**.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.

# Tutorial: Send events to Zendesk from Amazon EventBridge
Send events to Zendesk

You can use EventBridge to route [events](eb-events.md) to third-party services like [https://www.zendesk.com/](https://www.zendesk.com/).

In this tutorial, you'll use the EventBridge console to create a connection to Zendesk, an [API destination](eb-api-destinations.md) that points to Zendesk, and a [rule](eb-rules.md) to route events to Zendesk. 

**Topics**
+ [

## Prerequisites
](#eb-zd-prereqs)
+ [

## Step 1: Create connection
](#eb-zd-create-connection)
+ [

## Step 2: Create API destination
](#eb-zd-api-destination)
+ [

## Step 3: Create rule
](#eb-zd-create-rule)
+ [

## Step 4: Test the rule
](#eb-zd-test-rule)
+ [

## Step 5: Clean up your resources
](#cleanup)

## Prerequisites


To complete this tutorial, you'll need the following resources:
+ A [Zendesk account](https://www.zendesk.com/register/#step-1).
+ An EventBridge-enabled [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) bucket.

## Step 1: Create connection


To send events to Zendesk, you'll first have to establish a connection to the Zendesk API.

**To create the connection**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **API destinations**.

1. Choose the **Connections** tab, and then choose **Create connection**.

1. Enter a name and description for the connection. For example, enter **Zendesk** for the name, and **Connection to Zendesk API** for the description.

1. For **Authorization type**, choose **Basic (Username/Password)**.

1. For **Username**, enter your Zendesk username.

1. For **Password**, enter your Zendesk password.

1. Choose **Create**.

## Step 2: Create API destination


Now that you've created the connection, you'll next create the API destination to use as the [target](eb-targets.md) of the rule.

**To create the API Destination**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **API destinations**.

1. Choose **Create API destination**.

1. Enter a name and description for the API destination. For example, enter **ZendeskAD** for the name, and **Zendesk API destination** for the description.

1. For **API destination endpoint**, enter **https://*your-subdomain*.zendesk.com/api/v2/tickets.json**, where *your-subdomain* is the subdomain associated with your Zendesk account.

1. For **HTTP method**, choose **POST**.

1. For **Invocation rate limit**, enter **10**.

1. For **Connection**, choose **Use an existing connection** and choose the `Zendesk` connection you created in step 1.

1. Choose **Create**.

## Step 3: Create rule


Next, create a rule to send events to Zendesk when an Amazon S3 object is created.

**To create a rule**

1. Open the Amazon EventBridge console at [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/).

1. In the navigation pane, choose **Rules**.

1. Choose **Create rule**.

1. Enter a name and description for the rule. For example, enter **ZendeskRule** for the name, and **Rule to send events to Zendesk when S3 objects are created** for the description.

1. For **Event bus**, choose **default**.

1. For **Rule type**, choose **Rule with an event pattern**.

1. Choose **Next**.

1. For **Event source**, choose **Other**.

1. For **Event pattern**, enter the following:

   ```
   {
     "source": ["aws.s3"]
   }
   ```

1. Choose **Next**.

1. For **Target types**, choose **EventBridge API destination**.

1. For **API destination**, choose **Use an existing API destination**, and then choose the `ZendeskAD` destination you created in step 2.

1. For **Execution role**, choose **Create a new for role for this specific resource**.

1. For **Additional settings**, do the following:

   1. For **Configure target input**, choose **Input transformer** from the drop-down list.

   1. Choose **Configure input transformer**

   1. for **Sample events**, enter the following:

      ```
      {
        "detail":[]
      }
      ```

   1. For **Target input transformer** do the following:

      1. For **Input Path**, enter the following:

         ```
         {"detail":"$.detail"}
         ```

      1. For **Input Template**, enter the following:

         ```
         {"message": <detail>}
         ```

   1. Choose **Confirm.**.

1. Choose **Next**.

1. Choose **Next**.

1. Review the details of the rule and choose **Create rule**.

## Step 4: Test the rule


To test your rule, create an [Amazon S3 object](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html) by uploading a file to an EventBridge-enabled bucket. When the event matches the rule, EventBridge will call the [Zendesk Create Ticket API](https://developer.zendesk.com/rest_api/docs/support/tickets#create-ticket). The new ticket will appear in the Zendesk dashboard.

## Step 5: Clean up your resources


You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

**To delete the EventBridge Connections(s)**

1. Open the [API destination page](https://console.aws.amazon.com/events/home#/apidestinations) of the EventBridge console.

1. Choose the **Connections** tab.

1. Select the Connection(s) you created.

1. Choose **Delete**.

1. Enter the name of the connection and choose **Delete**.

**To delete the EventBridge API destination(s)**

1. Open the [API destination page](https://console.aws.amazon.com/events/home#/apidestinations) of the EventBridge console.

1. Select the API destinations(s) you created.

1. Choose **Delete**.

1. Enter the name of the API destination and choose **Delete**.

**To delete the EventBridge rule(s)**

1. Open the [Rules page](https://console.aws.amazon.com/events/home#/rules) of the EventBridge console.

1. Select the rule(s) that you created.

1. Choose **Delete**.

1. Choose **Delete**.