问题
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
回答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()
来源:https://stackoverflow.com/questions/53430332/result-returned-by-aws-lambda-python-function-execution-in-null