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

3 minute read
1

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

Short description

You can use an EventBridge rule to activate a Step Function state machine when an object is uploaded to an 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

Resolution

Prerequisite

Make sure that you have created a Step Function for your required task.

Complete these steps:

Create a bucket in Amazon S3

  1. Navigate to the Amazon S3 console.
  2. Choose Create bucket to create the bucket where you want to store your files.
  3. Enter a bucket name and other details.
  4. Create the bucket.

Activate Amazon S3 event notification with EventBridge

S3 bucket events aren't sent to the EventBridge bus, by default. You must activate it.

  1. Navigate to the Amazon S3 console.
  2. Choose the bucket that you want to activate events for.
  3. Choose Properties.
  4. Under the Event Notifications section, choose Edit in the Amazon EventBridge subsection.
  5. Under Send notifications to Amazon EventBridge for all events in this bucket, choose On.
  6. Choose Save changes.

Configure the state machine

Create a state machine.

Create an Amazon EventBridge rule

  1. Navigate to the Amazon EventBridge console, and then choose Create rule.
  2. Enter a Name for your rule.
  3. For Event bus and Rule type, keep the default selections.
  4. Choose Next. The Build event pattern page appears.
  5. Scroll down to the Event pattern section, and complete these steps:
    • For Event source, keep the default selection of AWS Services.
    • For AWS service, choose Simple Storage Service (S3).
    • For Event type, choose Amazon S3 Event Notification.
    • Choose Specific event(s), and then choose Object created.
    • Choose Specific bucket(s) by name and enter the bucket name you created earlier.
  6. Choose Next to open the Select target(s) page.
  7. In Target 1, keep the default selection of AWS service.
  8. In the Select a target dropdown list, select Step Functions state machine.
  9. In the State machine dropdown list, select the state machine that you created earlier.
  10. Keep all the default selections on the page, and choose Next to open the Configure tags page.
  11. Choose Next again.
  12. Review the details of the rule, and then choose Create rule.

Use these troubleshooting tips

  • The changes you make to activate S3 event notifications in EventBridge take about five minutes to complete. This means that the rule don'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 same AWS Region.
AWS OFFICIAL
AWS OFFICIALUpdated 8 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 2 months ago

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

profile pictureAWS
MODERATOR
replied 2 months 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?

replied a month ago

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

profile pictureAWS
MODERATOR
replied 24 days 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 10 days ago

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

profile pictureAWS
MODERATOR
replied 9 days ago