def remove_s3_bucket_lambda_event()

in templates/aws-cloudfront-waf/source/custom_resource/custom-resource.py [0:0]


def remove_s3_bucket_lambda_event(log, bucket_name, lambda_function_arn, lambda_log_partition_function_arn):
    if lambda_function_arn != None:
        log.info("[remove_s3_bucket_lambda_event] Start")

        s3_client = boto3.client('s3')
        try:
            new_conf = {}
            notification_conf = s3_client.get_bucket_notification_configuration(Bucket=bucket_name)

            log.info("[remove_s3_bucket_lambda_event]notification_conf:\n %s"
                    % (notification_conf))

            if 'TopicConfigurations' in notification_conf:
                new_conf['TopicConfigurations'] = notification_conf['TopicConfigurations']
            if 'QueueConfigurations' in notification_conf:
                new_conf['QueueConfigurations'] = notification_conf['QueueConfigurations']

            if 'LambdaFunctionConfigurations' in notification_conf:
                new_conf['LambdaFunctionConfigurations'] = []
                for lfc in notification_conf['LambdaFunctionConfigurations']:
                    if lfc['LambdaFunctionArn'] == lambda_function_arn or  \
                       lfc['LambdaFunctionArn'] == lambda_log_partition_function_arn:
                        log.info("[remove_s3_bucket_lambda_event]%s match found, continue." %lfc['LambdaFunctionArn'])
                        continue  # remove all references
                    else:
                        new_conf['LambdaFunctionConfigurations'].append(lfc)
                        log.info("[remove_s3_bucket_lambda_event]lfc appended: %s" %lfc)

            log.info("[remove_s3_bucket_lambda_event]new_conf:\n %s"
                     % (new_conf))
                        
            s3_client.put_bucket_notification_configuration(Bucket=bucket_name, NotificationConfiguration=new_conf)

        except Exception as error:
            log.error(
                "Failed to remove S3 Bucket lambda event. Check if the bucket still exists, you own it and has proper access policy.")
            log.error(str(error))

        log.info("[remove_s3_bucket_lambda_event] End")