handle-record-update: triggered when DB is changed; streams the changes over the websocket
importjsonimportboto3db=boto3.resource('dynamodb')
table=db.Table('WebSocketManager')
apigatewaymanagementapi=boto3.client(
'apigatewaymanagementapi',
endpoint_url='BROADCAST_ADDRESS_FOR_THE_SOCKET'
)
deflambda_handler(event, context):
# records = event['Records']# eventName = records[0]['eventName']# # Return if it's not a modification or insertion operation# if eventName != 'MODIFY' and eventName != 'INSERT':# return {}# Get the update fields# name = records[0]['dynamodb']['NewImage']['name']['S']# zip = records[0]['dynamodb']['NewImage']['zip']['N']# Get the existing connection idsconnections=table.scan()
forconnectioninconnections['Items']:
connectionId=connection['ConnectionId']
apigatewaymanagementapi.post_to_connection(
# Data = name + "|" + zip,Data='changed',
ConnectionId=connectionId
)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}