“"Null"”中 AWS Lambda python 函数执行返回的结果

Result returned by AWS Lambda python function execution in ""Null""

我有一个 python 脚本连接到 AWS MQ 并收集消息。我所有的连接都完美对齐,执行结果是成功的。但是我的函数执行返回的结果是"null"。 更新的错误日志:-

    {
  "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"
  ]
}

已更新 Python Lambda 函数:-

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

谁能告诉我如何调试才能从 MQ 获取消息

MQURL消息截图

MQ home page

Messages under queue

responsenull 的原因是因为您从来没有 return 一个值,您只是 return。这与您 运行 的响应相同:

def lambda_handler(event, context):
    return

您可能想要 return 一些东西,例如 lambda 中内置的示例:

import json

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

关于您的其余问题,您似乎根本没有收到消息。你从the web console of your MQ instance看队列里有没有消息,消息有没有被消费等等。

我看到的所有示例都涉及使用 wait=True 选项,例如conn.connect(wait=True) 所以你应该尝试将它添加到你的 conn.connect 除非你有充分的理由不使用它。

编辑: 我对此进行了测试,我认为您从未建立过连接。如果您添加 wait=True 那么您可能会看到连接失败并显示 ConnectFailedException ,就像我的一样。这可能是调试的第一件事。

编辑 2: 我解决了,您需要使用 SSL 连接到 AWS MQ 实例,如下所示:

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()