Triggering AWS Step Functions workflows on a daily basis
TypeScriptTo trigger AWS Step Functions workflows on a daily basis, we will use the following AWS resources:
-
AWS Step Functions State Machine: This is the workflow that you want to execute daily. We define the states and transitions in the workflow using the Amazon States Language.
-
AWS CloudWatch Events Rule: This service allows us to define a schedule (using a cron expression) to trigger events automatically. Here, it will trigger your Step Functions State Machine daily.
-
AWS CloudWatch Events Target: When the scheduled event occurs, we identify the AWS Step Functions State Machine as the target for the event to trigger.
Let’s create a Pulumi program in TypeScript that deploys these resources:
import * as aws from '@pulumi/aws'; const stateMachineRole = new aws.iam.Role("stateMachineRole", { assumeRolePolicy: JSON.stringify({ Version: "2012-10-17", Statement: [ { Action: "sts:AssumeRole", Effect: "Allow", Principal: { Service: "states.amazonaws.com", }, }, ], }), }); const stateMachinePolicyAttachment = new aws.iam.PolicyAttachment("stateMachinePolicyAttachment", { roles: [stateMachineRole.name], policyArn: "arn:aws:iam::aws:policy/service-role/AWSLambdaRole", // Minimal policy for demonstration purposes }); const stateMachine = new aws.sfn.StateMachine("dailyWorkflow", { roleArn: stateMachineRole.arn, definition: `{ "Comment": "A simple minimal example of the Amazon States Language using a wait state", "StartAt": "HelloWorld", "States": { "HelloWorld": { "Type": "Pass", "Result": "Hello World!", "End": true } } }`, }); const dailyRule = new aws.cloudwatch.EventRule("dailyRule", { scheduleExpression: "cron(0 0 * * ? *)", // runs every day at midnight }); const eventTarget = new aws.cloudwatch.EventTarget("stateMachineEventTarget", { rule: dailyRule.name, arn: stateMachine.arn, }); const permission = new aws.lambda.Permission("stateMachineInvokePermission", { action: "lambda:InvokeFunction", principal: "events.amazonaws.com", sourceArn: dailyRule.arn, function: stateMachine.arn, }); // Export the name of the state machine and the ARN of the CloudWatch Events Rule export const stateMachineName = stateMachine.name; export const cloudWatchRuleArn = dailyRule.arn;
In this program:
-
We define an IAM role (
stateMachineRole
) for the AWS Step Functions State Machine with a trust relationship policy that allows Step Functions to assume this role. -
We attach the
AWSLambdaRole
policy to the role. This is a managed policy that grants permissions to invoke AWS Lambda functions. In a real-world scenario, you may need to create a custom policy that grants specific permissions required by your state machine's tasks. -
We create a State Machine (
stateMachine
) with a simple definition that includes onePass
state. This is for illustrative purposes, and in a real-world application, you would replace this with the definition of your workflow. -
We define a CloudWatch Events Rule (
dailyRule
) with a cron expression to trigger the event every day at midnight. -
We create an Event Target (
eventTarget
) to link the CloudWatch Events Rule to the State Machine. -
We set up a Lambda Permission (
permission
) to allow CloudWatch Events to invoke the Step Functions State Machine.
The
export
statements at the end of the program are used to output the names of the resources we’ve created, so you can easily locate them in the AWS console or through the CLI.After deploying this code using
pulumi up
, Pulumi provisions these resources in your AWS account, and you'll have a Step Functions State Machine triggered once every day at midnight.-