Skip to content

Commit 185951c

Browse files
authored
fix(s3): move notification destinations into their own module (#2659)
In accordance with new guidelines, we're centralizing cross-service integrations into their own package. In this case, centralizing S3 Notification Destinations into `@aws-cdk/aws-s3-notifications`. Fixes #2445. BREAKING CHANGE: using a Topic, Queue or Lambda as bucket notification destination now requires an integration object from the `@aws-cdk/aws-s3-notifications` package.
1 parent d640055 commit 185951c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+6224
-893
lines changed

packages/@aws-cdk/assert/jest.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { Stack } from "@aws-cdk/cdk";
22
import { SynthesizedStack } from "@aws-cdk/cx-api";
33
import { HaveResourceAssertion, ResourcePart } from "./lib/assertions/have-resource";
4+
import { MatchStyle, matchTemplate } from "./lib/assertions/match-template";
45
import { expect as ourExpect } from './lib/expect';
56

67
declare global {
78
namespace jest {
89
interface Matchers<R> {
10+
toMatchTemplate(template: any,
11+
matchStyle?: MatchStyle): R;
12+
913
toHaveResource(resourceType: string,
1014
properties?: any,
1115
comparison?: ResourcePart): R;
@@ -18,6 +22,27 @@ declare global {
1822
}
1923

2024
expect.extend({
25+
toMatchTemplate(
26+
actual: SynthesizedStack | Stack,
27+
template: any,
28+
matchStyle?: MatchStyle) {
29+
30+
const assertion = matchTemplate(template, matchStyle);
31+
const inspector = ourExpect(actual);
32+
const pass = assertion.assertUsing(inspector);
33+
if (pass) {
34+
return {
35+
pass,
36+
message: () => `Not ` + assertion.description
37+
};
38+
} else {
39+
return {
40+
pass,
41+
message: () => assertion.description
42+
};
43+
}
44+
},
45+
2146
toHaveResource(
2247
actual: SynthesizedStack | Stack,
2348
resourceType: string,

packages/@aws-cdk/assert/lib/synth-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,24 @@ export class SynthUtils {
1414
const synth = new Synthesizer();
1515
return synth.synthesize(stack, options);
1616
}
17+
18+
public static subset(stack: Stack, options: SubsetOptions): any {
19+
const template = SynthUtils.toCloudFormation(stack);
20+
if (template.Resources) {
21+
for (const [key, resource] of Object.entries(template.Resources)) {
22+
if (options.resourceTypes && !options.resourceTypes.includes((resource as any).Type)) {
23+
delete template.Resources[key];
24+
}
25+
}
26+
}
27+
28+
return template;
29+
}
30+
}
31+
32+
export interface SubsetOptions {
33+
/**
34+
* Match all resources of the given type
35+
*/
36+
resourceTypes?: string[];
1737
}

packages/@aws-cdk/aws-iam/lib/policy-document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,4 @@ export class PolicyStatement extends cdk.Token {
343343
export enum PolicyStatementEffect {
344344
Allow = 'Allow',
345345
Deny = 'Deny',
346-
}
346+
}

packages/@aws-cdk/aws-iam/lib/principals.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ export interface ServicePrincipalOpts {
131131
* @default the current Stack's region.
132132
*/
133133
readonly region?: string;
134+
135+
/**
136+
* Additional conditions to add to the Service Principal
137+
*
138+
* @default - No conditions
139+
*/
140+
readonly conditions?: { [key: string]: any };
134141
}
135142

136143
/**
@@ -146,7 +153,7 @@ export class ServicePrincipal extends PrincipalBase {
146153
Service: [
147154
new ServicePrincipalToken(this.service, this.opts).toString()
148155
]
149-
});
156+
}, this.opts.conditions);
150157
}
151158

152159
public toString() {

packages/@aws-cdk/aws-lambda-event-sources/lib/s3.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import lambda = require('@aws-cdk/aws-lambda');
22
import s3 = require('@aws-cdk/aws-s3');
3+
import notifs = require('@aws-cdk/aws-s3-notifications');
34

45
export interface S3EventSourceProps {
56
/**
@@ -27,7 +28,7 @@ export class S3EventSource implements lambda.IEventSource {
2728
public bind(target: lambda.IFunction) {
2829
const filters = this.props.filters || [];
2930
for (const event of this.props.events) {
30-
this.bucket.addEventNotification(event, target, ...filters);
31+
this.bucket.addEventNotification(event, new notifs.LambdaDestination(target), ...filters);
3132
}
3233
}
3334
}

packages/@aws-cdk/aws-lambda-event-sources/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@aws-cdk/aws-kinesis": "^0.32.0",
7070
"@aws-cdk/aws-lambda": "^0.32.0",
7171
"@aws-cdk/aws-s3": "^0.32.0",
72+
"@aws-cdk/aws-s3-notifications": "^0.32.0",
7273
"@aws-cdk/aws-sns": "^0.32.0",
7374
"@aws-cdk/aws-sqs": "^0.32.0",
7475
"@aws-cdk/cdk": "^0.32.0"
@@ -82,6 +83,7 @@
8283
"@aws-cdk/aws-kinesis": "^0.32.0",
8384
"@aws-cdk/aws-lambda": "^0.32.0",
8485
"@aws-cdk/aws-s3": "^0.32.0",
86+
"@aws-cdk/aws-s3-notifications": "^0.32.0",
8587
"@aws-cdk/aws-sns": "^0.32.0",
8688
"@aws-cdk/aws-sqs": "^0.32.0",
8789
"@aws-cdk/cdk": "^0.32.0"

packages/@aws-cdk/aws-lambda/lib/function-base.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import cloudwatch = require('@aws-cdk/aws-cloudwatch');
22
import ec2 = require('@aws-cdk/aws-ec2');
33
import iam = require('@aws-cdk/aws-iam');
44
import logs = require('@aws-cdk/aws-logs');
5-
import s3n = require('@aws-cdk/aws-s3-notifications');
6-
import cdk = require('@aws-cdk/cdk');
75
import { IResource, Resource } from '@aws-cdk/cdk';
86
import { IEventSource } from './event-source';
97
import { EventSourceMapping, EventSourceMappingOptions } from './event-source-mapping';
108
import { CfnPermission } from './lambda.generated';
119
import { Permission } from './permission';
1210

1311
export interface IFunction extends IResource, logs.ILogSubscriptionDestination,
14-
s3n.IBucketNotificationDestination, ec2.IConnectable, iam.IGrantable {
12+
ec2.IConnectable, iam.IGrantable {
1513

1614
/**
1715
* Logical ID of this Function.
@@ -270,31 +268,6 @@ export abstract class FunctionBase extends Resource implements IFunction {
270268
return { arn: this.functionArn };
271269
}
272270

273-
/**
274-
* Allows this Lambda to be used as a destination for bucket notifications.
275-
* Use `bucket.onEvent(lambda)` to subscribe.
276-
*/
277-
public asBucketNotificationDestination(bucketArn: string, bucketId: string): s3n.BucketNotificationDestinationProps {
278-
const permissionId = `AllowBucketNotificationsFrom${bucketId}`;
279-
if (!this.node.tryFindChild(permissionId)) {
280-
this.addPermission(permissionId, {
281-
sourceAccount: this.node.stack.accountId,
282-
principal: new iam.ServicePrincipal('s3.amazonaws.com'),
283-
sourceArn: bucketArn,
284-
});
285-
}
286-
287-
// if we have a permission resource for this relationship, add it as a dependency
288-
// to the bucket notifications resource, so it will be created first.
289-
const permission = this.node.tryFindChild(permissionId) as cdk.CfnResource;
290-
291-
return {
292-
type: s3n.BucketNotificationDestinationType.Lambda,
293-
arn: this.functionArn,
294-
dependencies: permission ? [ permission ] : undefined
295-
};
296-
}
297-
298271
/**
299272
* Adds an event source to this function.
300273
*

packages/@aws-cdk/aws-lambda/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
"@aws-cdk/aws-iam": "^0.32.0",
8888
"@aws-cdk/aws-logs": "^0.32.0",
8989
"@aws-cdk/aws-s3": "^0.32.0",
90-
"@aws-cdk/aws-s3-notifications": "^0.32.0",
9190
"@aws-cdk/aws-sqs": "^0.32.0",
9291
"@aws-cdk/cdk": "^0.32.0",
9392
"@aws-cdk/cx-api": "^0.32.0"
@@ -101,7 +100,6 @@
101100
"@aws-cdk/aws-iam": "^0.32.0",
102101
"@aws-cdk/aws-logs": "^0.32.0",
103102
"@aws-cdk/aws-s3": "^0.32.0",
104-
"@aws-cdk/aws-s3-notifications": "^0.32.0",
105103
"@aws-cdk/aws-sqs": "^0.32.0",
106104
"@aws-cdk/cdk": "^0.32.0",
107105
"@aws-cdk/cx-api": "^0.32.0"
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
## S3 Bucket Notifications API
1+
## S3 Bucket Notifications Destinations
22

3-
This module includes the API that constructs should implement in order to be
4-
able to be used as destinations for bucket notifications.
5-
6-
To implement the `IBucketNotificationDestination`, a construct should implement
7-
a method `asBucketNotificationDestination(bucketArn, bucketId)` which registers
8-
this resource as a destination for bucket notifications _for the specified
9-
bucket_ and returns the ARN of the destination and it's type.
3+
This module includes integration classes for using Topics, Queues or Lambdas
4+
as S3 Notification Destinations.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export * from './destination';
1+
export * from './sqs';
2+
export * from './sns';
3+
export * from './lambda';

0 commit comments

Comments
 (0)