1. Answers
  2. Scheduling AWS Step Functions to perform tasks at specific times

How do I schedule AWS Step Functions to perform tasks at specific times?

In this example, we’ll show you how to schedule AWS Step Functions to perform tasks at specific times using Amazon CloudWatch Events. Step Functions allow you to build robust, serverless workflows, and CloudWatch Events can trigger these workflows on a schedule.

Overview

We’ll create the following resources:

  1. Step Function State Machine: Defines the workflow.
  2. IAM Role: Allows the Step Function to execute.
  3. CloudWatch Event Rule: Specifies the schedule to trigger the Step Function.
  4. CloudWatch Event Target: Links the CloudWatch Event Rule to the Step Function.

Detailed Explanation

  1. Step Function State Machine: This is where we define our actual workflow, specifying states and transitions.
  2. IAM Role: An IAM Role with appropriate policies to allow Step Functions to execute.
  3. CloudWatch Event Rule: This defines the cron expression for when the Step Function should be triggered.
  4. CloudWatch Event Target: This links the CloudWatch Event Rule to our Step Function, telling CloudWatch what to do when the rule is triggered.

Example Program

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create IAM Role for Step Function
const stepFunctionsRole = new aws.iam.Role("step_functions_role", {
    name: "step_functions_role",
    assumeRolePolicy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Principal: {
                Service: "states.amazonaws.com",
            },
            Effect: "Allow",
            Sid: "",
        }],
    }),
});
// Attach a policy to the IAM Role
const stepFunctionsPolicy = new aws.iam.RolePolicy("step_functions_policy", {
    name: "step_functions_policy",
    role: stepFunctionsRole.id,
    policy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [
            {
                Action: "logs:*",
                Resource: "*",
                Effect: "Allow",
            },
            {
                Action: "states:*",
                Resource: "*",
                Effect: "Allow",
            },
        ],
    }),
});
// Define the Step Function
const myStateMachine = new aws.sfn.StateMachine("my_state_machine", {
    name: "myStepFunction",
    roleArn: stepFunctionsRole.arn,
    definition: JSON.stringify({
        Comment: "A simple Step Function example",
        StartAt: "HelloWorld",
        States: {
            HelloWorld: {
                Type: "Task",
                Resource: "arn:aws:lambda:us-west-2:123456789012:function:myFunction",
                End: true,
            },
        },
    }),
});
// Define the CloudWatch Event Rule
const everyDayAtNoon = new aws.cloudwatch.EventRule("every_day_at_noon", {
    name: "every_day_at_noon",
    description: "Fires every day at noon",
    scheduleExpression: "cron(0 12 * * ? *)",
});
// Add the Step Function as the Target for the Event Rule
const triggerStepFunction = new aws.cloudwatch.EventTarget("trigger_step_function", {
    rule: everyDayAtNoon.name,
    targetId: "StepFunctionTarget",
    arn: myStateMachine.arn,
    roleArn: stepFunctionsRole.arn,
});
export const stateMachineArn = myStateMachine.arn;
export const cloudwatchEventRule = everyDayAtNoon.name;

In this program, we’ve set up a Step Function to execute an example workflow. The workflow is scheduled to trigger every day at noon using a CloudWatch Event Rule. The CloudWatch Event Rule is linked to the Step Function via a Target. The IAM Role allows the Step Function to execute and log its activities.

With this setup, you can automate recurring tasks in your AWS environment easily using Step Functions and CloudWatch Events. Happy coding!

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up