Skip to content

Commit df02e4f

Browse files
committed
feat(aws-s3-notifications): add option to skip permissions for LambdaDestination
Closes #34747
1 parent 47a3ee3 commit df02e4f

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

packages/aws-cdk-lib/aws-s3-notifications/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ const fn = new lambda.Function(this, 'MyFunction', {
4444

4545
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.LambdaDestination(fn));
4646
```
47+
48+
You can also skip the creation of arranging permissions:
49+
50+
```ts
51+
new s3n.LambdaDestination(fn, { addPermissions: false });
52+
```

packages/aws-cdk-lib/aws-s3-notifications/lib/lambda.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ import * as s3 from '../../aws-s3';
55
import { CfnResource, Names, Stack } from '../../core';
66
import { ValidationError } from '../../core/lib/errors';
77

8+
/**
9+
* Options that may be provided to LambdaDestination
10+
*/
11+
export interface LambdaDestinationOptions {
12+
/** Whether or not to add Lambda Permissions.
13+
* @default true
14+
*/
15+
readonly addPermissions?: boolean;
16+
}
17+
818
/**
919
* Use a Lambda function as a bucket notification destination
1020
*/
1121
export class LambdaDestination implements s3.IBucketNotificationDestination {
12-
constructor(private readonly fn: lambda.IFunction) {
22+
constructor(private readonly fn: lambda.IFunction, private readonly options: LambdaDestinationOptions = {}) {
1323
}
1424

1525
public bind(scope: Construct, bucket: s3.IBucket): s3.BucketNotificationDestinationConfig {
@@ -20,7 +30,7 @@ export class LambdaDestination implements s3.IBucketNotificationDestination {
2030
bucket construct (Bucket ${bucket.bucketName})`, scope);
2131
}
2232

23-
if (bucket.node.tryFindChild(permissionId) === undefined) {
33+
if (this.options.addPermissions !== false && bucket.node.tryFindChild(permissionId) === undefined) {
2434
this.fn.addPermission(permissionId, {
2535
sourceAccount: Stack.of(bucket).account,
2636
principal: new iam.ServicePrincipal('s3.amazonaws.com'),

packages/aws-cdk-lib/aws-s3-notifications/test/lambda/lambda.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,20 @@ test('add multiple event notifications using a singleton function', () => {
222222
}),
223223
});
224224
});
225+
226+
test('lambda permissions are not added when addPermissions is false', () => {
227+
const stack = new Stack();
228+
const bucket = new s3.Bucket(stack, 'MyBucket');
229+
const fn = new lambda.Function(stack, 'MyFunction1', {
230+
runtime: lambda.Runtime.NODEJS_LATEST,
231+
handler: 'index.handler',
232+
code: lambda.Code.fromInline('foo'),
233+
});
234+
235+
const lambdaDestination = new s3n.LambdaDestination(fn, { addPermissions: false });
236+
237+
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, lambdaDestination, { prefix: 'v1/' });
238+
239+
// expecting one permission for each function
240+
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 0);
241+
});

0 commit comments

Comments
 (0)