Use EventBridge to execute Step Functions periodically
EventBridge can be used to periodically run the Step Functions state machine.
You can execute an AWS Step Functions state machine in response to an event pattern or on a schedule using Amazon EventBridge.
Periodically Start a State Machine Execution Using EventBridge
This page will review how to use CloudFormation to periodically execute the Step Functions state machine.
Environment
Create one Step Functions state machine and two states inside it.
Within a state, we will execute a Lambda function.
The function performs the following functions
- Lambda function 1: Obtains current date/time information.
- Lambda function 2: Calculates and returns UNIX time from the date/time information.
The runtime environment for the function is Python 3.8.
Create an EventBridge rule to execute this state machine periodically.
Specifically, set the state machine to run once every 5 minutes.
CloudFormation template files
The above configuration is built with CloudFormation.
The CloudFormation templates are placed at the following URL
https://github.com/awstut-an-r/awstut-fa/tree/main/120
Explanation of key points of template files
This page focuses on how to run the Step Functions state machine periodically.
For information on how to create a Step Functions state machine, please see the following page.
EventBridge Rules
Resources:
Rule1:
Type: AWS::Events::Rule
Properties:
Name: !Sub "${Prefix}-EventsRule"
ScheduleExpression: rate(5 minutes)
State: ENABLED
Targets:
- Arn: !Ref StateMachineArn
Id: !Ref StateMachineName
RoleArn: !GetAtt EventsRuleRole.Arn
Code language: YAML (yaml)
The ScheduleExpression property specifies when to run the state machine.
In this case, we will set it to run every 5 minutes.
You can choose either cron or rate notation for the schedule, but we will use the latter in this case.
Please refer to the following page for detailed description.
https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#RateExpressions
The Targets property specifies the target resources to run.
In this case, we will target a state machine, so we will specify its ARN and the IAM role to be described later.
Create an IAM role.
Using an EventBridge rule to periodically run the Step Functions state machine means that EventBridge will run the state machine by proxy.
Therefore, we create an IAM role for EventBridge and give it permission to run the state machine.
Resources:
EventsRuleRole:
Type: AWS::IAM::Role
DeletionPolicy: Delete
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: events.amazonaws.com
Policies:
- PolicyName: !Sub "${Prefix}-StartStateMachineExecutionPolicy"
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: states:StartExecution
Resource: !Ref StateMachineArn
Code language: YAML (yaml)
The permission to give is to specify the state machine as a resource and then allow the states:StartExecution action.
Architecting
Use CloudFormation to build this environment and check its actual behavior.
Create CloudFormation stacks and check the resources in the stacks
Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following pages.
After reviewing the resources in each stack, information on the main resources created in this case is as follows
- Step Functions state machine: fa-120
- Lambda function 1: fa-120-function-01
- Lambda function 2: fa-120-function-02
- EventBridge rule: fa-120-EventsRule
Check the resources created from the AWS Management Console.
Check the state machine.
It has been successfully created.
You can see that the state machine consists of two Lambda functions.
Check the EventBridge rules.
An EventBridge rule is created.
The content is to run the above state machine every 5 minutes.
Action Check
Now that we are ready, we keep an eye on the execution of the state machine.
After a short wait, the state machine is automatically executed.
It was executed automatically and completed successfully.
Wait for a while longer.
The state machine ran every 5 minutes.
Thus, by using the EventBridge rule, the Step Functions state machine can be executed automatically.
Summary
We have identified how to use CloudFormation to periodically run the Step Functions state machine.