AWS_EN

Use EventBridge to execute Step Functions periodically

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

Diagram of using EventBridge to execute Step Functions periodically.

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

awstut-fa/120 at main · awstut-an-r/awstut-fa
Contribute to awstut-an-r/awstut-fa development by creating an account on GitHub.

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.

Creating an Amazon EventBridge rule that runs on a schedule - Amazon EventBridge
Learn how to create an EventBridge rule that runs on a regular schedule.

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.

Detail of Step Functions 1.

It has been successfully created.
You can see that the state machine consists of two Lambda functions.

Check the EventBridge rules.

Detail of EventBridge 1.
Detail of EventBridge 2.

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.

Detail of Step Functions 2.
Detail of Step Functions 3.

It was executed automatically and completed successfully.

Wait for a while longer.

Detail of Step Functions 4.

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.

タイトルとURLをコピーしました