Skip to content

Commit

Permalink
feat(scheduler-targets-alpha): SageMakerStartPipelineExecution Targ…
Browse files Browse the repository at this point in the history
…et (#28927)

This PR adds SageMakerStartPipelineExecution Target for EventBridge Scheduler.

Closes #27457

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
go-to-k authored Feb 13, 2024
1 parent 40ffe2b commit db260b0
Show file tree
Hide file tree
Showing 17 changed files with 35,609 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';

baseConfig.rules['import/no-extraneous-dependencies'] = ['error', { devDependencies: true, peerDependencies: true }];
baseConfig.rules['@aws-cdk/invalid-cfn-imports'] = 'off';

module.exports = baseConfig;
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following targets are supported:
8. `targets.KinesisStreamPutRecord`: [Put a record to an Amazon Kinesis Data Streams](#put-a-record-to-an-amazon-kinesis-data-streams)
9. `targets.KinesisDataFirehosePutRecord`: [Put a record to a Kinesis Data Firehose](#put-a-record-to-a-kinesis-data-firehose)
10. `targets.CodePipelineStartPipelineExecution`: [Start a CodePipeline execution](#start-a-codepipeline-execution)
11. `targets.SageMakerStartPipelineExecution`: [Start a SageMaker pipeline execution](#start-a-sagemaker-pipeline-execution)

## Invoke a Lambda function

Expand Down Expand Up @@ -289,3 +290,26 @@ new Schedule(this, 'Schedule', {
target: new targets.CodePipelineStartPipelineExecution(pipeline),
});
```

## Start a SageMaker pipeline execution

Use the `SageMakerStartPipelineExecution` target to start a new execution for a SageMaker pipeline.

The code snippet below creates an event rule with a SageMaker pipeline as target which is
called every hour by Event Bridge Scheduler.

```ts
import * as sagemaker from 'aws-cdk-lib/aws-sagemaker';

declare const pipeline: sagemaker.IPipeline;

new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(Duration.minutes(60)),
target: new targets.SageMakerStartPipelineExecution(pipeline, {
pipelineParameterList: [{
name: 'parameter-name',
value: 'parameter-value',
}],
}),
});
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './inspector-start-assessment-run';
export * from './kinesis-data-firehose-put-record';
export * from './kinesis-stream-put-record';
export * from './lambda-invoke';
export * from './sage-maker-start-pipeline-execution';
export * from './sns-publish';
export * from './sqs-send-message';
export * from './stepfunctions-start-execution';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { ISchedule, IScheduleTarget, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha';
import { Names } from 'aws-cdk-lib';
import { IRole } from 'aws-cdk-lib/aws-iam';
import { IPipeline } from 'aws-cdk-lib/aws-sagemaker';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

/**
* Properties for a pipeline parameter
*/
export interface SageMakerPipelineParameter {
/**
* Name of parameter to start execution of a SageMaker Model Building Pipeline.
*/
readonly name: string;

/**
* Value of parameter to start execution of a SageMaker Model Building Pipeline.
*/
readonly value: string;
}

/**
* Properties for a SageMaker Target
*/
export interface SageMakerStartPipelineExecutionProps extends ScheduleTargetBaseProps {
/**
* List of parameter names and values to use when executing the SageMaker Model Building Pipeline.
*
* The length must be between 0 and 200.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-scheduler-schedule-sagemakerpipelineparameters.html#cfn-scheduler-schedule-sagemakerpipelineparameters-pipelineparameterlist
*
* @default - no pipeline parameter list
*/
readonly pipelineParameterList?: SageMakerPipelineParameter[];
}

/**
* Use a SageMaker pipeline as a target for AWS EventBridge Scheduler.
*/
export class SageMakerStartPipelineExecution extends ScheduleTargetBase implements IScheduleTarget {
constructor(
private readonly pipeline: IPipeline,
private readonly props: SageMakerStartPipelineExecutionProps = {},
) {
super(props, pipeline.pipelineArn);

if (props.pipelineParameterList !== undefined && props.pipelineParameterList.length > 200) {
throw new Error(`pipelineParameterList length must be between 0 and 200, got ${props.pipelineParameterList.length}`);
}
}

protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
if (!sameEnvDimension(this.pipeline.stack.region, schedule.env.region)) {
throw new Error(`Cannot assign pipeline in region ${this.pipeline.stack.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the pipeline must be in the same region.`);
}

if (!sameEnvDimension(this.pipeline.stack.account, schedule.env.account)) {
throw new Error(`Cannot assign pipeline in account ${this.pipeline.stack.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the pipeline must be in the same account.`);
}

if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.pipeline.stack.account)) {
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.pipeline.node)} in account ${this.pipeline.stack.account}. Both the target and the execution role must be in the same account.`);
}

this.pipeline.grantStartPipelineExecution(role);
}

protected bindBaseTargetConfig(schedule: ISchedule): ScheduleTargetConfig {
const sageMakerPipelineParameters = this.props.pipelineParameterList ? {
pipelineParameterList: this.props.pipelineParameterList.map(param => {
return {
name: param.name,
value: param.value,
};
}),
} : undefined;
return {
...super.bindBaseTargetConfig(schedule),
sageMakerPipelineParameters,
};
}
}
Loading

0 comments on commit db260b0

Please sign in to comment.