Search code examples
amazon-web-servicesaws-lambdaamazon-kinesisamazon-cloudwatchlogsamazon-mq

Python Lambda function to capture AWS cloud watch logs of AWS MQ and send to kinesis


I got an Python script from put_records() only accepts keyword arguments in Kinesis boto3 Python API which will load json files to kinesis stream.

My architecture is something like this

In AWS console, I have created a Lambda function with the above code added to it.Lambda Function

How will I integrate or tell my lambda function that it wakes up every one minute. Should I need to add the capture messages via Cloud-watch events. If so how..?

I did get this solution form below link.

Python Script:-

import time
import boto3
import stomp

kinesis_client = boto3.client('kinesis')


class Listener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)

    def on_message(self, headers, message):
        print('received a message "%s"' % message)
        kinesis_client.put_record(
            StreamName='inter-lambda',
            Data=u'{}\r\n'.format(message).encode('utf-8'),
            PartitionKey='0'
        )


def handler(event, context):
    conn = stomp.Connection(host_and_ports=[('localhost', 61616)])
    conn.set_listener('', Listener(conn))
    conn.start()
    conn.connect(login='user', passcode='pass')
    conn.subscribe(destination='A.B.C.D', ack='auto')
    print('Waiting for messages...')
    time.sleep(10)
    conn.close()
    return ''

https://github.com/aws-samples/amazonmq-invoke-aws-lambda


Solution

  • You can schedule Lambda functions to run using CloudWatch Events.

    Another alternative may be to subscribe to the log events and deliver them to Lambda.