-3

I have created python script which is uploaded as a zip file in AWS Lambda function with stompy libraries bundled in them.

Logs for python 2.7:-

Response:
null

Request ID:
"c334839f-ee46-11e8-8970-612f1dc92e41"

Function Logs:
START RequestId: c334839f-ee46-11e8-8970-612f1dc92e41 Version: $LATEST
CONNECTION Started
CONNECTION established
CONNECTION Subscribed
[WARNING]   2018-11-22T11:07:12.798Z    c334839f-ee46-11e8-8970-612f1dc92e41    Unknown response frame type: '' (frame length was 3)
END RequestId: c334839f-ee46-11e8-8970-612f1dc92e41
REPORT RequestId: c334839f-ee46-11e8-8970-612f1dc92e41  Duration: 10027.75 ms   Billed Duration: 10100 ms   Memory Size: 128 MB Max Memory Used: 30 MB

My Code:-

import time
import boto3
import stomp

kinesis_client = boto3.client('kinesis')


class Listener(stomp.ConnectionListener):
    msg_list = []
    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='Purchasing',
            Data=u'{}\r\n'.format(message).encode('utf-8'),
            PartitionKey='0'
        )


def lambda_handler(event, context):
    conn = stomp.Connection(host_and_ports=[('b-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 8162)])
    lst = Listener()
    conn.set_listener('Listener', Listener())
    conn.start()
    conn.connect(login='test_mq', passcode='test_mq')
    conn.subscribe(destination='/queue/Purchasing', id='b-4714-4441-8166-47aae158281a', ack='auto')
    message = lst.msg_list
    print('Waiting for messages "%s"' % message)
    time.sleep(10)
    conn.disconnect()
    return ''

I am not sure why my message is not showing up in my output,instead it always shows up "Response: null".

  • Please post your code as text, not a screenshot. We might need to copy and paste your code to our own text editors to reproduce your problem, and we can't do that from a graphic. – cdarke Nov 20 '18 at 14:46
  • @cdarke I have attached the code –  Nov 20 '18 at 14:49

1 Answers1

1

EDIT: As pointed by @Petesh, the issue comes from stompy(external library), which hasn't been ported to Python3.

If you check the source code, you can find this:

except socket.timeout, exc:

which is invalid syntax for python3+

If you run your Lambdas in python3.6/3.7 environment, the syntax is invalid.

The issue might go away if you choose python 2.7, but you will also have to adjust your code, libraries, etc.

AlexK
  • 1,380
  • 10
  • 17
  • Can you clarify that the issue is with the `stompy` module, which has not been ported to python3 - it's not clear from your answer that this is what's the trigger of the issue. – Anya Shenanigans Nov 20 '18 at 15:11
  • @AlexK.. Thank you.. What adjustments I need to do to my code if I choose python 2.7. Moreover, How or where can I get python3+ stompy libraries –  Nov 20 '18 at 15:39
  • @AlexK.. Also I have updated the error logs for python 2.7 –  Nov 20 '18 at 15:44
  • This https://pypi.org/project/stomp.py/#description looks like a alternative for python 3 – AlexK Nov 20 '18 at 15:45
  • @AlexK I have used your link https://pypi.org/project/stomp.py/#files and downloaded tar.gz file. But it doesn't have stomp.py files in it –  Nov 20 '18 at 16:20
  • Please just execute this `pip install stomp.py`. Also I can confirm there are files when you extract the .tar – AlexK Nov 20 '18 at 16:22
  • Thank you @AlexK.. I got ew error now. I am really sorry to trouble you along. As a new bee, I need some guidance from you. –  Nov 20 '18 at 16:42
  • Well, you have to actually mention what error you got – AlexK Nov 20 '18 at 16:43
  • I have attached them in the question:- File "/var/task/one_purchasing.py", line 23, in lambda_handler conn.set_listener('', Listener(conn)) TypeError: object() takes no parameters –  Nov 20 '18 at 16:45
  • Well, unfortunately, my work hours are over, so I will have to go, but here you are trying to inherit a class `ConnectionListener` which takes no arguments and you are passing some to it. You can just read the docs in the link I have posted above and follow them and you should be fine. – AlexK Nov 20 '18 at 16:52
  • @AlexK I have updated the question with latest error message. Please suggest. Is there any possible way that we can connect. Its been week I am trying to understand and close this:( –  Nov 21 '18 at 10:09
  • Increase the lambda execution time, it is set to 3 seconds by default and you make the handler sleep for 10 seconds. – AlexK Nov 21 '18 at 10:16
  • @AlexK I have changed the timeout value to 30sec and observed errors. Errors:- START RequestId: eedddb94-ed7a-11e8-9e5a-ab744fd1f9da Version: $LATEST Waiting for messages... 'StompConnection11' object has no attribute 'close': AttributeError Traceback (most recent call last): File "/var/task/one_purchasing.py", line 29, in lambda_handler conn.close() AttributeError: 'StompConnection11' object has no attribute 'close' END RequestId: eedddb94-ed7a-11e8 REPORT RequestId: eedddb94-ed7a-11e8 Duration: 10021.06 ms Billed Duration: 10100 ms Memory Size: 128 MB Max Memory Used: 31 –  Nov 21 '18 at 10:49
  • @AlexK . Please suggest –  Nov 21 '18 at 11:14
  • Use `.disconnect` rather than `.close` – AlexK Nov 21 '18 at 11:19
  • Thanka a lot @AlexK That worked..:) Another quick question-- How will my cloudwatch events capture my messages and send it to my Lambda function –  Nov 21 '18 at 11:31
  • Cloudwatch isn't the tool for that task. Check SQS and SNS. But this is a different question already – AlexK Nov 21 '18 at 11:37
  • I am using AWS MQ , so I have no option other than Cloudwatch to capture messages and send it across to Lambda then to Kinesis. https://github.com/aws-samples/amazonmq-invoke-aws-lambda My architecture –  Nov 21 '18 at 11:50
  • As I said this is no longer in the scope of the question. I cannot help you much with that. – AlexK Nov 21 '18 at 11:51
  • @AlexK-- I feel my code is having issues. Its not able to pick messages sent from MQ –  Nov 22 '18 at 10:04
  • @AlexK I am receiving "Response: null". I am not sure where exactly is the error. Please suggest –  Nov 22 '18 at 11:18
  • Post another question with different tags and different topic/description. I cannot help you out with STOMP protocol. I answered to the question you have posted here. – AlexK Nov 22 '18 at 11:29
  • @AlexK-- I posted a new question here https://stackoverflow.com/questions/53430332/result-returned-by-aws-lambda-python-function-execution-in-null please suggest –  Nov 22 '18 at 11:47
  • @AlexK- Could you please suggest anything here plz--https://stackoverflow.com/questions/53445120/aws-kinesis-put-record-from-one-aws-account-to-different-account –  Nov 26 '18 at 10:21