Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-events-targets): centralized module for cloudwatch event targets #2343

Merged
merged 13 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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