0

I have a python script which connects to AWS MQ and collect message. All my connections are perfectly aligned and Execution result is success. But result returned by my function execution is "null". Updated error logs:-

    {
  "errorType": "ConnectFailedException",
  "stackTrace": [
    "  File \"/var/task/one_purchasing.py\", line 21, in lambda_handler\n    conn.connect(login='test_mq', passcode='test_secure_mq',wait=True)\n",
    "  File \"/var/task/stomp/connect.py\", line 164, in connect\n    Protocol11.connect(self, *args, **kwargs)\n",
    "  File \"/var/task/stomp/protocol.py\", line 340, in connect\n    self.transport.wait_for_connection()\n",
    "  File \"/var/task/stomp/transport.py\", line 327, in wait_for_connection\n    raise exception.ConnectFailedException()\n"
  ]
}

Updated Python Lambda function:-

import time
import boto3
import stomp
import json

kinesis_client = boto3.client('kinesis')


class Listener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)
        kinesis_client.put_record(
            StreamName='OnePurchasing',
            Data=u'{}\r\n'.format(message).encode('utf-8'),
            PartitionKey='0'
        )

    def on_message(self, headers, message):
        print('received a message "%s"' % message)
def lambda_handler(event, context):
    conn = stomp.Connection(host_and_ports=[('b-fa99d7c5-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
    lst = Listener()
    conn.set_listener('Listener', lst)
    conn.set_ssl(for_hosts=[('b-fa99d7c5-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
    conn.start()
    print('CONNECTION Started')
    conn.connect(login='test_mq', passcode='test_secure_mq',wait=True)
    print('CONNECTION established')
    conn.subscribe(destination='/queue/OnePurchasing', id=1, ack='auto')
    print('CONNECTION Subscribed')
    time.sleep(10)
    conn.disconnect()
    return

Could anyone tell me how can I debug more to get the message from MQ

MQ URL message screen shot

MQ home page

Messages under queue

  • Not sure it's the cause of your problem but according to https://github.com/jasonrbriggs/stomp.py/issues/33 you should make your port a string rather than int `, '61614')])` to make the strange warning disappear. Curious to see if it makes a difference. – Eric Darchis Nov 22 '18 at 12:09
  • @EricDarchis I tested, it does not make a difference, the strange warning remains. – Rob Bricheno Nov 22 '18 at 13:20
  • @EricDarchis .. Yes.. it did not make any difference.. –  Nov 22 '18 at 13:30
  • @Tinku regarding your edit to my answer, you probably want to change that in your question too :-) – Rob Bricheno Nov 23 '18 at 10:26
  • @RobBricheno-- I have posted different question- https://stackoverflow.com/questions/53445120/python-aws-kinesis-put-record-from-one-user-to-different-user –  Nov 23 '18 at 10:42
  • @RobBricheno-- I have updated my questions with my findings. Is there any URL where I can get this info. Please suggest.https://stackoverflow.com/questions/53445120/aws-kinesis-put-record-from-one-user-to-different-user –  Nov 23 '18 at 14:12
  • @RobBricheno- I have sent JSON format message in my MQ, its not considering complete JSON message as a single message instead each value inside JSON as a message. Moreover- in my Kinesis stream, I am unable to see the JSON format message:( –  Nov 29 '18 at 11:36

1 Answers1

1

The reason that response is null is because you don't ever return a value, you just return. It's the same response as if you run this:

def lambda_handler(event, context):
    return

You probably want to return something, like the example built in to lambda:

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Regarding the rest of your problem, it looks like you are never receivng a message at all. You see from the web console of your MQ instance if there are messages on the queue, if a message has been consumed, and so on.

All the examples I have seen involve using the wait=True option e.g. conn.connect(wait=True) so you should try adding that to your conn.connect unless there's a good reason you aren't using it.

Edit: I tested this, I don't think you are ever establishing a connection. If you add wait=True then you will probably see that the connection fails with a ConnectFailedException as mine did. This is probably the first thing to debug.

Edit 2: I solved it, you need to use SSL for your connection to the AWS MQ instance as follows:

conn = stomp.Connection(host_and_ports=[('b-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
lst = Listener()
conn.set_listener('Listener', lst)
conn.set_ssl(for_hosts=[('b-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
conn.start()
Rob Bricheno
  • 4,467
  • 15
  • 29
  • I have updated response and code as well. MQ does has messages in it. Updated with proper URL of MQ with credentials. Please suggest –  Nov 22 '18 at 13:34
  • @Tinku did you try `wait=True`? – Rob Bricheno Nov 22 '18 at 13:40
  • Yeah.. Even I received "errorType": "ConnectFailedException", error –  Nov 22 '18 at 13:42
  • How can I proceed from here. As I have given all the right values which I have –  Nov 22 '18 at 13:46
  • @Tinku check latest edit, I solved it, you need to use SSL :-) – Rob Bricheno Nov 22 '18 at 14:00
  • I have updated the code now, How can I put these messages into Kinesis..? Final suggestion. –  Nov 22 '18 at 14:08
  • @Tinku You'd probably add some code to the `on_message` function of your listener. But really that's quite a different, new question. Why don't you try a few things, and if you hit a problem post a new question about it? – Rob Bricheno Nov 22 '18 at 14:39
  • Could you suggest me anything here--https://stackoverflow.com/questions/53445120/aws-kinesis-put-record-from-one-aws-account-to-different-account –  Nov 26 '18 at 10:20