How do I use an Amazon EventBridge rule to trigger an AWS Step Function state machine on an S3 upload?

3 minute read
2

I want to activate an AWS Step Function state machine when files are added to an Amazon Simple Storage Service (Amazon S3) bucket.

Short description

You can use an Amazon EventBridge rule to activate a Step Function state machine when an object is uploaded to an Amazon S3 bucket. You must have the following resources to get started:

  • An S3 bucket where the object is uploaded
  • A state machine that you want to run
  • An EventBridge rule to invoke the state machine when the object is uploaded to the S3 bucket
  • A Step Function for your required task

Resolution

Create a bucket in Amazon S3

Complete the following steps:

  1. Open to the Amazon S3 console, and then choose Create bucket.
  2. For Bucket name, enter a name.
  3. Choose Create bucket.

Activate Amazon S3 Event Notification with EventBridge

Complete the following steps:

  1. Open the Amazon S3 console, and then choose Buckets.
  2. Choose the bucket that you want to activate events for, and then choose Properties.
  3. In the Amazon EventBridge section, choose Edit.
  4. In the Send notifications to Amazon EventBridge for all events in this bucket section, choose On.
  5. Choose Save changes.

Configure the state machine

Open the Steps Function console, and then follow the steps to create a state machine.

Create an EventBridge rule

Complete the following steps:

  1. Open the EventBridge console, and then choose Create rule.
  2. In Name, enter a name for your rule.
  3. For Rule type, choose Rule with an event pattern, and then choose Next.
  4. On the Build event pattern page, in the Event pattern section, complete the following choices:
    For Event source, choose AWS Services.
    For AWS service, choose Simple Storage Service (S3).
    For Event type, choose Amazon S3 Event Notification.
    For Event type Specification 1, choose Specific event(s).
    Choose the Specific event(s) dropdown list, and choose Object created.
    For Event type Specification 2, choose Specific bucket(s) by name.
    For Specific bucket(s) by name, enter the name of the bucket that you created previously, and then choose Next.
  5. On the Select target(s) page, complete the following choices:
    For Target 1, choose AWS service.
    Choose the Select a target dropdown list and choose Step Functions state machine.
    Choose the State machine dropdown list, and choose the state machine that you created previously.
    Choose Next.
  6. On the Configure tags - optional page, choose Next.
  7. On the Review and create page, choose Create rule.

(Optional) Monitor events in an S3 bucket subfolder

To trigger an EventBridge rule when Event Notifications occur on a specific S3 bucket subfolder, modify the event pattern to use prefix matching.

The following example event pattern triggers an EventBridge rule when an S3 object is created in a subfolder named example-folder:

{
  "source": [
    "aws.s3"
  ],
  "detail-type": [
    "Object Created"
  ],
  "detail": {
    "bucket": {
      "name": [
        "bucket-name"
      ]
    },
    "object": {
      "key": [
        {
          "prefix": "example-folder"
        }
      ]
    }
  }
}

Note:

  • The changes made to activate S3 Event Notifications in EventBridge take about five minutes to complete. This means that the rule doesn't trigger immediately on S3 events after you activate it.
  • If the rule doesn't trigger, make sure that the S3 bucket and the event rule are in the same AWS Region.
AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago
6 Comments

It would be helpful to show a CloudFormation script in both JSON and YAML that would produce the event bridge built in the console in this example.

profile picture
replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a year ago

I have one question trying this approach: what if I want to monitor events only for a subfolder of the specified bucket in step 5? is it permitted to write 'buket-name/folder-name' as specific bucket?

profile picture
replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a year ago

Hello, If you would like to monitor events specific to a subfolder in the S3 bucket, the event pattern can be modified to include a prefix matching for the "object" field. Here is a sample event pattern to achieve the use case:

{
  "source": ["aws.s3"],
  "detail-type": ["Object Created"],
  "detail": {
    "bucket": {
      "name": ["bucket-name"]
    },
    "object": {
      "key": [{
        "prefix": "folder-name"
      }]
    }
  }
}
AWS
replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
EXPERT
replied a year ago