@@ -511,21 +511,6 @@ exports.createS3EventSource = function (s3, lambda, bucket, prefix, functionName
511
511
if ( err ) {
512
512
callback ( err ) ;
513
513
} else {
514
- // now create the event source mapping
515
- var newEventConfiguration = {
516
- Events : [ 's3:ObjectCreated:*' , ] ,
517
- LambdaFunctionArn : functionArn ,
518
- Filter : {
519
- Key : {
520
- FilterRules : [ {
521
- Name : 'prefix' ,
522
- Value : prefix + "/"
523
- } ]
524
- }
525
- } ,
526
- Id : "LambdaRedshiftLoaderEventSource-" + uuid . v4 ( )
527
- } ;
528
-
529
514
// add the notification configuration to the
530
515
// set of existing lambda configurations
531
516
if ( ! currentNotificationConfiguration ) {
@@ -535,24 +520,54 @@ exports.createS3EventSource = function (s3, lambda, bucket, prefix, functionName
535
520
currentNotificationConfiguration . LambdaFunctionConfigurations = [ ] ;
536
521
}
537
522
538
- currentNotificationConfiguration . LambdaFunctionConfigurations . push ( newEventConfiguration ) ;
539
-
540
- // push the function event trigger
541
- // configurations back into S3
542
- var params = {
543
- Bucket : bucket ,
544
- NotificationConfiguration : currentNotificationConfiguration
545
- } ;
546
-
547
- s3 . putBucketNotificationConfiguration ( params , function ( err , data ) {
548
- if ( err ) {
549
- console . log ( this . httpResponse . body . toString ( ) ) ;
550
- console . log ( err ) ;
551
- callback ( err ) ;
552
- } else {
553
- callback ( ) ;
523
+ configAlreadyExists = false ;
524
+
525
+ // Let's check our configs to see if we already have one that exists
526
+ currentNotificationConfiguration . LambdaFunctionConfigurations . forEach ( config => {
527
+ if ( config . Filter . Key . FilterRules [ 0 ] . Value == prefix + '/' ) {
528
+ console . log ( 'Skipping creation of notification config because it already exists' ) ;
529
+ configAlreadyExists = true ;
554
530
}
555
531
} ) ;
532
+
533
+ // Create a new notification config
534
+ if ( ! configAlreadyExists ) {
535
+ console . log ( 'Creating notification configuration' ) ;
536
+
537
+ // now create the event source mapping
538
+ var newEventConfiguration = {
539
+ Events : [ 's3:ObjectCreated:*' , ] ,
540
+ LambdaFunctionArn : functionArn ,
541
+ Filter : {
542
+ Key : {
543
+ FilterRules : [ {
544
+ Name : 'prefix' ,
545
+ Value : prefix + "/"
546
+ } ]
547
+ }
548
+ } ,
549
+ Id : "LambdaRedshiftLoaderEventSource-" + uuid . v4 ( )
550
+ } ;
551
+
552
+ currentNotificationConfiguration . LambdaFunctionConfigurations . push ( newEventConfiguration ) ;
553
+
554
+ // push the function event trigger
555
+ // configurations back into S3
556
+ var params = {
557
+ Bucket : bucket ,
558
+ NotificationConfiguration : currentNotificationConfiguration
559
+ } ;
560
+
561
+ s3 . putBucketNotificationConfiguration ( params , function ( err , data ) {
562
+ if ( err ) {
563
+ console . log ( this . httpResponse . body . toString ( ) ) ;
564
+ console . log ( err ) ;
565
+ callback ( err ) ;
566
+ } else {
567
+ callback ( ) ;
568
+ }
569
+ } ) ;
570
+ }
556
571
}
557
572
} ) ;
558
573
}
0 commit comments