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:
- Open to the Amazon S3 console, and then choose Create bucket.
- For Bucket name, enter a name.
- Choose Create bucket.
Activate Amazon S3 Event Notification with EventBridge
Complete the following steps:
- Open the Amazon S3 console, and then choose Buckets.
- Choose the bucket that you want to activate events for, and then choose Properties.
- In the Amazon EventBridge section, choose Edit.
- In the Send notifications to Amazon EventBridge for all events in this bucket section, choose On.
- 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:
- Open the EventBridge console, and then choose Create rule.
- In Name, enter a name for your rule.
- For Rule type, choose Rule with an event pattern, and then choose Next.
- 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.
- 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.
- On the Configure tags - optional page, choose Next.
- 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.