Skip to content

Commit

Permalink
feat(aws-events-targets): centralized module for cloudwatch event tar…
Browse files Browse the repository at this point in the history
…gets (#2343)

Start moving all implementations of `IEventRuleTarget` into a centralized
module to adhere with the new AWS Construct Library guideline for integrations.

This change includes the implementation of the following event rule targets:

- `targets.SnsTopic`
- `targets.CodeBuildProject`

BREAKING CHANGE: `sns.Topic` no longer implements `IEventRuleTarget`. Use `@aws-cdk/aws-events-targets.SnsTopic` instead.
* `codebuild.Project` no longer implements `IEventRuleTarget`. Use `@aws-cdk/aws-events-targets.CodeBuildProject`
  • Loading branch information
Elad Ben-Israel committed Apr 22, 2019
1 parent 4112c84 commit 1069938
Show file tree
Hide file tree
Showing 34 changed files with 5,800 additions and 1,929 deletions.
8 changes: 5 additions & 3 deletions packages/@aws-cdk/aws-codebuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ by events via an event rule.

### Using Project as an event target

The `Project` construct implements the `IEventRuleTarget` interface. This means
that it can be used as a target for event rules:
The `@aws-cdk/aws-events-targets.CodeBuildProject` allows using an AWS CodeBuild
project as a AWS CloudWatch event rule target:

```ts
// start build when a commit is pushed
codeCommitRepository.onCommit('OnCommit', project);
const targets = require('@aws-cdk/aws-events-targets');

codeCommitRepository.onCommit('OnCommit', new targets.CodeBuildProject(project));
```

### Using Project as an event source
Expand Down
26 changes: 1 addition & 25 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const CODEPIPELINE_TYPE = 'CODEPIPELINE';
const S3_BUCKET_ENV = 'SCRIPT_S3_BUCKET';
const S3_KEY_ENV = 'SCRIPT_S3_KEY';

export interface IProject extends IResource, events.IEventRuleTarget, iam.IGrantable {
export interface IProject extends IResource, iam.IGrantable {
/** The ARN of this Project. */
readonly projectArn: string;

Expand Down Expand Up @@ -167,9 +167,6 @@ export abstract class ProjectBase extends Resource implements IProject {
/** The IAM service Role of this Project. */
public abstract readonly role?: iam.IRole;

/** A role used by CloudWatch events to trigger a build */
private eventsRole?: iam.Role;

public abstract export(): ProjectImportProps;

/**
Expand Down Expand Up @@ -347,27 +344,6 @@ export abstract class ProjectBase extends Resource implements IProject {
...props,
});
}

/**
* Allows using build projects as event rule targets.
*/
public asEventRuleTarget(_ruleArn: string, _ruleId: string): events.EventRuleTargetProps {
if (!this.eventsRole) {
this.eventsRole = new iam.Role(this, 'EventsRole', {
assumedBy: new iam.ServicePrincipal('events.amazonaws.com')
});

this.eventsRole.addToPolicy(new iam.PolicyStatement()
.addAction('codebuild:StartBuild')
.addResource(this.projectArn));
}

return {
id: this.node.id,
arn: this.projectArn,
roleArn: this.eventsRole.roleArn,
};
}
}

class ImportedProject extends ProjectBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,6 @@
},
"MyTopic86869434": {
"Type": "AWS::SNS::Topic"
},
"MyTopicPolicy12A5EC17": {
"Type": "AWS::SNS::TopicPolicy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "sns:Publish",
"Effect": "Allow",
"Principal": {
"Service": {
"Fn::Join": [
"",
[
"events.",
{
"Ref": "AWS::URLSuffix"
}
]
]
}
},
"Resource": {
"Ref": "MyTopic86869434"
},
"Sid": "0"
}
],
"Version": "2012-10-17"
},
"Topics": [
{
"Ref": "MyTopic86869434"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ const stack = new cdk.Stack(app, 'aws-cdk-codecommit-events');
const repo = new codecommit.Repository(stack, 'Repo', { repositoryName: 'aws-cdk-codecommit-events' });
const topic = new sns.Topic(stack, 'MyTopic');

repo.onReferenceCreated('OnReferenceCreated', topic);
// we can't use @aws-cdk/aws-events-targets.SnsTopic here because it will
// create a cyclic dependency with codebuild, so we just fake it
repo.onReferenceCreated('OnReferenceCreated', {
asEventRuleTarget: () => ({
arn: topic.topicArn,
id: 'MyTopic'
})
});

app.run();
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codepipeline-actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"devDependencies": {
"@aws-cdk/assert": "^0.28.0",
"@aws-cdk/aws-cloudtrail": "^0.28.0",
"@aws-cdk/aws-events-targets": "^0.28.0",
"@types/lodash": "^4.14.123",
"cdk-build-tools": "^0.28.0",
"cdk-integ-tools": "^0.28.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import targets = require('@aws-cdk/aws-events-targets');
import sns = require('@aws-cdk/aws-sns');
import cdk = require('@aws-cdk/cdk');
import cpactions = require('../lib');
Expand Down Expand Up @@ -42,17 +43,17 @@ pipeline.addStage({

const topic = new sns.Topic(stack, 'MyTopic');

pipeline.onStateChange('OnPipelineStateChange').addTarget(topic, {
pipeline.onStateChange('OnPipelineStateChange').addTarget(new targets.SnsTopic(topic), {
textTemplate: 'Pipeline <pipeline> changed state to <state>',
pathsMap: {
pipeline: '$.detail.pipeline',
state: '$.detail.state'
}
});

sourceStage.onStateChange('OnSourceStateChange', topic);
sourceStage.onStateChange('OnSourceStateChange', new targets.SnsTopic(topic));

sourceAction.onStateChange('OnActionStateChange', topic).addEventPattern({
sourceAction.onStateChange('OnActionStateChange', new targets.SnsTopic(topic)).addEventPattern({
detail: { state: [ 'STARTED' ] }
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, haveResource, haveResourceLike, SynthUtils } from '@aws-cdk/ass
import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import targets = require('@aws-cdk/aws-events-targets');
import lambda = require('@aws-cdk/aws-lambda');
import s3 = require('@aws-cdk/aws-s3');
import sns = require('@aws-cdk/aws-sns');
Expand Down Expand Up @@ -188,7 +189,7 @@ export = {
],
});

pipeline.onStateChange('OnStateChange', topic, {
pipeline.onStateChange('OnStateChange', new targets.SnsTopic(topic), {
description: 'desc',
scheduleExpression: 'now',
eventPattern: {
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-events-targets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.js
tsconfig.json
tslint.json
*.js.map
*.d.ts
*.generated.ts
dist
lib/generated/resources.ts
.jsii

.LAST_BUILD
.nyc_output
coverage
.nycrc
.LAST_PACKAGE
*.snk
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-events-targets/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Don't include original .ts files when doing `npm pack`
*.ts
!*.d.ts
coverage
.nyc_output
*.tgz

dist
.LAST_PACKAGE
.LAST_BUILD
!*.js

# Include .jsii
!.jsii

*.snk
Loading

0 comments on commit 1069938

Please sign in to comment.