Skip to content

Commit

Permalink
feat(scheduler-targets): add CodePipeline as target for scheduler (#2…
Browse files Browse the repository at this point in the history
…7799)

Closes #27449

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
daschaa authored Dec 18, 2023
1 parent 3e6f10d commit 8c44f32
Show file tree
Hide file tree
Showing 16 changed files with 38,218 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The following targets are supported:
7. `targets.InspectorStartAssessmentRun`: [Start an Amazon Inspector assessment run](#start-an-amazon-inspector-assessment-run)
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)

## Invoke a Lambda function

Expand Down Expand Up @@ -270,3 +271,21 @@ new Schedule(this, 'Schedule', {
}),
});
```

## Start a CodePipeline execution

Use the `CodePipelineStartPipelineExecution` target to start a new execution for a CodePipeline pipeline.

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

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

declare const pipeline: codepipeline.Pipeline;

new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(Duration.minutes(60)),
target: new targets.CodePipelineStartPipelineExecution(pipeline),
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
import { Names } from 'aws-cdk-lib';
import { IPipeline } from 'aws-cdk-lib/aws-codepipeline';
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

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

protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
const region = this.pipeline.env.region ?? '';
const account = this.pipeline.env.account ?? '';

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

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

if (this.props.role && !sameEnvDimension(this.props.role.env.account, 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 ${account}. Both the target and the execution role must be in the same account.`);
}

role.addToPrincipalPolicy(new PolicyStatement({
actions: ['codepipeline:StartPipelineExecution'],
resources: [this.pipeline.pipelineArn],
}));
}
}
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
@@ -1,4 +1,5 @@
export * from './codebuild-start-build';
export * from './codepipeline-start-pipeline-execution';
export * from './event-bridge-put-events';
export * from './inspector-start-assessment-run';
export * from './kinesis-data-firehose-put-record';
Expand Down
Loading

0 comments on commit 8c44f32

Please sign in to comment.