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

7
  • Not sure it's the cause of your problem but according to 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. Commented Nov 22, 2018 at 12:09
  • @EricDarchis I tested, it does not make a difference, the strange warning remains. Commented Nov 22, 2018 at 13:20
  • @EricDarchis .. Yes.. it did not make any difference.. Commented Nov 22, 2018 at 13:30
  • @Tinku regarding your edit to my answer, you probably want to change that in your question too :-) Commented Nov 23, 2018 at 10:26
  • @RobBricheno-- I have posted different question- stackoverflow.com/questions/53445120/… Commented Nov 23, 2018 at 10:42

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

8 Comments

I have updated response and code as well. MQ does has messages in it. Updated with proper URL of MQ with credentials. Please suggest
@Tinku did you try wait=True?
Yeah.. Even I received "errorType": "ConnectFailedException", error
How can I proceed from here. As I have given all the right values which I have
@Tinku check latest edit, I solved it, you need to use SSL :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.