Schedule run of Step Function

In a previous post here I described how to start a server and set it’s DNS record. In this case, I had to manually go and start execution for the Step Function. What if you wanted this to happen every day or every weekday at a certain fixed time. For this to happen we can set up a CloudWatch Event Rule to start our function.

AWS CloudWatch Events allows us to schedule automated actions on a schedule using cron or rate expressions. CloudWatch Events use targets to specify the action and the targets can be different AWS services. EC2 instances, Step Function, Lambda Functions and Batch jobs are among some of the targets.

In its simplest form, the CloudWatch Event triggers on a specified cron or rate schedule and triggers the defined target. Let us take a look at the procedure for setting this up.

Procedure:

Select CloudWatch service from the console and navigate to ‘Events’and click on ‘Rules’ to bring you to a second screen similar to this.

Click on ‘Create rule’ to reach the next screen where you will select ‘Schedule’ and enter the cron expression corresponding to the time when you want to start execution of your function.

Create rule

The cron is similar to what you would use on a Linux server so it is easy to figure it out.

0 10 * * ? * implies Run everyday at 10:00 am UTC 

Once you enter the time, the console will show you the next 10 Trigger dates corresponding to your cron expression. This is a good way to validate your expression. If the triggers look incorrect, make adjustments to your cron expression.

cron expression

It is best to review the AWS documentation for the cron expression so as not to make a mistake. All scheduled events use UTC time zone. I have provided the link below in this post to read more on the cron expressions.

Next click on ‘Add target’ on the right-hand side of the screen. Select ‘Step Function state machine’ from the drop down and let AWS create the role for you if you don’t have one already. Click ‘Configure details’ button and enter a name for the rule and description.

select function

That’s all there is to be done, now at the scheduled time, your function will execute and start the server for you.

If for any reason you wanted to disable the schedule you could do that from the Rules page. Select your rule and from the ‘Action’ select enable or disable as needed.

Further reading:

 

 

Leave a Reply