Skip to content

Commit 5815188

Browse files
committed
Don't create notification event if it already exists
1 parent 0cef1de commit 5815188

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

common.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -511,21 +511,6 @@ exports.createS3EventSource = function (s3, lambda, bucket, prefix, functionName
511511
if (err) {
512512
callback(err);
513513
} 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-
529514
// add the notification configuration to the
530515
// set of existing lambda configurations
531516
if (!currentNotificationConfiguration) {
@@ -535,24 +520,54 @@ exports.createS3EventSource = function (s3, lambda, bucket, prefix, functionName
535520
currentNotificationConfiguration.LambdaFunctionConfigurations = [];
536521
}
537522

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;
554530
}
555531
});
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+
}
556571
}
557572
});
558573
}

0 commit comments

Comments
 (0)