Skip to content

Commit db260b0

Browse files
authored
feat(scheduler-targets-alpha): SageMakerStartPipelineExecution Target (#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*
1 parent 40ffe2b commit db260b0

17 files changed

+35609
-0
lines changed

packages/@aws-cdk/aws-scheduler-targets-alpha/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
22
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
33

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

67
module.exports = baseConfig;

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

+24
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The following targets are supported:
3434
8. `targets.KinesisStreamPutRecord`: [Put a record to an Amazon Kinesis Data Streams](#put-a-record-to-an-amazon-kinesis-data-streams)
3535
9. `targets.KinesisDataFirehosePutRecord`: [Put a record to a Kinesis Data Firehose](#put-a-record-to-a-kinesis-data-firehose)
3636
10. `targets.CodePipelineStartPipelineExecution`: [Start a CodePipeline execution](#start-a-codepipeline-execution)
37+
11. `targets.SageMakerStartPipelineExecution`: [Start a SageMaker pipeline execution](#start-a-sagemaker-pipeline-execution)
3738

3839
## Invoke a Lambda function
3940

@@ -289,3 +290,26 @@ new Schedule(this, 'Schedule', {
289290
target: new targets.CodePipelineStartPipelineExecution(pipeline),
290291
});
291292
```
293+
294+
## Start a SageMaker pipeline execution
295+
296+
Use the `SageMakerStartPipelineExecution` target to start a new execution for a SageMaker pipeline.
297+
298+
The code snippet below creates an event rule with a SageMaker pipeline as target which is
299+
called every hour by Event Bridge Scheduler.
300+
301+
```ts
302+
import * as sagemaker from 'aws-cdk-lib/aws-sagemaker';
303+
304+
declare const pipeline: sagemaker.IPipeline;
305+
306+
new Schedule(this, 'Schedule', {
307+
schedule: ScheduleExpression.rate(Duration.minutes(60)),
308+
target: new targets.SageMakerStartPipelineExecution(pipeline, {
309+
pipelineParameterList: [{
310+
name: 'parameter-name',
311+
value: 'parameter-value',
312+
}],
313+
}),
314+
});
315+
```

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './inspector-start-assessment-run';
55
export * from './kinesis-data-firehose-put-record';
66
export * from './kinesis-stream-put-record';
77
export * from './lambda-invoke';
8+
export * from './sage-maker-start-pipeline-execution';
89
export * from './sns-publish';
910
export * from './sqs-send-message';
1011
export * from './stepfunctions-start-execution';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { ISchedule, IScheduleTarget, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha';
2+
import { Names } from 'aws-cdk-lib';
3+
import { IRole } from 'aws-cdk-lib/aws-iam';
4+
import { IPipeline } from 'aws-cdk-lib/aws-sagemaker';
5+
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6+
import { sameEnvDimension } from './util';
7+
8+
/**
9+
* Properties for a pipeline parameter
10+
*/
11+
export interface SageMakerPipelineParameter {
12+
/**
13+
* Name of parameter to start execution of a SageMaker Model Building Pipeline.
14+
*/
15+
readonly name: string;
16+
17+
/**
18+
* Value of parameter to start execution of a SageMaker Model Building Pipeline.
19+
*/
20+
readonly value: string;
21+
}
22+
23+
/**
24+
* Properties for a SageMaker Target
25+
*/
26+
export interface SageMakerStartPipelineExecutionProps extends ScheduleTargetBaseProps {
27+
/**
28+
* List of parameter names and values to use when executing the SageMaker Model Building Pipeline.
29+
*
30+
* The length must be between 0 and 200.
31+
*
32+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-scheduler-schedule-sagemakerpipelineparameters.html#cfn-scheduler-schedule-sagemakerpipelineparameters-pipelineparameterlist
33+
*
34+
* @default - no pipeline parameter list
35+
*/
36+
readonly pipelineParameterList?: SageMakerPipelineParameter[];
37+
}
38+
39+
/**
40+
* Use a SageMaker pipeline as a target for AWS EventBridge Scheduler.
41+
*/
42+
export class SageMakerStartPipelineExecution extends ScheduleTargetBase implements IScheduleTarget {
43+
constructor(
44+
private readonly pipeline: IPipeline,
45+
private readonly props: SageMakerStartPipelineExecutionProps = {},
46+
) {
47+
super(props, pipeline.pipelineArn);
48+
49+
if (props.pipelineParameterList !== undefined && props.pipelineParameterList.length > 200) {
50+
throw new Error(`pipelineParameterList length must be between 0 and 200, got ${props.pipelineParameterList.length}`);
51+
}
52+
}
53+
54+
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
55+
if (!sameEnvDimension(this.pipeline.stack.region, schedule.env.region)) {
56+
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.`);
57+
}
58+
59+
if (!sameEnvDimension(this.pipeline.stack.account, schedule.env.account)) {
60+
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.`);
61+
}
62+
63+
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.pipeline.stack.account)) {
64+
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.`);
65+
}
66+
67+
this.pipeline.grantStartPipelineExecution(role);
68+
}
69+
70+
protected bindBaseTargetConfig(schedule: ISchedule): ScheduleTargetConfig {
71+
const sageMakerPipelineParameters = this.props.pipelineParameterList ? {
72+
pipelineParameterList: this.props.pipelineParameterList.map(param => {
73+
return {
74+
name: param.name,
75+
value: param.value,
76+
};
77+
}),
78+
} : undefined;
79+
return {
80+
...super.bindBaseTargetConfig(schedule),
81+
sageMakerPipelineParameters,
82+
};
83+
}
84+
}

0 commit comments

Comments
 (0)