""" * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import print_function import base64 import boto3 import os from jsonschema import validate import json from ast import literal_eval print('Loading function') s3 = boto3.client('s3') bucket_name = os.environ['BUCKET_VALIDATION_DEFS'] file_name = os.environ['BUCKET_VALIDATION_FILE'] def get_validation_schema(): f = open('payload_validation.json',) return json.load(f) def handler(event, context): output = [] # fetch the json payload schema to validate the data against schema = get_validation_schema() print('Schema {}'.format(schema)) for record in event['records']: payload = base64.b64decode(record['data']) # print(payload) payload1 = payload.decode("utf-8") # print(payload1) jsonData = json.loads(payload1) # validate it try: validate(instance=jsonData, schema=schema) print("Record valid {}".format(jsonData)) output_record = { 'recordId': record['recordId'], 'result': 'Ok', 'data': record['data'] } except Exception as e: print("Record invalid {}".format(jsonData)) print(e) # record is not valid format and will be stored in the 'processingfailed' prefix in S3 bucket output_record = { 'recordId': record['recordId'], 'result': 'ProcessingFailed', 'data': record['data'] } output.append(output_record) print('Successfully processed {} records.'.format(len(event['records']))) return {'records': output}