[英]lambda client invoke function and read json messages from MQ and send to AWS Kinesis
I have two lambda function written in python. 我有两个用python编写的lambda函数。
1- subscriber.py (Will connect to AWS MQ, collect and displays the message) 1-subscription.py(将连接到AWS MQ,收集并显示消息)
import time
import boto3
import stomp
import json
global message
lambda_client = boto3.client('lambda')
class Listener(stomp.ConnectionListener):
def on_error(self, headers, message):
print('received an error "%s"' % message)
def on_message(self, headers, message):
print('received a message in subscriber : "%s"' % message)
invoke_response = lambda_client.invoke(FunctionName="op_worker",
InvocationType='Event',
Payload=json.dumps(message)
)
#print('invoke_response a message in subscriber : "%s"' % invoke_response)
print("terstesfsff")
print (invoke_response['Payload'].read())
def lambda_handler(event, context):
conn = stomp.Connection(host_and_ports=[('b-4714-4441-8166-1.mq.eu-central-1.amazonaws.com', 61614)])
lst = Listener()
conn.set_listener('Listener', lst)
conn.set_ssl(for_hosts=[('b-4714-4441-8166-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/', id=1, ack='auto')
print('CONNECTION Subscribed')
time.sleep(10)
conn.disconnect()
return
2- worker.py (Which needs to collect message from subscriber.py function and pass this message to Kinesis) 2- worker.py(需要从Subscriber.py函数收集消息并将此消息传递给Kinesis)
import time
import boto3
import stomp
import json
lambda_client = boto3.client('lambda')
kinesis_client = boto3.client('kinesis')
def lambda_handler(event, context):
#print (invoke_response['Payload'].read())
print('received a message in worker : "%s"' % message)
kinesis_client.put_record(
StreamName='',
Data=b'bytes',
PartitionKey='1'
)
return {
'message' : message
}
Error message while I am executing subscriber.py Lambda function:- 我执行Subscriber.py Lambda函数时出现错误消息:-
Response:
null
Request ID:
"009406bc-f334-11e8-950f-fde41035f262"
Function Logs:
START RequestId: 009406bc-f334-11e8-950f-fde41035f262 Version: $LATEST
CONNECTION Started
CONNECTION established
CONNECTION Subscribed
received a message in subscriber : "{
"rateChange":{
"contractCode":"DOPC/DOPC0004/W19",
"sourceMarketGroup":"TNO"
}
}"
terstesfsff
b''
END RequestId: 009406bc-f334-11e8-950f-fde41035f262
REPORT RequestId: 009406bc-f334-11e8-950f-fde41035f262 Duration: 10255.69 ms Billed Duration: 10300 ms Memory Size: 128 MB Max Memory Used: 33 MB
Error message on worker.py:- 关于worker.py的错误消息:-
{
"errorMessage": "name 'message' is not defined",
"errorType": "NameError",
"stackTrace": [
[
"/var/task/op_worker.py",
12,
"lambda_handler",
"print('received a message in worker : \"%s\"' % message)"
]
Subscriber Cloudwatch events
订户Cloudwatch事件
Worker Cloudwatch events 工人Cloudwatch事件
Here's the problem: 这是问题所在:
subscriber.py
returns no error, but rather what is supposed to return. subscriber.py
没有返回错误,而是应该返回的错误。 It returns b''
as response from your worker.py
call because worker.py
yields no result b''
作为来自worker.py
调用的响应,因为worker.py
不产生结果 worker.py
returns error because there's no variable called message
. worker.py
返回错误,因为没有名为message
的变量。 The json you send as function parameter when calling worker.py
is inside the event
object and you should access the information you are sending through event
. worker.py
时作为函数参数发送的json在event
对象内部,您应该访问通过event
发送的信息。 Updated code for worker:- 工作人员的更新代码:-
import time
import boto3
import stomp
import json
lambda_client = boto3.client('lambda')
kinesis_client = boto3.client('kinesis')
def lambda_handler(event, context):
print('received a message in worker : "%s"' % event)
kinesis_client.put_record(
StreamName='OnePurchasing',
Data=b'bytes',
PartitionKey='1'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.