@@ -427,6 +427,13 @@ export interface BucketAttributes {
427
427
* @default - it's assumed the bucket is in the same region as the scope it's being imported into
428
428
*/
429
429
readonly region ?: string ;
430
+
431
+ /**
432
+ * The role to be used by the notifications handler
433
+ *
434
+ * @default - a new role will be created.
435
+ */
436
+ readonly notificationsHandlerRole ?: iam . IRole ;
430
437
}
431
438
432
439
/**
@@ -484,14 +491,12 @@ export abstract class BucketBase extends Resource implements IBucket {
484
491
*/
485
492
protected abstract disallowPublicAccess ?: boolean ;
486
493
487
- private readonly notifications : BucketNotifications ;
494
+ private notifications ?: BucketNotifications ;
495
+
496
+ protected notificationsHandlerRole ?: iam . IRole ;
488
497
489
498
constructor ( scope : Construct , id : string , props : ResourceProps = { } ) {
490
499
super ( scope , id , props ) ;
491
-
492
- // defines a BucketNotifications construct. Notice that an actual resource will only
493
- // be added if there are notifications added, so we don't need to condition this.
494
- this . notifications = new BucketNotifications ( this , 'Notifications' , { bucket : this } ) ;
495
500
}
496
501
497
502
/**
@@ -836,7 +841,17 @@ export abstract class BucketBase extends Resource implements IBucket {
836
841
* https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
837
842
*/
838
843
public addEventNotification ( event : EventType , dest : IBucketNotificationDestination , ...filters : NotificationKeyFilter [ ] ) {
839
- this . notifications . addNotification ( event , dest , ...filters ) ;
844
+ this . withNotifications ( notifications => notifications . addNotification ( event , dest , ...filters ) ) ;
845
+ }
846
+
847
+ private withNotifications ( cb : ( notifications : BucketNotifications ) => void ) {
848
+ if ( ! this . notifications ) {
849
+ this . notifications = new BucketNotifications ( this , 'Notifications' , {
850
+ bucket : this ,
851
+ handlerRole : this . notificationsHandlerRole ,
852
+ } ) ;
853
+ }
854
+ cb ( this . notifications ) ;
840
855
}
841
856
842
857
/**
@@ -1459,6 +1474,13 @@ export interface BucketProps {
1459
1474
*/
1460
1475
readonly transferAcceleration ?: boolean ;
1461
1476
1477
+ /**
1478
+ * The role to be used by the notifications handler
1479
+ *
1480
+ * @default - a new role will be created.
1481
+ */
1482
+ readonly notificationsHandlerRole ?: iam . IRole ;
1483
+
1462
1484
/**
1463
1485
* Inteligent Tiering Configurations
1464
1486
*
@@ -1542,6 +1564,7 @@ export class Bucket extends BucketBase {
1542
1564
public policy ?: BucketPolicy = undefined ;
1543
1565
protected autoCreatePolicy = false ;
1544
1566
protected disallowPublicAccess = false ;
1567
+ protected notificationsHandlerRole = attrs . notificationsHandlerRole ;
1545
1568
1546
1569
/**
1547
1570
* Exports this bucket from the stack.
@@ -1629,6 +1652,8 @@ export class Bucket extends BucketBase {
1629
1652
physicalName : props . bucketName ,
1630
1653
} ) ;
1631
1654
1655
+ this . notificationsHandlerRole = props . notificationsHandlerRole ;
1656
+
1632
1657
const { bucketEncryption, encryptionKey } = this . parseEncryption ( props ) ;
1633
1658
1634
1659
Bucket . validateBucketName ( this . physicalName ) ;
0 commit comments