Skip to content

Commit 3a87141

Browse files
authored
feat(scheduler-targets): step function start execution target (#27424)
A StepFunctionStartExecution ScheduleTarget was implemented similar to the already existing [LambdaInvoke](https://github.com/aws/aws-cdk/blob/b2a895ef285e5451e64c21e179172e998c479582/packages/%40aws-cdk/aws-scheduler-targets-alpha/lib/lambda-invoke.ts#L8) target. I've added an integration test, which will trigger an eventbridge schedule once. This schedule has a step function as a target. The step function creates a parameter with a given value, which will get verified by the test Closes #27377 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 61be7a6 commit 3a87141

15 files changed

+35577
-1
lines changed

packages/@aws-cdk/aws-scheduler-targets-alpha/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ number of supported AWS Services.
2525
The following targets are supported:
2626

2727
1. `targets.LambdaInvoke`: [Invoke an AWS Lambda function](#invoke-a-lambda-function))
28+
2. `targets.StepFunctionsStartExecution`: [Start an AWS Step Function](#start-an-aws-step-function)
2829

2930
## Invoke a Lambda function
3031

@@ -61,3 +62,43 @@ const schedule = new Schedule(this, 'Schedule', {
6162
target
6263
});
6364
```
65+
66+
## Start an AWS Step Function
67+
68+
Use the `StepFunctionsStartExecution` target to start a new execution on a StepFunction.
69+
70+
The code snippet below creates an event rule with a Step Function as a target
71+
called every hour by Event Bridge Scheduler with a custom payload.
72+
73+
```ts
74+
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
75+
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
76+
77+
const payload = {
78+
Name: "MyParameter",
79+
Value: '🌥️',
80+
};
81+
82+
const putParameterStep = new tasks.CallAwsService(this, 'PutParameter', {
83+
service: 'ssm',
84+
action: 'putParameter',
85+
iamResources: ['*'],
86+
parameters: {
87+
"Name.$": '$.Name',
88+
"Value.$": '$.Value',
89+
Type: 'String',
90+
Overwrite: true,
91+
},
92+
});
93+
94+
const stateMachine = new sfn.StateMachine(this, 'StateMachine', {
95+
definitionBody: sfn.DefinitionBody.fromChainable(putParameterStep)
96+
});
97+
98+
new Schedule(this, 'Schedule', {
99+
schedule: ScheduleExpression.rate(Duration.hours(1)),
100+
target: new targets.StepFunctionsStartExecution(stateMachine, {
101+
input: ScheduleTargetInput.fromObject(payload),
102+
}),
103+
});
104+
```
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './target';
2-
export * from './lambda-invoke';
2+
export * from './lambda-invoke';
3+
export * from './stepfunctions-start-execution';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { IScheduleTarget, ISchedule } from '@aws-cdk/aws-scheduler-alpha';
2+
import { Names } from 'aws-cdk-lib';
3+
import { IRole } from 'aws-cdk-lib/aws-iam';
4+
import { IStateMachine } from 'aws-cdk-lib/aws-stepfunctions';
5+
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6+
import { sameEnvDimension } from './util';
7+
8+
/**
9+
* Use an AWS Step function as a target for AWS EventBridge Scheduler.
10+
*/
11+
export class StepFunctionsStartExecution extends ScheduleTargetBase implements IScheduleTarget {
12+
constructor(
13+
private readonly stateMachine: IStateMachine,
14+
private readonly props: ScheduleTargetBaseProps,
15+
) {
16+
super(props, stateMachine.stateMachineArn);
17+
}
18+
19+
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20+
const stateMachineEnv = this.stateMachine.env;
21+
if (!sameEnvDimension(stateMachineEnv.region, schedule.env.region)) {
22+
throw new Error(`Cannot assign stateMachine in region ${stateMachineEnv.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the stateMachine must be in the same region.`);
23+
}
24+
25+
if (!sameEnvDimension(stateMachineEnv.account, schedule.env.account)) {
26+
throw new Error(`Cannot assign stateMachine in account ${stateMachineEnv.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the stateMachine must be in the same account.`);
27+
}
28+
29+
if (this.props.role && !sameEnvDimension(this.props.role.env.account, stateMachineEnv.account)) {
30+
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.stateMachine.node)} in account ${stateMachineEnv.account}. Both the target and the execution role must be in the same account.`);
31+
}
32+
33+
this.stateMachine.grantStartExecution(role);
34+
}
35+
}

0 commit comments

Comments
 (0)