Search code examples
python-3.xamazon-web-servicesaws-lambdaamazon-kinesisamazon-kinesis-firehose

aws firehose lambda function invocation gives wrong output strcuture format


When i insert a data object to aws firhose stream using a put operation it works fine .As lambda function is enabled on my firehose stream .hence a lambda function is invoked but gives me a output structure response error :

"errorMessage":"Invalid output structure: Please check your function and make sure the processed records contain valid result status of Dropped, Ok, or ProcessingFailed."

so now i have created my lambda function like this way to make the correct output strcuture :

import base64
import json

print('Loading function')

def lambda_handler(event, context):
    output=[]
    print('event'+str(event))
    for record in event['records']:
        payload = base64.b64decode(record['data'])
        print('payload'+str(payload))
        payload=base64.b64encode(payload)
        output_record={
            'recordId':record['recordId'],
            'result': 'Ok',
             'data':  base64.b64encode(json.dumps('hello'))
        }
    output.append(output_record)
    return { 'records': output }

Now i am getting the follwing eror on encoding the 'data' field as

"errorMessage": "a bytes-like object is required, not 'str'",

and if i change the 'hello' to bytes like b'hello' then i get the following error :

 "errorMessage": "Object of type bytes is not JSON serializable",

Solution

  • import json import base64 import gzip import io import zlib

    def lambda_handler(event, context): output = []

    for record in event['records']:
        payload = base64.b64decode(record['data']).decode('utf-8')
        output_record = {
            'recordId': record['recordId'],
            'result': 'Ok',
            'data': base64.b64encode(payload.encode('utf-8')).decode('utf-8')
        }
        output.append(output_record)
    
    return {'records': output}