Skip to content

Commit 8c44f32

Browse files
authored
feat(scheduler-targets): add CodePipeline as target for scheduler (#27799)
Closes #27449 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3e6f10d commit 8c44f32

16 files changed

+38218
-2
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The following targets are supported:
3333
7. `targets.InspectorStartAssessmentRun`: [Start an Amazon Inspector assessment run](#start-an-amazon-inspector-assessment-run)
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)
36+
10. `targets.CodePipelineStartPipelineExecution`: [Start a CodePipeline execution](#start-a-codepipeline-execution)
3637

3738
## Invoke a Lambda function
3839

@@ -270,3 +271,21 @@ new Schedule(this, 'Schedule', {
270271
}),
271272
});
272273
```
274+
275+
## Start a CodePipeline execution
276+
277+
Use the `CodePipelineStartPipelineExecution` target to start a new execution for a CodePipeline pipeline.
278+
279+
The code snippet below creates an event rule with a CodePipeline pipeline as target which is
280+
called every hour by Event Bridge Scheduler.
281+
282+
```ts
283+
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';
284+
285+
declare const pipeline: codepipeline.Pipeline;
286+
287+
new Schedule(this, 'Schedule', {
288+
schedule: ScheduleExpression.rate(Duration.minutes(60)),
289+
target: new targets.CodePipelineStartPipelineExecution(pipeline),
290+
});
291+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2+
import { Names } from 'aws-cdk-lib';
3+
import { IPipeline } from 'aws-cdk-lib/aws-codepipeline';
4+
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
5+
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6+
import { sameEnvDimension } from './util';
7+
8+
/**
9+
* Use an AWS CodePipeline pipeline as a target for AWS EventBridge Scheduler.
10+
*/
11+
export class CodePipelineStartPipelineExecution extends ScheduleTargetBase implements IScheduleTarget {
12+
constructor(
13+
private readonly pipeline: IPipeline,
14+
private readonly props: ScheduleTargetBaseProps = {},
15+
) {
16+
super(props, pipeline.pipelineArn);
17+
}
18+
19+
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20+
const region = this.pipeline.env.region ?? '';
21+
const account = this.pipeline.env.account ?? '';
22+
23+
if (!sameEnvDimension(region, schedule.env.region)) {
24+
throw new Error(`Cannot assign pipeline in region ${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.`);
25+
}
26+
27+
if (!sameEnvDimension(account, schedule.env.account)) {
28+
throw new Error(`Cannot assign pipeline in account ${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.`);
29+
}
30+
31+
if (this.props.role && !sameEnvDimension(this.props.role.env.account, account)) {
32+
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 ${account}. Both the target and the execution role must be in the same account.`);
33+
}
34+
35+
role.addToPrincipalPolicy(new PolicyStatement({
36+
actions: ['codepipeline:StartPipelineExecution'],
37+
resources: [this.pipeline.pipelineArn],
38+
}));
39+
}
40+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './codebuild-start-build';
2+
export * from './codepipeline-start-pipeline-execution';
23
export * from './event-bridge-put-events';
34
export * from './inspector-start-assessment-run';
45
export * from './kinesis-data-firehose-put-record';

0 commit comments

Comments
 (0)