Skip to content

Commit

Permalink
feat(scheduler-targets): Add step function start execution target
Browse files Browse the repository at this point in the history
  • Loading branch information
WtfJoke committed Oct 5, 2023
1 parent 495dafa commit e9cb7d0
Show file tree
Hide file tree
Showing 11 changed files with 35,016 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IScheduleTarget, ISchedule } from '@aws-cdk/aws-scheduler-alpha';
import { Names } from 'aws-cdk-lib';
import { IRole } from 'aws-cdk-lib/aws-iam';
import { IStateMachine } from 'aws-cdk-lib/aws-stepfunctions';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

/**
* Use an AWS Step function as a target for AWS EventBridge Scheduler.
*/
export class StepFunctionsStartExecution extends ScheduleTargetBase implements IScheduleTarget {
constructor(
private readonly stateMachine: IStateMachine,
private readonly props: ScheduleTargetBaseProps,
) {
super(props, stateMachine.stateMachineArn);
}

protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
const stateMachineEnv = this.stateMachine.env;
if (!sameEnvDimension(stateMachineEnv.region, schedule.env.region)) {
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.`);
}

if (!sameEnvDimension(stateMachineEnv.account, schedule.env.account)) {
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.`);
}

if (this.props.role && !sameEnvDimension(this.props.role.env.account, stateMachineEnv.account)) {
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.`);
}

this.stateMachine.grantStartExecution(role);
}
}
Loading

0 comments on commit e9cb7d0

Please sign in to comment.