AWS Lambda python函数执行返回的结果将以"Null"表示. [英] Result returned by AWS Lambda python function execution in ""Null""

查看:152
本文介绍了AWS Lambda python函数执行返回的结果将以"Null"表示.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

更新的Python Lambda函数:-

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

谁能告诉我如何调试更多信息以从MQ中获取消息

Could anyone tell me how can I debug more to get the message from MQ

MQ URL消息屏幕截图

MQ URL message screen shot

MQ主页

队列中的消息

推荐答案

responsenull的原因是因为您从未返回任何值,而只是返回了return.就像您运行此命令一样:

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

您可能想要返回一些信息,例如lambda内置的示例:

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.

我看到的所有示例都涉及使用wait=True选项,例如conn.connect(wait=True),因此除非有充分的理由不使用它,否则应尝试将其添加到conn.connect中.

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.

我对此进行了测试,我认为您从未建立连接.如果添加wait=True,则可能会像我的那样看到连接失败并出现ConnectFailedException的情况.这可能是第一件事.

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.

我已解决它,您需要使用SSL来连接到AWS MQ实例,如下所示:

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

这篇关于AWS Lambda python函数执行返回的结果将以"Null"表示.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆