0
votes

This is a blueprint from AWS.

import base64

print('Loading function')


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

    for record in event['records']:
        print(record['recordId'])
        payload = base64.b64decode(record['data']).decode('utf-8')

        # Do custom processing on the payload here

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

    print('Successfully processed {} records.'.format(len(event['records'])))

    return {'records': output}

When I test my delivery stream with such data:

1{ 
2    "TICKER_SYMBOL": "QXZ",
3    "SECTOR": "HEALTHCARE",
4    "CHANGE": -0.05,
5    "PRICE": 84.51
6}

it is loaded as it as to S3. Now, I want to modify the lambda function to do some transformations. For example, I want to add another field to the json before uploading the event to s3.

"NEW-PRICE": {PRICE-CHANGE}

perhaps using another function:

def find_newprice(price, change):
    return price-change

new_price = findnewprice(payload["PRICE"],payload["CHANGE"])

However, I am not sure how I can add this new field to my payloadjson or the final output that is passed on to S3. What's the correct syntax to achieve this?