【问题标题】:Result returned by AWS Lambda python function execution in ""Null""AWS Lambda python 函数在“Null”中执行返回的结果
【发布时间】:2018-11-22 11:47:11
【问题描述】:

我有一个连接到 AWS MQ 并收集消息的 python 脚本。我所有的连接都完美对齐,执行结果是成功的。但是我的函数执行返回的结果是“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 获取消息

MQ URL 消息屏幕截图

MQ home page

Messages under queue

【问题讨论】:

  • 不确定这是您问题的原因,但根据github.com/jasonrbriggs/stomp.py/issues/33,您应该将端口设为字符串而不是int , '61614')]),以使奇怪的警告消失。很想知道它是否有所作为。
  • @EricDarchis 我测试过,没有区别,奇怪的警告仍然存在。
  • @EricDarchis .. 是的.. 没有任何区别..
  • @Tinku 关于你对我的回答的编辑,你可能也想在你的问题中改变它:-)
  • @RobBricheno--我发布了不同的问题-stackoverflow.com/questions/53445120/…

标签: python python-3.x python-2.7 amazon-web-services amazon-mq


【解决方案1】:

responsenull 的原因是因为你永远不会返回值,你只是return。与运行此命令的响应相同:

def lambda_handler(event, context):
    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()

【讨论】:

  • 我也更新了响应和代码。 MQ 中确实有消息。使用带有凭据的 MQ 的正确 URL 进行了更新。请建议
  • @Tinku 你试过wait=True吗?
  • 是的.. 即使我收到“errorType”:“ConnectFailedException”,错误
  • 我该如何从这里开始。因为我已经给出了我拥有的所有正确的价值观
  • @Tinku 检查最新编辑,我解决了,你需要使用 SSL :-)
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 2020-10-06
  • 2018-12-29
  • 2019-05-03
  • 2017-02-17
  • 2022-06-17
  • 1970-01-01
相关资源
最近更新 更多